Fork creation
-
Upload
sreedevi-yashpal -
Category
Documents
-
view
223 -
download
0
Transcript of Fork creation
-
8/19/2019 Fork creation
1/23
1
Unix system calls
fork( )
wait( )exit( )
-
8/19/2019 Fork creation
2/23
2
Processes are the primitive units for allocation of system
resources. Each process has its own address space and (usually) one thread
of control.
A process executes a program; you can have multiple processes
executing the same program !ut each process has its own copy ofthe program within its own address space and executes it
independently of the other copies.
Processes are organi"ed hierarchically. Each process has a
parent process which explicitly arranged to create it. #he processes created !y a given parent are called its child
processes.
A child inherits many of its attri!utes from the parent process.
Process $reation
-
8/19/2019 Fork creation
3/23
3
• A process ID number names each process.
• A uni%ue process &' is allocated to each process when it iscreated.
• #he lifetime of a process ends when its termination is reported to
its parent process; at that time all of the process resources
including its process &' are freed.• Processes are created with the fork() system call (so the operation
of creating a new process is sometimes called forking a process).
• #he child process created !y fork is a copy of the original parent
process except that it has its own process &'.
Process $reation
-
8/19/2019 Fork creation
4/23
4
ow #o $reate ew Processes*
Underlying mechanism
-A process runs fork to create a child process
- Parent and children execute concurrently
- $hild process is a duplicate of the parent process
After a fork !oth parent and child keep running and each can fork
off other processes.
parent
child
fork()
-
8/19/2019 Fork creation
5/23
5
+ootstrapping
,hen a computer is switched on or reset there must !e an
initial program that gets the system running
#his is the !ootstrap program
- &nitiali"e $PU registers device controllers memory
--oad the / into memory
- /tart the / running
/ starts the first process (such as 0init1)
/ waits for some event to occur
- ardware interrupts or software interrupts (traps)
-
8/19/2019 Fork creation
6/23
6
2ork /ystem $all
$urrent process split into 3 processes4 parent child
#ext
'ata
/tack
#ext
'ata
/tack
fork() 5eturns 67 if unsuccessful
5eturns 8 in the child
5eturns the child9sidentifier in the parent
-
8/19/2019 Fork creation
7/237
2ork /ystem $all
#he child process inherits from parent
-identical copy of memory
- $PU registers
- all files that have !een opened !y the parent
Execution proceeds concurrently with the instruction following
the fork system call
#he execution context (P$+) for the child process is a copy of the parent9s context at the time of the call
-
8/19/2019 Fork creation
8/23
Process 'escription4 Process $ontrol +lock (P$+)
• &nformation associated with each process
– Process state
– Program counter
– $PU registers
– $PU scheduling information
– :emory6management information
– Accounting information
– & status information
•
A process is named using its process &' (P&')• 'ata is stored in a process control !lock (P$+)
-
8/19/2019 Fork creation
9/239
ow fork ,orks (7)
TextStack
Data
File
Resources
pid = !
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child
code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
%
"#$
%&I'
-
8/19/2019 Fork creation
10/231
ow fork ,orks (3)
Text
"#$
Stack
Data
File
Resources
pid = !
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
%
%&I'
ret =
Text
"#$
Stack
Data
pid =
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
%
ret =
-
8/19/2019 Fork creation
11/231
ow fork ,orks (
-
8/19/2019 Fork creation
12/231
ow fork ,orks (=)
Text
"#$
Stack
Data
File
Resources
pid = !
Text
"#$
Stack
Data
pid =
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
%
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
%
%&I'
ret = ret =
-
8/19/2019 Fork creation
13/231
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
%
ow fork ,orks (>)
Text
"#$
Stack
Data
File
Resources
pid = !
Text
"#$
Stack
Data
pid =
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
%
%&I'
ret = ret =
-
8/19/2019 Fork creation
14/231
ow fork ,orks (?)
Text
"rocess Status
Stack
Data
File
Resources
pid = !
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child code for child !
exit(0);
defa"lt: // I am pare#t $$$
code for parent !
wait(0);
& !
ret =
%&I'
-
8/19/2019 Fork creation
15/237
@includestdio.hB
@includesystypes.hB
@includestdli!.hB
void main()
C int pidn; pid*t pid+ printf(DEnter the num!ernD);
scanf(DFdDGn);
pidHfork();
if(pid8)C printf(0Error1);
exit(7);
I
else if(pidHH8)
C printf(Dchild process4nD);Ielse
C wait(7);
printf(Dparent process4nD);
I
I
-
8/19/2019 Fork creation
16/231
rderly #ermination4 exit()
#o finish execution a child may call exit(number )
#his system call4
- /aves result H argument of exit
- $loses all open files connections
-'eallocates memory
- $hecks if parent is alive
- &f parent is alive holds the result value until the parent re%uests
it (with wait); in this case the child process does not really die
!ut it enters a "om!iedefunct state- &f parent is not alive the child terminates (dies)
-
8/19/2019 Fork creation
17/231
,aiting for the $hild to 2inish
Parent may want to wait for children to finish
-Example4 a shell waiting for operations to complete
,aiting for any some child to terminate4 wait()
- +locks until some child terminates
- 5eturns the process &' of the child process
- r returns 67 if no children exist (i.e. already exited)
#include #include
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
-
8/19/2019 Fork creation
18/23
-
8/19/2019 Fork creation
19/231
ther useful system calls4 getpid, getppid
getpid returns the identifier of the calling process. Example
call (pid is an integer)4
pid getpid();
getppid returns the identifier of the parent.
i l d di h
-
8/19/2019 Fork creation
20/23
@includestdio.hB
@includesystypes.hB
@includestdli!.hB
void main() C int pidn; pidJt pid;
printf(DEnter the num!ernD);
scanf(DFdDGn);
pidHfork();
printf(Dout pid4FdnDpid);
if(pid8)
C printf(DE5rD);
exit(7);
I
3
$
-
8/19/2019 Fork creation
21/23
$ont..
if(pidHH8)
C printf(Dchild process4 Fd nDKKn);
printf(Dch pid HFdnDpid); printf(Dch4fn pid FdnDgetpid());
printf(Dch4fn ppidFdnDgetppid());
exit(8);
I
else C wait(7);
printf(Dpar4 pidFdnDgetppid());
printf(Dpar4 pid varFdnDpid); printf(Dparent process4Fd nDn);
printf(Dpar4 pidFdnDgetpid());
I
3
-
8/19/2019 Fork creation
22/23
,ait()- .xit() / Sleep()
,ait()
- for a child process to terminate or stop and determine its status.
- will force a parent process to wait for a child process to stop or
terminate.
- return the pid of the child or 67 for an error.
.xit()
- exit() terminates the process which calls this function and returns
the exit status value.
- +y convention a status of 8 means normal termination.
- Any other value indicates an error or unusual occurrence.
Sleep ()
- A process may suspend for a period of time using the sleep
command3
-
8/19/2019 Fork creation
23/23
2
/ummary
2ork
-$reates a duplicate of the calling process
- #he result is two processes4 parent and child
- +oth continue executing from the same point on
Exit- rderly program termination
- Un!locks waiting parent
,ait- Used !y parent
- ,aits for child to finish execution