• Aucun résultat trouvé

Licence 2 — I4b Semaines du 04/04 au 7/05/2005

N/A
N/A
Protected

Academic year: 2022

Partager "Licence 2 — I4b Semaines du 04/04 au 7/05/2005"

Copied!
3
0
0

Texte intégral

(1)

Licence 2 — I4b

Semaines du 04/04 au 7/05/2005

TP 5 & 6 • Cr´ eation de processus et threads de C et C++

Exercice 1. Cr´ eation de processus

1. Ex´ ecuter les programmes suivants 2. Interpr` eter le comportement

3. Reprendre l’exercice du TD qui entraine une cr´ eation non contrˆ ol´ ee de processus pour repro- duire son comportement sur le machine

4. Corriger le programme afin d’obtenir uniquement 5 processus fils

1 #i n c l u d e <s t d i o . h>

2 #i n c l u d e <s y s / t y p e s . h>

3 #i n c l u d e <u n i s t d . h>

4

5 i n t main (v o i d)

6 {

7 i n t v a l e u r ;

8 v a l e u r = f o r k ( ) ;

9 p r i n t f ( ” V a l e u r ´e r e t o u r n e p a r l a f o n c t i o n f o r k : % d\n ” , (i n t) v a l e u r ) ;

10 p r i n t f ( ” J e s u i s l e p r o c e s s u s : % d\n ” , (i n t) g e t p i d ( ) ) ;

11 }

Listing 1 – Cr´ eation de processus

1 #i n c l u d e <s t d i o . h>

2 #i n c l u d e <s y s / t y p e s . h>

3 #i n c l u d e <u n i s t d . h>

4

5 i n t main (v o i d)

6 {

7 i n t v a l e u r ;

8 v a l e u r = f o r k ( ) ;

9 i f ( v a l e u r = = 0 ) s l e e p ( 4 ) ;

10 p r i n t f ( ” V a l e u r r e t o u r n e e p a r l a f o n c t i o n f o r k : % d\n ” , (i n t) v a l e u r ) ;

11 p r i n t f ( ” J e s u i s l e p r o c e s s u s : % d\n ” , (i n t) g e t p i d ( ) ) ;

12 }

Listing 2 – Cr´ eation de processus avec attente

1 #i n c l u d e <s t d i o . h>

2 #i n c l u d e <s y s / t y p e s . h>

3 #i n c l u d e <u n i s t d . h>

4

5 i n t main (v o i d)

6 {

7

8 i n t v a l e u r , v a l e u r 1 ;

9 p r i n t f ( ” 1 − J e s u i s l e p r o c e s s u s p e r e num = %d \n ” ,

10 (i n t) g e t p i d ( ) ) ;

1

(2)

11 v a l e u r = f o r k ( ) ;

12 p r i n t f ( ” 2 − r e t o u r f o r k : % d − p r o c e s s u s num= %d − num p e r e=%d \n ” ,

13 v a l e u r , (i n t) g e t p i d ( ) , (i n t) g e t p p i d ( ) ) ;

14 v a l e u r 1 = f o r k ( ) ;

15 p r i n t f ( ” 3 − r e t o u r f o r k : % d − p r o c e s s u s num= %d − num p e r e=%d \n ” ,

16 v a l e u r 1 , (i n t) g e t p i d ( ) , (i n t) g e t p p i d ( ) ) ;

17 }

Listing 3 – Cr´ eation de processus multiples

Exercice 2. Cr´ eation de thread sous UNIX

1. Ex´ ecuter les programmes suivants 2. Interpr` eter leur comportement

3. Reprendre le programme sur la banque et les distributeur et le r´ ealiser en C/C++, inspirez vous du programme utilisant les s´ emaphores

1 #i n c l u d e <s t d i o . h>

2 #i n c l u d e <s t d l i b . h>

3 #i n c l u d e <p t h r e a d . h>

4

5 v o i d ∗m y t h r e a d p r o c e s s (v o i d ∗ a r g )

6 {

7 i n t i ;

8

9 f o r ( i = 0 ; i < 5 ; i ++) {

10 p r i n t f ( ” Thre ad %s : % d\n ” , (c h a r∗) a r g , i ) ;

11 s l e e p ( 1 ) ;

12 }

13 p t h r e a d e x i t ( 0 ) ;

14 }

15

16 main (i n t ac , c h a r ∗ ∗av )

