Process Control
fork()
Creates a new child process by duplicating the calling (parent) process.
1pid_t fork(void);
Return Value:
- Parent: child’s PID (positive)
- Child: 0
- Error: -1
Example:
wait()
Suspends execution of the calling process until any of its child processes terminates.
1pid_t wait(int *status);
Return Value:
- Success: PID of terminated child
- Error: -1
Example:
waitpid()
Waits for a specific child process or set of children to terminate.
1pid_t waitpid(pid_t pid, int *status, int options);
Return Value:
- Success: PID of child
- Error: -1
- With WNOHANG and no children ready: 0
Example:
exit()
Terminates the calling process and performs cleanup (e.g., flushes stdio buffers).
1void exit(int status);
Return Value:
- Does not return
Example:
1exit(0);
_exit()
Immediately terminates the process without flushing stdio buffers or calling cleanup handlers.
1void _exit(int status);
Return Value:
- Does not return
Example:
1_exit(0);
MACROS
WIFEXITED
Returns true if the child terminated normally (via exit() or _exit()).
1WIFEXITED(status)
Return Value:
- True if child terminated normally
Example:
WEXITSTATUS
Returns the exit status code of the child (only valid if WIFEXITED(status) is true).
1WEXITSTATUS(status)
Return Value:
- Return code when WIFEXITED is true
Example:
WIFSIGNALED
Returns true if the child terminated due to an uncaught signal.
1WIFSIGNALED(status)
Return Value:
- True if child terminated by signal
Example: