Fork Unix: Difference between revisions
pc>Yuron No edit summary |
W81054ch [PHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs) m (1 revision imported) |
(No difference)
|
Latest revision as of 10:03, 5 August 2019
Depends on | Processes |
---|
fork()
is the means by which a Unix process creates another process. It is a system call which is called once but returns twice – once in the calling process and again in a nearly identical copy of that code (but in a separate process). It is then usual to break this symmetry – which can be done by checking the value returned by the fork()
– and have one process evolve to run a different program.
A typical example (in C) might look roughly like this:
new_PID = fork();
if (new_PID == -1) exit(1); // Error case
if (new_PID != 0) // This is the parent process
{
... // Record the child PID for later reference
}
else // (new_PID == 0) This is the child process
exec(<arguments>) // Load and execute other code
Here, exec()
is representative of a number of variant calls.
The example above neglects any details about resynchronising and terminating processes, which is discussed elsewhere. Here it is sufficient to note that the parent can record the child’s PID on creation (it may have several child processes) and the child can find its parent (it can only have one!) as ppid
.
This topic is part of this exercise.
Also refer to: | Operating System Concepts, 10th Edition: Chapter 3.3.1, pages 116-121 |
---|