C process control
Functions of the C standard software library
C process control refers to a group of functions in the standard library of the C programming language implementing basic process control operations. The process control operations include actions such as termination of the program with various levels of cleanup, running an external command interpreter or accessing the list of the environment operations.
01Overview of functions
The process control functions are defined in the stdlib.h header (cstdlib header in C++).
| Function | Description | |
|---|---|---|
| Terminating a program |
abort | causes abnormal program termination (without cleaning up) |
| exit | causes normal program termination with cleaning up | |
| _Exit | causes normal program termination without cleaning up (C99) | |
| atexit | registers a function to be called on exit() invocation | |
| quick_exit | causes normal program termination without cleaning up, but with IO buffers flushed (C11) | |
| at_quick_exit | registers a function to be called on quick_exit() invocation | |
| Communicating with the environment |
getenv | accesses the list of the environment variables |
| system | calls the host environment's command processor |
02Example
The following is an example of communicating with the system environment in C.
#include <stdio.h> #include <stdlib.h> int main() { char* path = getenv("PATH"); if (!path) { fprintf(stderr, "PATH environment variable not found.\n"); } else { printf("PATH variable: %s\n", path); } printf("Listing current directory contents using system(\"ls\"):\n"); int ret = system("ls"); if (ret == -1) { fprintf(stderr, "system() call failed!"); } return 0; }Sources and credits
This article is adapted from the Wikipedia article “C process control”, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.
Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.