Fork Unix

From COMP15212 Wiki
Revision as of 16:35, 18 July 2019 by pc>Yuron
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

fork & exec

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.



Articles on Processes
About this resource • Atomicity • Containers • Context • Context Switching • Daemons • Fork Unix • Hypervisor • Idle • Interprocess Communication • Multi Threading • Mutual exclusion • Pipes • Pointer Arithmetic • Process Control Block (PCB) • Process Priority • Process Scheduling • Process States • Processes • Queues • Queues Extra • Race Conditions • Real Time • Resources • Scheduler • Signal and Wait • Sleep • Starvation • Synchronisation • Thrashing • Threads • Unix Signals