17 {

18 p t h r e a d t th1 , t h 2 ;

19 v o i d ∗r e t ;

20

21 i f ( p t h r e a d c r e a t e (& th1 , NULL , m y t h r e a d p r o c e s s , ” 1 ” ) < 0 ) {

22 f p r i n t f ( s t d e r r , ” p t h r e a d c r e a t e e r r o r f o r t h r e a d 1\n ” ) ;

23 e x i t ( 1 ) ;

24 }

25

26 i f ( p t h r e a d c r e a t e (& th2 , NULL , m y t h r e a d p r o c e s s , ” 2 ” ) < 0 ) {

27 f p r i n t f ( s t d e r r , ” p t h r e a d c r e a t e e r r o r f o r t h r e a d 2\n ” ) ;

28 e x i t ( 1 ) ;

29 }

30

31 (v o i d) p t h r e a d j o i n ( th1 , & r e t ) ;

32 (v o i d) p t h r e a d j o i n ( th2 , & r e t ) ;

33 }

Listing 4 – Cr´ eation de thread

1 #i n c l u d e <s t d i o . h>

2 #i n c l u d e <s t d i o . h>

3 #i n c l u d e <s t d l i b . h>

4 #i n c l u d e <p t h r e a d . h>

2

(3)

5 #i n c l u d e <s e m a p h o r e . h>

6

7 s t a t i c s e m t my sem ;

8 i n t t h e e n d ;

9

10 v o i d ∗t h r e a d 1 p r o c e s s (v o i d ∗ a r g )

11 {

12 w h i l e ( ! t h e e n d ) {

13 p r i n t f ( ” J e t ’ a t t e n d !\n ” ) ;

14 s e m w a i t (& my sem ) ;

15 }

16

17 p r i n t f ( ”OK, j e s o r s !\n ” ) ;

18 p t h r e a d e x i t ( 0 ) ;

19 }

20

21 v o i d ∗t h r e a d 2 p r o c e s s (v o i d ∗ a r g )

22 {

23 r e g i s t e r i n t i ;

24

25 f o r ( i = 0 ; i < 5 ; i ++) {

26 p r i n t f ( ” J ’ a r r i v e %d !\n ” , i ) ;

27 s e m p o s t (& my sem ) ;

28 s l e e p ( 1 ) ;

29 }

30

31 t h e e n d = 1 ;

32 s e m p o s t (& my sem ) ; /∗ Pour d e b l o q u e r l e d e r n i e r s e m w a i t ∗/

33 p t h r e a d e x i t ( 0 ) ;

34 }

35

36 main (i n t ac , c h a r ∗ ∗av )

37 {

38 p t h r e a d t th1 , t h 2 ;

39 v o i d ∗r e t ;

40

41 s e m i n i t (& my sem , 0 , 0 ) ;

42

43 i f ( p t h r e a d c r e a t e (& th1 , NULL , t h r e a d 1 p r o c e s s , NULL ) < 0 ) {

44 f p r i n t f ( s t d e r r , ” p t h r e a d c r e a t e e r r o r f o r t h r e a d 1\n ” ) ;

45 e x i t ( 1 ) ;

46 }

47

48 i f ( p t h r e a d c r e a t e (& th2 , NULL , t h r e a d 2 p r o c e s s , NULL ) < 0 ) {

49 f p r i n t f ( s t d e r r , ” p t h r e a d c r e a t e e r r o r f o r t h r e a d 2\n ” ) ;

50 e x i t ( 1 ) ;

51 }

52

53 (v o i d) p t h r e a d j o i n ( th1 , & r e t ) ;

54 (v o i d) p t h r e a d j o i n ( th2 , & r e t ) ;

55 }

Listing 5 – Cr´ eation de thread

3

Références

Documents relatifs

– double nval : contient la valeur du nombre si le lex` eme courant est un nombre ; – String sval : ce champ contient une chaˆıne de caract` eres repr´ esentant ce mot, si....

Quels sont les ´ el´ ements n´ ecessaire pour transferer un fichier entre deux ordinateurs connect´ es sur un r´ eseau.. Afin que le transfert soit valide, il est n´ ecessaire

L’´ equipement Rep est un r´ ept´ eteur qui a pour fonction de recopier des trames Ethernet d’un segment de cˆ able ` a un autre.. Donner l’adresse IP de la machine X et son

Pendant l’ex´ ecution d’un portion de code synchronis´ ee par une thread A , toute autre thread essayant d’ex´ ecuter une portion de code synchronis´ ee sur le mˆ eme objet

– au probl` eme de l’exclusion mutuelle pour l’acc` es ` a des donn´ ees (objets) ou ` a du code (m´ ethodes) ; – au probl` eme de la synchronisation sur des ´ ev´

Reprendre l’exercice du TD qui entraine une cr´eation non contr ˆol´ee de processus pour reproduire son comportement sur le

– notify() permet `a un thread de r´eveiller un des threads qui ´etait en attente dans le wait-set de l’objet r´ecepteur de l’appel de la m´ethode.. Attention, le choix du

Dans un cadre g´en´eral, un arbre binaire est une structure dans laquelle chaque nœud comporte une valeur et deux pointeurs ou r´ef´erences vers deux fils : le fils gauche et le