CS351 - Fork( ) System Call (2024)

Fork( ) System Call
Single Concept Learning Module

Objectives:

The objective of this module is to introduce students to the fork systemcall. After the audience has listened to the lecture and executed the sampleprogram, students will have the ability to develop multi - process programs.

Concepts:

I. Fork definition.

II. Process Creation.

III. Process Assignment.

IV. Example.

V. Discussion.

Concept Hours:3

Lecture:

The fork() System Call

I. Fork definition

To create a new process, in UNIX, the fork() system call is used. Fork()creates a new context based on the context of the calling process. Thefork() call is unusual in that it returns twice: It returns in both theprocess calling fork() and in the newly created process. The child processreturns zero and the parent process returns a number greater then zero.

The synopsis for fork() is as follows:

#include

pid_t fork(void);

pid_t vfork(void);

If fork() is successful, it returns a number of type pid_t which isgreater than 0 and represents the PID of the newly created child process.In the child process, fork() returns 0. If fork() fails then its returnvalue will be less than 0. vfork() is a m ore efficient version of fork(),which does not duplicate the entire parent context.

II. Process Creation.

A example of fork() follows. Here, the parent process prints Hello tostdout, and the new child process prints World. Note that the order ofprinting is not guaranteed. Without some method of synchronizing the processesexecution, Hello may or may not be printed before World.

#include

#include

char string1[] = "\n Hello";

char string2[] = " CS560.\n";

int main(void)

{

pid_t PID;

PID = fork();

if (PID == 0) /* Child process */

printf("%s", string2);

else /* Parent process */

printf("%s", string1);

exit(0); /* Executed by both processes */

}

III. Process Assignment

The fork() creates a copy of the process that was executing. The fork()is called once but returns twice (once in the parent and once in the child).

The line PID = fork(); returns the value of the fork() system call.The if (PID == 0) evaluates the return value. If PID is equal to zero thenprintf() is executed in the child process, but not in the parent process.

The else part of the condition is executed in the parent process andthe child process but only the parent process will execute the else partof the condition.

IV. Example

The child process can obtain the process ID of the parent by using thegetppid system call. Below is an example of the fork and getppid systemcalls.

#include

void

main(void)

{

int childpid;

if( (childpid = fork() ) == -1)

{

printf("\n Can't fork.\n");

exit(0);

}

else

if(childpid == 0)

{ /* Child process */

printf("\n Child: Child pid = %d, Parent pid = %d \n", getpid(),getppid());

exit(0);

}

else

{ /* Parent Process */

printf("\n Parent: Child pid = %d, Parent pid = %d \n", childpid,getpid());

exit(0);

}

}

V. Discussion

One important feature of the fork system call is that the files thatwere open in the parent process before the fork are shared by the childprocess after the fork. This feature allows the parent to pass open filehandles and device driver handles to th e child.

The values of the following variables are copied from the parent processto the child process.

· real user ID

· real group

· effective user ID

· effective group ID

· process group ID

· terminal group ID

· root directory

· current working directory

· signal handling settings

· file mode creation mask

The child process differs from the parent process in the followingways:

· the child process has a new, unique process ID,

· the child process has a different parent process ID,

· the child process has its own copies of the parent's file descriptors,

· the time left until an alarm clock signal is set to zero inthe child.

In review, there are basically two uses for the fork operation.

1. A process wants to make a copy of itself so that one copy can handlean operation while the other copy does another task.

2. A process wants to execute another program. Since the only way tocreate a new process is with the fork operation, the process must firstfork to make a copy of itself, then one of the copies issues an exec systemcall operation to execute a new program. This is typical for programs suchas shells.

CS351 - Fork( ) System Call (2024)
Top Articles
Aerospace and Defense Market Size, Share, Growth, Analysis 2023-2030
Is getting a 3 on an AP exam bad?
Fiskars X27 Kloofbijl - 92 cm | bol
Jail Inquiry | Polk County Sheriff's Office
Craigslist Free En Dallas Tx
Algebra Calculator Mathway
What Are the Best Cal State Schools? | BestColleges
T Mobile Rival Crossword Clue
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Sprague Brook Park Camping Reservations
Tyrunt
Items/Tm/Hm cheats for Pokemon FireRed on GBA
Jack Daniels Pop Tarts
Classroom 6x: A Game Changer In The Educational Landscape
7 Low-Carb Foods That Fill You Up - Keto Tips
ocala cars & trucks - by owner - craigslist
Walmart Windshield Wiper Blades
Conan Exiles Thrall Master Build: Best Attributes, Armor, Skills, More
I Touch and Day Spa II
Pricelinerewardsvisa Com Activate
Glenda Mitchell Law Firm: Law Firm Profile
Scout Shop Massapequa
Sea To Dallas Google Flights
Ivegore Machete Mutolation
Anotherdeadfairy
F45 Training O'fallon Il Photos
2487872771
Sofia the baddie dog
Page 2383 – Christianity Today
Wood Chipper Rental Menards
Radical Red Ability Pill
Hwy 57 Nursery Michie Tn
Rek Funerals
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
Vlacs Maestro Login
Account Now Login In
Fox And Friends Mega Morning Deals July 2022
Mg Char Grill
Supermarkt Amsterdam - Openingstijden, Folder met alle Aanbiedingen
Devin Mansen Obituary
Naya Padkar Newspaper Today
Imperialism Flocabulary Quiz Answers
How to Draw a Sailboat: 7 Steps (with Pictures) - wikiHow
Is Arnold Swansinger Married
Janaki Kalaganaledu Serial Today Episode Written Update
US-amerikanisches Fernsehen 2023 in Deutschland schauen
Is Ameriprise A Pyramid Scheme
Ghareeb Nawaz Texas Menu
Yale College Confidential 2027
Where and How to Watch Sound of Freedom | Angel Studios
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5593

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.