0% found this document useful (0 votes)
116 views

Fork

The document discusses the fork system call in Unix/Linux operating systems. It explains that fork creates a new process called the child process that is identical to the parent process. Fork returns 0 to the child process and returns the child's process ID to the parent process. This allows the parent and child processes to execute the same code but distinguish themselves. Examples are provided to illustrate how fork works and how multiple levels of child processes can be created. The exit system call is also introduced to end a process. Zombie processes are discussed which occur when a child process terminates but the parent does not wait for it to reap its exit status.

Uploaded by

dayesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Fork

The document discusses the fork system call in Unix/Linux operating systems. It explains that fork creates a new process called the child process that is identical to the parent process. Fork returns 0 to the child process and returns the child's process ID to the parent process. This allows the parent and child processes to execute the same code but distinguish themselves. Examples are provided to illustrate how fork works and how multiple levels of child processes can be created. The exit system call is also introduced to end a process. Zombie processes are discussed which occur when a child process terminates but the parent does not wait for it to reap its exit status.

Uploaded by

dayesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

University

of Washington

fork: Crea3ng New Processes

int fork(void)
creates a new process (child process) that is iden2cal

to the calling process (parent process)


returns 0 to the child process
returns childs process ID (pid) to the parent process
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

Fork is interes3ng (and o<en confusing) because


it is called once but returns twice
1

University of Washington

Understanding fork
Process n
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

University of Washington

Understanding fork
Process n
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

Child Process m
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid = m

University of Washington

Understanding fork
Process n
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

Child Process m
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
pid = m
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

University of Washington

Understanding fork
Process n

Child Process m

pid_t pid = fork();


if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
pid = m
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
pid = 0
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

University of Washington

Understanding fork
Process n

Child Process m

pid_t pid = fork();


if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
pid = m
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
pid = 0
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();


if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

hello from parent

Which one is rst?

hello from child


6

University of Washington

Fork Example #1

Parent and child both run same code


Dis2nguish parent from child by return value from fork

Start with same state, but each has private copy


Including shared output le descriptor
Rela2ve ordering of their print statements undened

void fork1()
{
int x = 1;
pid_t pid = fork();
if (pid == 0) {
printf("Child has x = %d\n", ++x);
} else {
printf("Parent has x = %d\n", --x);
}
printf("Bye from process %d with x = %d\n", getpid(), x);
}
7

University of Washington

Fork Example #2

Both parent and child can con3nue forking


void fork2()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("Bye\n");
}

University of Washington

Fork Example #2

Both parent and child can con3nue forking


void fork2()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("Bye\n");
}

L0

L1

Bye
Bye

L1

Bye
Bye

University of Washington

Fork Example #3

Both parent and child can con3nue forking


void fork3()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("L2\n");
fork();
printf("Bye\n");
}

10

University of Washington

Fork Example #3

Both parent and child can con3nue forking


void fork3()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("L2\n");
fork();
printf("Bye\n");
}

L2
L1

L0

L1

Bye
Bye

L2

Bye
Bye
Bye
Bye

L2

Bye
Bye

L2

11

University of Washington

Fork Example #4

Both parent and child can con3nue forking


void fork4()
{
printf("L0\n");
if (fork() != 0) {
printf("L1\n");
if (fork() != 0) {
printf("L2\n");
fork();
}
}
printf("Bye\n");
}

12

University of Washington

Fork Example #4

Both parent and child can con3nue forking


void fork4()
{
printf("L0\n");
if (fork() != 0) {
printf("L1\n");
if (fork() != 0) {
printf("L2\n");
fork();
}
}
printf("Bye\n");
}

Bye
Bye
L0

L1

L2

Bye
Bye

13

University of Washington

Fork Example #4

Both parent and child can con3nue forking


void fork5()
{
printf("L0\n");
if (fork() == 0) {
printf("L1\n");
if (fork() == 0) {
printf("L2\n");
fork();
}
}
printf("Bye\n");
}

14

University of Washington

Fork Example #4

Both parent and child can con3nue forking


void fork5()
{
printf("L0\n");
if (fork() == 0) {
printf("L1\n");
if (fork() == 0) {
printf("L2\n");
fork();
}
}
printf("Bye\n");
}

Bye
L2
L1
L0

Bye

Bye

Bye

15

University of Washington

exit: Ending a process

void exit(

status)

exits a process
Normally

atexit return with status 0


