Process API β
- fork()
- wait()
- exec()
- kill()
- pipe()
Fork π΄ β
- creates a child process that is a clone of the parent
- The child process differs from the parent process only in its process id and its parent process id
- The fork() is called once but it returns twice!
Process Hierarchy β
Exercise β
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set to indicate the error.
/* process A */
/* ... */
if ( fork () == 0) {
if ( fork () == 0) {
if ( fork () == 0) {
if ( fork () == 0) {
/* do something */
}
}
}
}
How many new processes are created by the code snippet?
Waitπ β
Sometimes, as it turns out, it is quite useful for a parent to wait for a child process to finish what it has been doing. This task is accomplished with the wait() system call (or its more complete sibling waitpid()
Exec β
A final and important piece of the process creation API is the exec() system call. This system call is useful when you want to run a program that is different from the calling program
Linux vs Windows β
Linux | Win32 |
---|---|
fork(), exec() | CreateProcess() (fork exec combined) |
exit() | ExitProcess() |
wait(), waitpid() | WaitForSingleObject(), WaitForMultipleObjects() |
kill() | TerminateProcess() |
Signals β
Linux/Unix signals are a type of event. Signals are asynchronous in nature and are used to inform processes of certain events happening (man 7 signal).
Examples β
- SIGIN
- SIGCON
- SIGKILL