You are on page 1of 4

Lab on processes

bootstrap loaded at power-up


I-Organization of the lab: bootstrap then loads the kernel
then executs init the first process
Examples to make work + Presentation of a report.
kernel is the only program running at all times

II. Process creation (fork function)


init is the parent of all the processes
II.1 Used functions
process id is an integer number ppid is parent process id and all processes have a
parent
We will first use the following functions:
commands may have arguments or options
• fork() :
This function will create a process. The return value n of this function indicates:
o n>0
We are in the parent process,
o n=0
We are in the child process,
o n = -1
fork failed, could not create process.
• getpid() :
This function returns the current process ID (PID).
• getppid() :
This function returns the parent process ID (PPID)

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

Compile fork-mul.c (see Appendix), then run it.

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.

status is a pointer to a two-byte word:

• 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

Compile and run fork-sync.c (see Appendix).

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>

int main (void){


int valeur;
printf("printf-0 Avant fork : ici processus numero %d\n", (int)getpid());
valeur = fork();
printf("printf-1 Valeur retournee par la fonction fork: %d\n", (int)valeur);
printf("printf-2 Je suis le processus numero %d\n", (int)getpid());
return 0;
}

fork-mul.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main (void)


{

int valeur, valeur1 ;


printf (" print 1 - Je suis le processus pere num=%d \n",
(int)getpid() );
valeur = fork();
printf (" print 2 - retour fork: %d - processus num= %d -num pere=%d \n",

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>

int main (void)


{
int valeur, ret_fils,etat, ex_etat ;
printf ("Je suis le processus pere de pid %d \n\n", (int)getpid());
valeur=fork();
switch (valeur)
{
case 0 :
printf
("* FILS *\n");
printf ("Proc fils de pid %d \nPere de pid %d \n",
(int) getpid(),(int) getppid() );
printf("Je vais dormir 60 secondes ...\n\n");
sleep (60);
printf
("Je suis le fils, je me reveille et Je termine mon execution par un EXIT(7)\n");
exit (7);
case -1:
printf ("Le fork a echoue");
exit(2);
default:
printf("\n* PERE *\n");
printf ("Proc pere de pid %d \nFils de pid %d \n",
(int) getpid(),valeur );
printf ("J'attends la fin de mon fils...\n\n");
ret_fils = wait (&etat);
ex_etat = WEXITSTATUS(etat);
printf("Mon fils de pid %d est termine,Son etat etait :%4x, et son etat de sortie est: %d\n", ret_fils,etat,
ex_etat);
}
return 0;
}

You might also like