0% ont trouvé ce document utile (0 vote)
85 vues5 pages

TD1 Processus

Transféré par

Aymen Bhd
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
85 vues5 pages

TD1 Processus

Transféré par

Aymen Bhd
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

éJ J.

ª‚Ë@
 éJ £@Q®Ö ß YË@ éK QK@Qm.Ì '@ éK PñêÒm.Ì '@
àA‚ÒÊK - éJ ®J J.¢JË@ ÐñʪË@ ú¯ AJʪË@ éƒPYÖ Ï@
Higher School in Applied Sciences-Tlemcen (ESSAT)

Module : Système temps réel Filière : Électrotechnique

TD N◦ 1 : Les processus

Exercice 1
Écrire un programme en C qui permet de créer deux processus en affichant le PID du processus
et sa nature (père, fils).

Exercice 2
Écrire un programme en C qui permet de créer une chaı̂ne de n-1 processus en affichant le PID
du processus fils et le PID du processus père.

Exercice 3
Écrire un programme en C qui permet de créer deux processus dont le but est d’incrémenter
une variable initialisée a 20, tel que le processus père permet de l’incrémenter par 100 et le fils par
10.

Exercice 4
Donner le résultat d’exécution du programme suivant :
1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e <u n i s t d . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 i n t main ( ) {
5 i n t i , n=5;
6 int childpid ;
7 f o r ( i =1; i <n ; i ++){
8 i f ( c h i l d p i d=f o r k ( ) <=0)break ;
9 p r i n t f ( ” P r o c e s s u s %d avec pè r e %d i=%d\n” , g e t p i d ( ) , g e t p p i d ( ) , i ) ;
10 }
11 return 0;}

Exercice 5
Écrire un programme en C qui permet de créer deux processus en affichant le PID du processus
et sa nature (père, fils) et qui oblige le processus père de se terminer après le processus fils.

Ghouti ABDELLAOUI, Lecturer


[email protected]
ESSA-Tlemcen, Department of the second cycle -1- 8 janvier 2022
BP 165 RP Bel Horizon 13000 Tlemcen, Algerie
http://www.essa-tlemcen.dz/
éJ J.ª‚Ë@
 éJ £@Q®Ö ß YË@ éK QK@Qm.Ì '@ éK PñêÒm.Ì '@
àA‚ÒÊK - éJ ®J J.¢JË@ ÐñʪË@ ú¯ AJʪË@ éƒPYÖ Ï@
Higher School in Applied Sciences-Tlemcen (ESSAT)

Exercice 6
Donner le nombre de processus crées par le programme suivant :
1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e <u n i s t d . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 i n t main ( ) {
5 i n t i , n=5;
6 pid t childpid ;
7 p r i n t f ( ” P r o c e s s u s dé p a r t %d\n” , g e t p i d ( ) ) ;
8 f o r ( i =1; i <n ; i ++){
9 c h i l d p i d=f o r k ( ) ;
10 i f ( c h i l d p i d ==−1)break ;
11 e l s e i f ( c h i l d p i d ==0) p r i n t f ( ” P r o c e s s u s %d avec pè r e %d i=%d\n” , g e t p i d ( ) , g e t p p i d ( )
,i);
12 }
13 w h i l e ( w a i t (NULL)>=0) ;
14 return 0;}

Exercice 7
Écrire un programme qui permet de créer deux processus dont chacun doit faire clingonter
deux LEDs avec deux fréquences différentes (1 Hz, 0.5 Hz)

Ghouti ABDELLAOUI, Lecturer


[email protected]
ESSA-Tlemcen, Department of the second cycle -2- 8 janvier 2022
BP 165 RP Bel Horizon 13000 Tlemcen, Algerie
http://www.essa-tlemcen.dz/
éJ J.ª‚Ë@
 éJ £@Q®Ö ß YË@ éK QK@Qm.Ì '@ éK PñêÒm.Ì '@
àA‚ÒÊK - éJ ®J J.¢JË@ ÐñʪË@ ú¯ AJʪË@ éƒPYÖ Ï@
Higher School in Applied Sciences-Tlemcen (ESSAT)

Solutions

Solution 1
1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e <u n i s t d . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 i n t main ( ) {
5 pid t childpid ;
6 p r i n t f ( ” P r o c e s s u s pè r e avant f o r k avec PID = %d\n” , g e t p i d ( ) ) ;
7 c h i l d p i d=f o r k ( ) ;
8 i f ( c h i l d p i d == −1) p r i n t f ( ” E r r o r \n” ) ;
9 e l s e i f ( c h i l d p i d ==0) p r i n t f ( ” P r o c e s s u s f i l s avec PID = %d\n” , g e t p i d ( ) ) ;
10 e l s e p r i n t f ( ” p r o c e s s u s pè r e avec PID = %d\n” , g e t p i d ( ) ) ;
11

12 return 0;}

Solution 2
1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e <u n i s t d . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 i n t main ( ) {
5 int i ;
6 pid t childpid ;
7 p r i n t f ( ” P r o c e s s u s pè r e avant f o r k avec PID = %d\n” , g e t p i d ( ) ) ;
8 f o r ( i =1; i <5; i ++){
9 c h i l d p i d=f o r k ( ) ;
10 i f ( c h i l d p i d > 0 ) break ; // c ’ e s t l e p e r e
11 p r i n t f ( ” P r o c e s s u s %d avec pè r e %d\n” , g e t p i d ( ) , g e t p p i d ( ) ) ;
12 }
13 w h i l e ( w a i t (NULL)>=0) ;
14 return 0;}

