The fork() System Call (2024)

The fork() System Call (1)

System call fork() is used to create processes. It takes no argumentsand returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of thecaller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork():

  • If fork() returns a negative value, the creation of a child process was unsuccessful.
  • fork() returns a zero to the newly created child process.
  • fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
Therefore, after the system call to fork(), a simple test can tell which process is the child. Please note thatUnix will make an exact copy of the parent's address space andgive it to the child. Therefore, the parent and child processes have separate address spaces.

Let us take an example to make the above points clear. This example doesnot distinguish parent and the child processes.Click here to download this filefork-01.c.

#include <stdio.h>#include <string.h>#include <sys/types.h>#define MAX_COUNT 200#define BUF_SIZE 100void main(void){ pid_t pid; int i; char buf[BUF_SIZE]; fork(); pid = getpid(); for (i = 1; i <= MAX_COUNT; i++) { sprintf(buf, "This line is from pid %d, value = %d\n", pid, i); write(1, buf, strlen(buf)); } }
Suppose the above program executes up to the point ofthe call to fork() (marked in red color):
The fork() System Call (2)

If the call to fork() is executed successfully, Unix will

  • make two identical copies of address spaces, one for the parent and the other for the child.
  • Both processes will start their execution at the next statement following the fork() call. In this case, both processes will start their execution at the assignment statement as shown below:
The fork() System Call (3)

Both processes start their execution right after the system call fork(). Since both processes have identical but separate address spaces, those variables initialized beforethe fork() call have the same values in both address spaces.Since every process has its own address space, any modifications will be independent of the others. In other words, if the parent changes the value of its variable, the modification will only affect the variable in the parentprocess's address space. Other address spaces created by fork()calls will not be affected even though they have identical variable names.

What is the reason of using write rather than printf?It is because printf() is "buffered," meaning printf()will group the output of a process together. While buffering the outputfor the parent process, the child may also use printf to printout some information, which will also be buffered. As a result, since theoutput will not be send to screen immediately, you may not get the rightorder of the expected result. Worse, the output from the two processesmay be mixed in strange ways. To overcome this problem, you mayconsider to use the "unbuffered" write.

If you run this program, you might see the following on the screen:

 ................This line is from pid 3456, value 13This line is from pid 3456, value 14 ................This line is from pid 3456, value 20This line is from pid 4617, value 100This line is from pid 4617, value 101 ................This line is from pid 3456, value 21This line is from pid 3456, value 22 ................
Process ID 3456 may be the one assigned to the parent or the child.Due to the fact that these processes are run concurrently, their outputlines are intermixed in a rather unpredictable way. Moreover, the order ofthese lines are determined by the CPU scheduler. Hence, if you runthis program again, you may get a totally different result.

Consider one more simple example, which distinguishes the parent from the child. Click here to download this filefork-02.c.

#include <stdio.h>#include <sys/types.h>#define MAX_COUNT 200void ChildProcess(void); /* child process prototype */void ParentProcess(void); /* parent process prototype */void main(void){ pid_t pid; pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess();}void ChildProcess(void){ int i; for (i = 1; i <= MAX_COUNT; i++) printf(" This line is from child, value = %d\n", i); printf(" *** Child process is done ***\n");}void ParentProcess(void){ int i; for (i = 1; i <= MAX_COUNT; i++) printf("This line is from parent, value = %d\n", i); printf("*** Parent is done ***\n");}
In this program, both processes print lines that indicate(1) whether the line is printed by the child or by the parent process, and (2) the value of variable i. For simplicity, printf() is used.

When the main program executes fork(), an identical copy of itsaddress space, including the program and all data, is created. System callfork() returns the child process ID to the parent andreturns 0 to the child process. The following figure shows that in bothaddress spaces there is a variable pid. The one in the parent receivesthe child's process ID 3456 and the one in the child receives 0.

The fork() System Call (4)

Now both programs (i.e., the parent and child) will executeindependent of each other starting at the next statement:

The fork() System Call (5)

In the parent, since pid is non-zero, it calls functionParentProcess(). On the other hand, the child has a zero pidand calls ChildProcess() as shown below:

The fork() System Call (6)

Due to the fact that the CPU scheduler will assign a time quantum to each process, the parent or the child process will run for some time before thecontrol is switched to the other and the running process will print some lines before you can see any line printed by the other process. Therefore, the value of MAX_COUNT should be large enough so that both processes will run for at least two or more time quanta. If the value of MAX_COUNT is so small that a process can finish in one time quantum, you will see two groups of lines, each of which contains all lines printed bythe same process.

The fork() System Call (2024)
Top Articles
What Is Inflation and How Does Inflation Affect Investments?
Understanding the Impact of Inflation on Your Savings | Bank of Hillsboro
Toa Guide Osrs
Global Foods Trading GmbH, Biebesheim a. Rhein
Restored Republic January 20 2023
What Happened To Dr Ray On Dr Pol
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
What Happened To Father Anthony Mary Ewtn
Imbigswoo
Tugboat Information
Weather In Moon Township 10 Days
Day Octopus | Hawaii Marine Life
Aquatic Pets And Reptiles Photos
More Apt To Complain Crossword
Theycallmemissblue
Immortal Ink Waxahachie
111 Cubic Inch To Cc
Promiseb Discontinued
Baja Boats For Sale On Craigslist
Www Pointclickcare Cna Login
Turbo Tenant Renter Login
1145 Barnett Drive
From This Corner - Chief Glen Brock: A Shawnee Thinker
2023 Ford Bronco Raptor for sale - Dallas, TX - craigslist
Wonder Film Wiki
208000 Yen To Usd
Rs3 Bring Leela To The Tomb
R/Sandiego
Kaiserhrconnect
Elanco Rebates.com 2022
Wega Kit Filtros Fiat Cronos Argo 1.8 E-torq + Aceite 5w30 5l
Sports Clips Flowood Ms
Kips Sunshine Kwik Lube
Western Gold Gateway
Srg Senior Living Yardi Elearning Login
Indio Mall Eye Doctor
Doordash Promo Code Generator
Riverton Wyoming Craigslist
What Is A K 56 Pink Pill?
Nina Flowers
Academic Calendar / Academics / Home
Grand Valley State University Library Hours
Child care centers take steps to avoid COVID-19 shutdowns; some require masks for kids
Whitney Wisconsin 2022
Maplestar Kemono
Aloha Kitchen Florence Menu
Zits Comic Arcamax
Cognitive Function Test Potomac Falls
Shad Base Elevator
Olay Holiday Gift Rebate.com
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6020

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.