Professional Documents
Culture Documents
II.2 Exercise 1
1. After compiling of exo1.c (see Appendix), we will execute the exo1 program several times.
Why are five lines displayed when there are only two printf in the program?
2. Now, add the following line behind the call to fork :
if (valeur == 0) sleep (4);
What is going on ? Why ?
II. 3 Exercise 2
After execution and using the following diagram, note the PIDs and number the order of execution of the
printf instructions so as to find the order of execution of the processes.
Remark:
if we note a process number equal to 1, it is the init process, father of all the processes. init adopts orphan
processes, i.e. a process whose parent has terminated becomes a child of the process whose pid equals 1, and
getppid() returns 1.
1
III. Synchronization of parent and child processes (wait/exit
mechanism)
III.1 Used functions
• exit( i ) :
ends a process, i is a byte (therefore possible values: 0 to 255) returned in an int type variable to the
parent process.
• wait( &status ):
puts the process on hold for one of its child processes to terminate.
When a process terminates, the SIGCHILD signal is sent to its parent. Receipt of this signal causes the
parent process to move from the blocked state to the ready state. The parent process therefore exits the
wait function.
The return value of wait is the PID of the child process that has just terminated.
When there are no more (or no) child processes to wait for, the wait function returns -1.
Each time a child terminates the parent process exits wait, and it can consult status for information about the
child that just terminated.
• The most significant byte contains the value returned by the child ( i of the exit( i function),
• the least significant byte:
o contains 0 in the general case
o In case of abnormal termination of the child process, this least significant byte contains the value
of the signal received by the child. This value is increased by 80 in hexadecimal (128 decimal), if
this signal caused the memory image of the process to be saved in a core file.
Diagram summarizing the content of the word pointed to by status at the exit of wait:
2
III.2 Mechanism wait/exit
1. Run the program several times after compilation, then explain how it works. (NB: before terminating the
execution of the program, use the command “ps –ef” in another shell to see the PIDs).
2. What is the return of the “wait” function?
3. Changing the value of i in the “exit(i)” function, what do you notice? (NB: see the values of the 2 variables
" etat " and " ex_etat "). You infer the output of the "WEXITSTATUS" macro regarding the "wait"
function.
4. What happens to program execution if you eliminate the “wait” function? What will be the PPID of the
program (the child) in this case? Explain.
Appendix:
exo1.c:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
fork-mul.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
3
valeur, (int)getpid(), (int)getppid() );
valeur1 = fork();
printf (" print 3 - retour fork: %d - processus num= %d -num pere=%d \n",
valeur1, (int)getpid(), (int)getppid() );
return 0;
}
fork-sync.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>