Docsity

Understanding Concurrent Processes: Context Switches, Forking, & Exit in OS - Prof. Richar, Study notes of Computer Science

Toggle sidebar
bg1
Page 1
Concurrent Processes
Concurrent Processes
Topics
Topics
Process context switches
Creating and destroying processes
–2–
Processes
Processes
Def: A
Def: A process
process is an instance of a running program.
is an instance of a running program.
One of the most profound ideas in computer science.
Not the same as “program” or “processor”
Process provides each program with two key
Process provides each program with two key
abstractions:
abstractions:
Logical control flow
zEach program seems to have exclusive use of the CPU.
Private address space
zEach program seems to have exclusive use of main memory.
How are these Illusions maintained?
How are these Illusions maintained?
Process executions interleaved (multitasking)
Address spaces managed by virtual memory system
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Concurrent Processes: Context Switches, Forking, & Exit in OS - Prof. Richar and more Study notes Computer Science in PDF only on Docsity!

Concurrent Processes Concurrent Processes

TopicsTopics

„ Process context switches

„ Creating and destroying processes

  • 2 –

Processes Processes

Def: ADef: A processprocess is an instance of a running program.is an instance of a running program.

„ One of the most profound ideas in computer science.

„ Not the same as “program” or “processor”

Process provides each program with two keyProcess provides each program with two key

abstractions:abstractions:

„ Logical control flow

z Each program seems to have exclusive use of the CPU.

„ Private address space

z Each program seems to have exclusive use of main memory.

How are these Illusions maintained?How are these Illusions maintained?

„ Process executions interleaved (multitasking)

„ Address spaces managed by virtual memory system

  • 3 –

Logical Control Flows Logical Control Flows

Time

Process A Process B Process C

Each process has its own logical control flow

  • 4 –

Concurrent Processes Concurrent Processes

Two processesTwo processes run concurrentlyrun concurrently (( are concurrent)are concurrent) ifif

their flows overlap in time.their flows overlap in time.

Otherwise, they areOtherwise, they are^ sequential.sequential.

Examples:Examples:

„ Concurrent: A & B, A & C

„ Sequential: B & C

Time

Process A Process B Process C

  • 7 –

Private Address Spaces Private Address Spaces

Each process has its own private address space.Each process has its own private address space.

kernel virtual memory (code, data, heap, stack)

memory mapped region for shared libraries

run-time heap (managed by malloc)

user stack (created at runtime)

0 unused

%esp (stack pointer)

memory invisible to user code

brk

0xc

0x

0x

read/write segment (.data, .bss) read-only segment (.init, .text, .rodata)

loaded from the executable file

0xffffffff

  • 8 –

fork fork : Creating new processes: Creating new processes

intint fork(voidfork(void))

„ creates a new process (child process) that is identical to the

calling process (parent process)

„ returns 0 to the child process

„ returns child’s pid to the parent process

if (fork() == 0) {

printf("hello from child\n");

} else {

printf("hello from parent\n");

Fork is interesting

(and often confusing)

because it is called

once but returns twice

  • 9 –

Fork Example #1 Fork Example

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);

Key PointsKey Points

„ Parent and child both run same code

z Distinguish parent from child by return value from fork

„ Start with same state, but each has private copy

z Including shared output file descriptor

z Relative ordering of their print statements undefined

  • 10 –

Fork Example #2 Fork Example

void fork2()

printf("L0\n");

fork();

printf("L1\n");

fork();

printf("Bye\n");

Key PointsKey Points

„ Both parent and child can continue forking

L0 L

L

Bye

Bye

Bye

Bye

  • 13 –

Fork Example #5 Fork Example

void fork5()

printf("L0\n");

if (fork() == 0) {

printf("L1\n");

if (fork() == 0) {

printf("L2\n");

fork();

printf("Bye\n");

Key PointsKey Points

„ Both parent and child can continue forking

L0 Bye

L

Bye

Bye

Bye

L

  • 14 –

exit exit : Destroying Process: Destroying Process

void void exit(intexit(int status)status)

„ exits a process

z Normally return with status 0

„ atexit() registers functions to be executed upon exit

void cleanup(void) {

printf("cleaning up\n");

void fork6() {

atexit(cleanup);

fork();

exit(0);

  • 15 –

Zombies Zombies

IdeaIdea

„ When process terminates, still consumes system resources

z Various tables maintained by OS

„ Called a “zombie”

z Living corpse, half alive and half dead

ReapingReaping

„ Performed by parent on terminated child

„ Parent is given exit status information

„ Kernel discards process

What if Parent Doesn’t Reap?What if Parent Doesn’t Reap?

„ If any parent terminates without reaping a child, then child

will be reaped by init process

„ Only need explicit reaping for long-running processes

z E.g., shells and servers

  • 16 –

linux> ./forks 7 & [1] 6639 Running Parent, PID = 6639 Terminating Child, PID = 6640 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6639 ttyp9 00:00:03 forks 6640 ttyp9 00:00:00 forks 6641 ttyp9 00:00:00 ps linux> kill 6639 [1] Terminated linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6642 ttyp9 00:00:00 ps

Zombie

Example

Zombie

Example

„ ps shows child

process as “defunct”

„ Killing parent allows

child to be reaped

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

  • 19 –

wait wait : Synchronizing with children: 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();

HP

HC Bye

CT Bye

  • 20 –

Wait Example Wait Example

„ If multiple children completed, will take in arbitrary order

„ Can use macros WIFEXITED and WEXITSTATUS to get

information 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 terminate abnormally\n", wpid);

  • 21 –

Waitpid Waitpid

„ waitpid(pid, &status, options)

z Can wait for specific process

z Various options

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);

  • 22 –

Wait/Waitpid Example Outputs Wait/Waitpid Example Outputs

Child 3565 terminated with exit status 103

Child 3564 terminated with exit status 102

Child 3563 terminated with exit status 101

Child 3562 terminated with exit status 100

Child 3566 terminated with exit status 104

Child 3568 terminated with exit status 100

Child 3569 terminated with exit status 101

Child 3570 terminated with exit status 102

Child 3571 terminated with exit status 103

Child 3572 terminated with exit status 104

Using wait ( fork10 )

Using waitpid ( fork11 )