void
void cleanup(void)
cleanup(void) {
{
printf("cleaning up\n");
}
void fork6() {
atexit(cleanup);
fork();
exit(0);

16

University of Washington

Zombies

Idea
When process terminates, s2ll consumes system resources
Various tables maintained by OS
Called a zombie
That is, a living corpse, half alive and half dead

Reaping
Performed by parent on terminated child (horror movie!)
Parent is given exit status informa2on
Kernel discards process

What if parent doesnt reap?


If any parent terminates without reaping a child, then child will be
reaped by init process
So, only need explicit reaping in long-running processes
e.g., shells and servers

17

University of Washington

Zombie
Example

void fork7()
{
if (fork() == 0) {
/* Child */
printf("Terminating Child, PID = %d\n",
getpid());
exit(0);
} else {
printf("Running Parent, PID = %d\n",
linux> ./forks 7 &
getpid());
while (1)
[1] 6639
; /* Infinite loop */
Running Parent, PID = 6639
}
Terminating Child, PID = 6640
}

linux> ps
PID TTY
TIME
6585 ttyp9
00:00:00
6639 ttyp9
00:00:03
6640 ttyp9
00:00:00
6641 ttyp9
00:00:00
linux> kill 6639
[1]
Terminated
linux> ps
PID TTY
TIME
6585 ttyp9
00:00:00
6642 ttyp9
00:00:00

CMD
tcsh
forks
forks <defunct>
ps

CMD
tcsh
ps

ps shows child process as


defunct
Killing parent allows child to be
reaped by init

18

University of Washington

Non-termina3ng
Child Example

void fork8()
{
if (fork() == 0) {
/* Child */
printf("Running Child, PID = %d\n",
getpid());
while (1)
; /* Infinite loop */
} else {
printf("Terminating Parent, PID = %d\n",
getpid());
exit(0);
}
}

linux> ./forks 8
Terminating Parent, PID = 6675
Running Child, PID = 6676
linux> ps
PID TTY
TIME CMD
6585 ttyp9
00:00:00 tcsh
6676 ttyp9
00:00:06 forks
6677 ttyp9
00:00:00 ps
linux> kill 6676
linux> ps
PID TTY
TIME CMD
6585 ttyp9
00:00:00 tcsh
6678 ttyp9
00:00:00 ps

Child process s2ll ac2ve even though


parent has terminated
Must kill explicitly, or else will keep
running indenitely

19

University of Washington

Synchroniza3on!

20

University of Washington

wait: Synchronizing with Children

int wait(int *child_status)


suspends current process un2l one of its children terminates
return value is the pid of the child process that terminated
if child_status != NULL, then the object it points to will be set
to a status indica2ng why the child process terminated

21

University of Washington

wait: Synchronizing with Children


void fork9() {
int child_status;
if (fork() == 0) {
printf("HC: hello from child\n");
}
else {
printf("HP: hello from parent\n");
wait(&child_status);
printf("CT: child has terminated\n");
}
printf("Bye\n");
exit();

HC Bye
HP

CT Bye

22

University of Washington

wait() Example

If mul2ple children completed, will take in arbitrary order


Can use macros WIFEXITED and WEXITSTATUS to get informa2on about
exit status

void fork10()
{
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0)
exit(100+i); /* Child */
for (i = 0; i < N; i++) {
pid_t wpid = wait(&child_status);
if (WIFEXITED(child_status))
printf("Child %d terminated with exit status %d\n",
wpid, WEXITSTATUS(child_status));
else
printf("Child %d terminated abnormally\n", wpid);
}
}
23

University of Washington

waitpid(): Wai3ng for a Specic Process

waitpid(pid, &status, options)


suspends current process un2l specic process terminates
various op2ons (that we wont talk about)

void fork11()
{
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0)
exit(100+i); /* Child */
for (i = 0; i < N; i++) {
pid_t wpid = waitpid(pid[i], &child_status, 0);
if (WIFEXITED(child_status))
printf("Child %d terminated with exit status %d\n",
wpid, WEXITSTATUS(child_status));
else
printf("Child %d terminated abnormally\n", wpid);
}
24

University of Washington

execve: Loading and Running Programs


Stack

int execve(
char *filename,
char *argv[],
char *envp
)

0xbfffffff

Loads and runs


Executable filename
With argument list argv
And environment variable list envp

Does not return (unless error)


Overwrites process, keeps pid
Environment variables:
name=value strings

Null-terminated
environment
variable strings
Null-terminated
commandline
arg strings

unused
envp[n] = NULL
envp[n-1]

envp[0]
argv[argc] = NULL
argv[argc-1]

argv[0]
Linker vars
envp
argv
argc

25

University of Washington

execve: Example

envp[n] = NULL
envp[n-1]

envp[0]

PWD=/homes/iws/luisceze
PRINTER=ps581
USER=luisceze

argv[argc] = NULL
argv[argc-1]

argv[0]

/usr/include
-l
ls

26

University of Washington

Summary

Excep3ons
Events that require non-standard control ow
Generated externally (interrupts) or internally (traps and faults)

Processes
At any given 2me, system has mul2ple ac2ve processes
Only one can execute at a 2me, however,
Each process appears to have total control of
the processor + has a private memory space

27

University of Washington

Summary (contd)

Spawning processes
Call to fork
One call, two returns

Process comple3on
Call exit
One call, no return

Reaping and wai3ng for Processes


Call wait or waitpid

Loading and running Programs


Call execl (or variant)
One call, (normally) no return

28

You might also like