Session 4 : Operating System Programming Concepts : Lab 4 - Process Synchronization


Lab4 : Process Synchronization 

MOst innocent robbery: As the thief left the house, the kid awokened and
said to the thief "Take my school bag you blady, or I will wake
up Mummy!"

Q?Which system call do we use for process synchronization
/>we use system call wait 
/>pid_t wait(int *status)
/>blocks the calling process
until one of its child processes exits or a signal is received
/>performing a wait allows the system to release
the resouces associated with the child. 
/>if a wait is not performed, then the terminated child remains in a
“zombie” state

Q?what is the zombie process 
/>a zombie process or defunct process is a process that has completed
execution but still has an entry in the process table. 
This entry is still needed to allow the parent process to read its child's
exit status. 

Q?what is orphan process
/>the process that is still executing, but whose
parent has died. 
/>this kind of processes do not become zombie processes but are adopted
by init (process id 1), which waits on its childrens


Example:Zombie 

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

//zombie test
int main(){
pid_t pid;
fprintf(stdout,"Parent: my PID is %d: my parent is process %d \n", getpid(),getppid());
pid=fork();
if(pid==0){
//child
fprintf(stdout,"Child,my PID is %d:my parent is process %d\n",getpid(),getppid());
}else{
//Parent
fprintf(stdout,"ID returned to parent:%d\n",pid);
int t=20;
fprintf(stdout,"Sleep for:%d seconds \n",t);
sleep(t);
}
exit(0);
}


Example: Orphan 

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

//zombie test
int main(){
pid_t pid;
fprintf(stdout,"Parent: my PID is %d: my parent is process %d \n", getpid(),getppid());
pid=fork();
if(pid==0){
//child
fprintf(stdout,"Child,my PID is %d:my parent is process %d\n",getpid(),getppid());
int t=20;
fprintf(stdout,"Sleep for:%d seconds \n",t);
sleep(t);
}else{
//Parent
fprintf(stdout,"ID returned to parent:%d\n",pid);
}
exit(0);
}


Q?How do we use the system call waitpid 
/>pid_t waitpid(pid_t pid, int *status, int options)
/>the waitpid() sytem call suspends
execution of the calling process until a child specified by pid
argument has changed state. 
/>by default, waitpid() waits only for terminated children, but its
behaviour is modifiable via options arguments.


Lab 4 : Process Synchronization 

Learning goals: in this laboratory activity you will improve your 
understanding
of the fork() function. Moreover the wait() function is introduced as
a mean to synchronize the execution of parent and child processes. 
Exercise 1 
Write a C program which uses the functions fork and wait to implement the
following precedence graph. 













Be careful P1 is the original parent process. When it first calls fork
it creates a child which is one of P1L or P1R. If for example the
child is P1R the parent will be P1L. Circles do not represent
processes, but only instruction flows. Then P1L and P1R synchronize
themselves in P2 which is the original parent process P1. The same
happens for P2L, P2R and P3. So imagine the parent going on one side
and the child going the other way. Each instruction flow should only
print a message. 
Answer the following questions: 
1.How do you synchronize P1L and P1R? 
2.Which flow is executed first, P1L or P1R? 
3.What’s the purpose of the wait function?
4.What happens if a process is killed and the parent is waiting for its
termination? 

Exercise 2 
Write a C program which creates n-1 processes synchronizing themselves with
the wait function. Each one of the n-1 processes prints on screen the
value of an element of an array of size n. The remaining element
(there are n-1 child processes and n elements) is printed by the
original parent process. 
The program must: 
1.Read a command line parameter n; 
2.Dynamically allocate an array of size n, read n values from stdin and
store the values in the array; 
3.Print the values of the array starting with the last element all the
way down to the first; 
These operations must be done by cloning n-1 times the original parent
process. Each process prints the value of one element of the array
synchronizing itself with the other processes by means of the wait
function. The values must be printed so that the one with index n-1
is the first, then the one with index n-2 and so on. 
Hint
P1 creates a child process P2 and calls wait. 
P2 creates a child process P3 and calls wait. 
...
P(n-1) creates a child process P(n) and calls wait. 
P(n) prints element n of the array end exits. 
P(n-1)prints element n-1 of the array and exits. 
...

P1 printf element 1 of the array and exits. 
(Here array indexes go from 1 to n). 
 
Summary
By the end of this laboratory activity you should be familiar with the
fork function and should also have a clear understanding of the wait
function as a mean to synchronize the execution of a parent and child
process.




Comments

Popular Posts