Solution 3
1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e <u n i s t d . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 void ParentProcess ( i n t ∗a ) {
5 p r i n t f ( ”Dé marrage du p r o c e s s u s pè r e \n” ) ;
6 ∗ a += 1 0 0 ;
7 }
8 void ChildProcess ( i n t ∗a ) {
9 p r i n t f ( ”Dé marrage du p r o c e s s u s f i l s \n” ) ;

Ghouti ABDELLAOUI, Lecturer


[email protected]
ESSA-Tlemcen, Department of the second cycle -3- 8 janvier 2022
BP 165 RP Bel Horizon 13000 Tlemcen, Algerie
http://www.essa-tlemcen.dz/
éJ J.ª‚Ë@
 éJ £@Q®Ö ß YË@ éK QK@Qm.Ì '@ éK PñêÒm.Ì '@
àA‚ÒÊK - éJ ®J J.¢JË@ ÐñʪË@ ú¯ AJʪË@ éƒPYÖ Ï@
Higher School in Applied Sciences-Tlemcen (ESSAT)

10 ∗ a += 5 0 ;
11 }
12

13 i n t main ( ) {
14 int x = 20;
15 pid t childpid ;
16 c h i l d p i d=f o r k ( ) ;
17 i f ( c h i l d p i d > 0 ) P a r e n t P r o c e s s (&x ) ;
18 e l s e C h i l d P r o c e s s (&x ) ;
19 p r i n t f ( ” x = %d\n” , x ) ;
20 return 0;}

Solution 4
1 $Processus 3920 avec pè r e 3833 i =1
2 $Processus 3920 avec pè r e 3833 i =2
3 $Processus 3920 avec pè r e 3833 i =3
4 $Processus 3920 avec pè r e 3833 i =4

Le programme execute en boucle de 1 jusqu’à 4 un fork, si le résultat obtenu de ce dernier est une
erreur ou un processus fils, il arrête le processus sinon il affiche le PID du processus courant (3920)
et le PID de son père (3833).
Solution 5
1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e <u n i s t d . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 #d e f i n e MAX COUNT 200
5 void ParentProcess ( ) {
6 i n t i =0;
7 f o r ( i =0; i<= MAX COUNT; i ++)
8 p r i n t f ( ” P r o c e s s u s pè r e i = %d\n” , i ) ;
9 w a i t (NULL) ;
10 p r i n t f ( ” Fin p r o c e s s u s pè r e \n” ) ;
11 }
12 void ChildProcess ( ) {
13 i n t i =0;
14 f o r ( i =0; i<= MAX COUNT; i ++)
15 p r i n t f ( ” P r o c e s s u s f i l s i = %d\n” , i ) ;
16 p r i n t f ( ” Fin p r o c e s s u s f i l s \n” ) ;
17 }
18
19 i n t main ( ) {
20 pid t childpid ;
21 c h i l d p i d=f o r k ( ) ;
22 i f ( c h i l d p i d > 0) ParentProcess ( ) ;

Ghouti ABDELLAOUI, Lecturer


[email protected]
ESSA-Tlemcen, Department of the second cycle -4- 8 janvier 2022
BP 165 RP Bel Horizon 13000 Tlemcen, Algerie
http://www.essa-tlemcen.dz/
éJ J.ª‚Ë@
 éJ £@Q®Ö ß YË@ éK QK@Qm.Ì '@ éK PñêÒm.Ì '@
àA‚ÒÊK - éJ ®J J.¢JË@ ÐñʪË@ ú¯ AJʪË@ éƒPYÖ Ï@
Higher School in Applied Sciences-Tlemcen (ESSAT)

23 e l s e ChildProcess () ;
24 return 0;}

Solution 6
1 $Processus dé p a r t 6329
2 $Processus 6330 avec pè r e 6329 i =1
3 $Processus 6331 avec pè r e 6329 i =2
4 $Processus 6333 avec pè r e 6329 i =4
5 $Processus 6332 avec pè r e 6329 i =3
6 $Processus 6337 avec pè r e 6330 i =3
7 $Processus 6334 avec pè r e 6330 i =2
8 $Processus 6338 avec pè r e 6330 i =4
9 $Processus 6339 avec pè r e 6332 i =4
10 $Processus 6335 avec pè r e 6331 i =3
11 $Processus 6340 avec pè r e 6337 i =4
12 $Processus 6341 avec pè r e 6334 i =3
13 $Processus 6342 avec pè r e 6334 i =4
14 $Processus 6343 avec pè r e 6335 i =4
15 $Processus 6336 avec pè r e 6331 i =4
16 $Processus 6344 avec pè r e 6341 i =4

p r o c e s s u s 0 => f i l s 0 1 , f i l s 0 2 , f i l s 0 3 , f i l s 0 4 ;
f i l s 01 => f i l s 0 1 2 , f i l s 0 1 3 , f i l s 014
f i l s 02 => f i l s 0 2 3 , f i l s 024
f i l s 03 => f i l s 034
p r o c e s s u s 01
f i l s 012 => f i l s 0 1 2 3 , f i l s 0124
f i l s 013 => f i l s 0134
p r o c e s s u s 02
f i l s 023 => f i l s 0234
p r o c e s s u s 012
f i l s 0123 => f i l s 01234

Ghouti ABDELLAOUI, Lecturer


[email protected]
ESSA-Tlemcen, Department of the second cycle -5- 8 janvier 2022
BP 165 RP Bel Horizon 13000 Tlemcen, Algerie
http://www.essa-tlemcen.dz/

Vous aimerez peut-être aussi