0% found this document useful (0 votes)
18 views14 pages

RDoc 37568

Uploaded by

roopasree004
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)
18 views14 pages

RDoc 37568

Uploaded by

roopasree004
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/ 14

Process creation

Creating a process by cloning


Cloning
child process is an exact replica of the parent
fork system call

Process 1

System call fork

Kernel
(execute fork)
Creating a process by cloning
Cloning
child process is an exact replica of the parent
fork system call
parent child

Process 1 Process 1 Process 2

System call fork

Kernel Kernel
(execute fork) (execute fork)
Creating a process by cloning
(using fork system call)
If any error by fork(),
int p; PID : p = -1

p=fork();
if (p>0){ parent child
printf((“parent : child PID =%d”,p);
p=wait(); Process 1 Process 2
printf(“parent : child %d existed\n”,p)
} else if (p==0){ PID : p > 0 PID : p = 0
printf(“in child process”);
exit(0);
}
else{
printf(“error”); Kernel
} (execute fork)
fork : from an OS perspective
contains PID,
Parent process information in kernel state, parent,
Files opened,
Page Pointers to
table Kernel page table,
stack kernel stack,
etc.
PCB
fork : from an OS perspective
kernel contains PID,
Parent process information in kernel state, parent,
Files opened,
Page Pointers to
table Kernel page table,
stack kernel stack,
etc.
PCB

child process information in kernel • Find an unused PID


• Set state to NEW
PCB
Page • Set pointers to newly formed
table Kernel • Page table
stack • Kernel stack
• etc.
• Copy information like
files opened, size,
cwd, parent
fork : from an OS perspective
kernel contains PID,
Parent process information in kernel state, parent,
Files opened,
Page Pointers to
table Kernel page table,
stack kernel stack,
etc.
PCB

• Find an unused PID


child process information in kernel • Set state to NEW
• Set pointers to newly formed
PCB
Page • Page table
table Kernel • Kernel stack
stack • etc.
• Copy information like
files opened, size,
cwd, parent
• Set the state to READY
fork : from an OS perspective contains PID,
state, parent,
Files opened,
kernel Pointers to
Parent process information in kernel page table,
kernel stack,
Page etc.
table Kernel
stack State NEW indicates
the PID has been
PCB taken, the process is
being created but
not ready to run
• Find an unused PID
child process information in kernel
• Set state to NEW
• Set pointers to newly formed
PCB
Page • Page table
table • Kernel stack
Kernel
State READY means the • etc.
stack
process is in ready queue • Copy information like
and ready to run files opened, size,
cwd, parent
• Set the state to READY
return from fork
Return from fork is placed in
kernel kernel stack
Parent process information in kernel
Return value in parent has new
Page Kernel child’s PID
table Stack
PID Return value in child has 0 (zero)
PCB

child process information in kernel


PCB
Page Kernel
table Stack
0
Copying page tables Parent and child page tables point to the
same physical memory

kernel
Parent process information in kernel
Page Kernel Page
table Stack table

PCB

child process information in kernel page


table
PCB
Page Kernel
table Stack

RAM
Parent process child process
virtual memory map virtual memory map Duplicating page tables RAM
9 9 15 4
8 8
14 5
7 7
13
6 6
5 5 12 3
4 4
11
3 3 Parent’s process child process
2 2 Page table Page table
10 1
1 1 block Page frame block Page frame 9

1 10 1 10 8 2
2 8 2 8 7
3 12 3 12
6
4 15 4 15
5
5 14 5 14

6 1 6 1
4 9

kernel
7 2 7 2 3 8
8 3 8 3 2 7
9 4 9 4 1 6
example

int i=20, pid;


pid = fork();
if (pid>0) { Page
sleep(1); table
printf(“parent : %d\n”, i)
wait();
} else {
printf(“ child : %d\n”, i)
}
page
table

i
OUTPUT

child : 20
RAM
Parent : 20
Copy on write

int i=20, pid;


pid = fork();
if (pid>0) { Page
sleep(1); table
printf(“parent : %d\n”, i)
wait();
} else {
i=i+1;
printf(“ child : %d\n”, i)
page
}
table

i
OUTPUT

child : 21
RAM
Parent : 20
Copy on write

Page
• All parent pages initially marked as shared
table
(a bit in parent and child page tables)

• When data in any of the shared pages change, OS


intercepts and makes a copy of the page.

• Thus, parent and child will have different copies of page i of child
this page ( all other pages remain the same) table here
i of parent
here

RAM

You might also like