Fork Unix: Difference between revisions

From COMP15212 Wiki
gravatar Yuron [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
pc>Yuron
No edit summary
Line 1: Line 1:
{{#set: Priority=2 | Summary=How Unix processes can create more Unix processes.}}<!--
{{#set: Priority=2 | Summary=How Unix processes can create more Unix processes.}}<!--
-->{{#invoke:Dependencies|add|Processes,3}}
-->{{#invoke:Dependencies|add|Processes,3}}
<code>fork()</code> is the means by which a Unix process creates another
<code>fork()</code> is the means by which a Unix process creates another process.  It is a [[System_Calls|system call]] which is called once but returns <em>twice</em> – 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 <code>fork()</code> – and have one process evolve to run a different program.
process.  It is a [[System_Calls|system call]] which is called once but
returns <em>twice</em> – 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 <code>fork()</code> – and have one process evolve to run a
different program.
A typical example (in C) might look roughly like this:
A typical example (in C) might look roughly like this:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 24: Line 18:
[[Image:fork_exec.png|link=|alt=fork & exec]]
[[Image:fork_exec.png|link=|alt=fork & exec]]


The example above neglects any details about resynchronising and
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 <code>ppid</code>.
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 <code>ppid</code>.


This topic is part of [[Exercises:C_processes|this exercise]].
This topic is part of [[Exercises:C_processes|this exercise]].
----
----
 
{{BookChapter|3.3.1|116-121}}
{{PageGraph}}
{{PageGraph}}
{{Category|Processes}}
{{Category|Processes}}

Revision as of 09:55, 1 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.

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.


Also refer to: Operating System Concepts, 10th Edition: Chapter 3.3.1, pages 116-121


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