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
Migrate from Travis to Azure Pipelines - Azure Pipelines
All You Need To Know About India's Crypto Bill
Creepshotorg
Average Jonas Wife
Ups Stores Near
Nehemiah 4:1–23
How To Be A Reseller: Heather Hooks Is Hooked On Pickin’ - Seeking Connection: Life Is Like A Crossword Puzzle
Eric Rohan Justin Obituary
Citi Card Thomas Rhett Presale
Mission Impossible 7 Showtimes Near Regal Bridgeport Village
Nier Automata Chapter Select Unlock
Springfield Mo Craiglist
What is Cyber Big Game Hunting? - CrowdStrike
Samantha Lyne Wikipedia
2016 Hyundai Sonata Refrigerant Capacity
Violent Night Showtimes Near Amc Fashion Valley 18
Vipleaguenba
Copart Atlanta South Ga
Hdmovie 2
Happy Life 365, Kelly Weekers | 9789021569444 | Boeken | bol
Chase Bank Pensacola Fl
The Tower and Major Arcana Tarot Combinations: What They Mean - Eclectic Witchcraft
Employee Health Upmc
Gazette Obituary Colorado Springs
Yugen Manga Jinx Cap 19
Ontdek Pearson support voor digitaal testen en scoren
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Medline Industries, LP hiring Warehouse Operator - Salt Lake City in Salt Lake City, UT | LinkedIn
Sandals Travel Agent Login
Ncal Kaiser Online Pay
Dairy Queen Lobby Hours
Ezstub Cross Country
Ellafeet.official
Frommer's Belgium, Holland and Luxembourg (Frommer's Complete Guides) - PDF Free Download
Grapes And Hops Festival Jamestown Ny
Bay Focus
دانلود سریال خاندان اژدها دیجی موویز
Dmitri Wartranslated
Elisabeth Shue breaks silence about her top-secret 'Cobra Kai' appearance
Tyler Perry Marriage Counselor Play 123Movies
Jasgotgass2
Restored Republic June 6 2023
Questions answered? Ducks say so in rivalry rout
Clima De 10 Días Para 60120
Danielle Ranslow Obituary
8776725837
Strange World Showtimes Near Century Stadium 25 And Xd
Greatpeople.me Login Schedule
Marcel Boom X
Rick And Morty Soap2Day
Makemkv Key April 2023
Jigidi Jigsaw Puzzles Free
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.