Licence d’informatique 2004-2005 Cours syst`emes d’exploitation, p.1
TD : communication par tubes anonymes
Exercice 1. Que se passe-t’il pendant l’ex´ecution du programme suivant ? 1 # i n c l u d e < s t d l i b . h >
2 # i n c l u d e < s t d i o . h >
3 # i n c l u d e < u n i s t d . h >
4 # i n c l u d e < sys / w a i t . h >
6 int p [ 2 ] ;
8 v o i d f i l s 1 ( v o i d ) 9 {
10 c h a r c ;
12 c l o s e ( p [ 0 ] ) ;
13 p r i n t f (" d ´e b u t f i l s 1 ( t a p e r 0 p o u r f i n i r )\ n ");
14 w h i l e ( ( c = g e t c h a r ( ) ) ! = ’ 0 ’ )
15 if ( ( c >= ’ A ’ & & c <= ’ Z ’ ) | | ( c >= ’ a ’ & & c <= ’ z ’ ) )
16 {
17 if (( c >= ’ a ’ ) & & ( c <= ’ z ’ ) ) c - = 3 2 ; 18 w r i t e ( p [1] ,& c , 1 ) ;
19 p r i n t f (" Le f i l s 1 e n v o i e >% c <\ n " , c );
20 }
21 c l o s e ( p [ 1 ] ) ;
22 e x i t ( E X I T _ S U C C E S S );
23 }
25 v o i d f i l s 2 ( v o i d ) 26 {
27 c h a r c ;
29 c l o s e ( p [ 1 ] ) ;
30 p r i n t f (" d e b u t f i l s 2 \ n ");
31 w h i l e ( r e a d ( p [0] ,& c , 1 ) > 0 )
32 p r i n t f (" f i l s 2 r e ¸c o i t >% c <\ n " , c );
33 c l o s e ( p [ 0 ] ) ;
34 e x i t ( E X I T _ S U C C E S S );
35 }
37 int m a i n ( v o i d ) 38 {
39 if ( p i p e ( p ) ! = 0 )
40 {
41 p r i n t f (" pb o u v e r t u r e p i p e \ n ");
42 e x i t ( E X I T _ F A I L U R E );
43 }
44 if ( f o r k ( ) = = 0 ) f i l s 1 ();
45 if ( f o r k ( ) = = 0 ) f i l s 2 ();
46 c l o s e ( p [ 0 ] ) ; c l o s e ( p [ 1 ] ) ; 47 w a i t ( N U L L ); w a i t ( N U L L );
48 p r i n t f (" fin du p `e r e \ n ");
49 e x i t ( E X I T _ S U C C E S S );
50 }
Licence d’informatique 2004-2005 Cours syst`emes d’exploitation, p.2
Exercice 2. Que ce passe-t’il pendant l’ex´ecution du programmetubonacci.csuivant ? 1 # i n c l u d e < s t d l i b . h >
2 # i n c l u d e < s t d i o . h >
3 # i n c l u d e < u n i s t d . h >
4 # i n c l u d e < w a i t . h >
6 v o i d p a r e n t ( int entree , int s o r t i e ) 7 {
8 u n s i g n e d c h a r n = 0 ;
10 w r i t e ( sortie , & n , 1 ) ; 11 n ++; w r i t e ( sortie , & n , 1 ) ;
12 do {
13 r e a d ( entree , & n , 1 ) ;
14 p r i n t f ("% u " , n ); f f l u s h ( s t d o u t );
15 w r i t e ( sortie , & n , 1 ) ; 16 } w h i l e ( n < = 2 0 0 ) ;
18 p r i n t f ("\ n ");
19 c l o s e ( s o r t i e );
20 w a i t ( N U L L );
21 }
23 v o i d e n f a n t ( int entree , int s o r t i e ) 24 {
25 u n s i g n e d c h a r p , q ; 26 r e a d ( entree , & p , 1 ) ;
28 for ( ; ; ) {
29 w r i t e ( sortie , & p , 1 ) ;
30 if ( r e a d ( entree , & q , 1 ) ! = 1 ) r e t u r n ;
31 p + = q ;
32 }
33 }
35 int m a i n ( v o i d ) 36 {
37 int t u b e _ p e [2] , t u b e _ e p [2] , pid ;
39 p r i n t f ( " [ % d ] : d ´e b u t du p `e r e .\ n " , g e t p i d ( ) ) ;
41 p i p e ( t u b e _ p e ) ; / * t u b e P a r e n t - > E n f a n t */
42 p i p e ( t u b e _ e p ) ; / * t u b e E n f a n t - > P a r e n t */
44 if ( ( pid = f o r k ( ) ) = = - 1 ) e x i t ( E X I T _ F A I L U R E );
45 if ( pid > 0 ) { / * p a r e n t */
46 c l o s e ( t u b e _ p e [ 0 ] ) ; c l o s e ( t u b e _ e p [ 1 ] ) ; 47 p a r e n t ( t u b e _ e p [0] , t u b e _ p e [ 1 ] ) ;
48 }
49 e l s e { / * e n f a n t */
50 c l o s e ( t u b e _ p e [ 1 ] ) ; c l o s e ( t u b e _ e p [ 0 ] ) ; 51 e n f a n t ( t u b e _ p e [0] , t u b e _ e p [ 1 ] ) ;
52 }
54 p r i n t f ( " [ % d ] : t e r m i n a i s o n \ n " , g e t p i d ( ) ) ; 55 e x i t ( E X I T _ S U C C E S S );
56 }
Licence d’informatique 2004-2005 Cours syst`emes d’exploitation, p.3
Exercice 3. Que ce passe-t’il si l’on supprime la ligne 19 detubonacci.c?
Exercice 4.Modifiertubonacci.cpour calculer les termes de la suite d´efinie paru0= 2,u1= 3,un+2= 2un+1+3un. Exercice 5 : chasse aux erreurs. Que devrait faire le programme suivant ? Trouvez au moins deux erreurs et corrigez-les.
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 < u n i s t d . h >
4 # i n c l u d e < s i g n a l . h >
6 s t a t i c int n = 0 ;
8 v o i d e r r e u r ( c o n s t c h a r * e r r m e s s ) 9 {
10 p e r r o r ( e r r m e s s ); e x i t ( E X I T _ F A I L U R E );
11 }
13 v o i d i n t e r r u p t i o n ( int s i g n u m ) 14 {
15 p r i n t f (" No c h o i s i : % d \ n " , n ); e x i t ( E X I T _ S U C C E S S );
16 }
18 v o i d i n i t _ i t e r a t i o n ( int d _ e c r i t u r e ) 19 {
20 w r i t e ( d _ e c r i t u r e ,& n , s i z e o f ( int ));
21 }
23 v o i d i t e r a t i o n ( int d _ l e c t u r e , int d _ e c r i t u r e ) 24 {
25 w h i l e ( 1 ) {
26 r e a d ( d _ l e c t u r e ,& n , s i z e o f ( int ));
27 n ++;
28 w r i t e ( d _ e c r i t u r e ,& n , s i z e o f ( int ));
29 }
30 }
32 int m a i n ( v o i d ) 33 {
34 int pf [2] , fp [ 2 ] ; / * C o m m u n i c a t i o n b i d i r e c t i o n n e l l e */
35 s t r u c t s i g a c t i o n a c t i o n ; 36 a c t i o n . s a _ f l a g s =0;
37 s i g e m p t y s e t (& a c t i o n . s a _ m a s k );
39 if ( p i p e ( pf ) = = - 1 | | p i p e ( fp ) = = - 1 ) e r r e u r (" c r ´e a t i o n d ’ un t u be ");
40 s w i t c h ( f o r k ( ) ) {
41 c a s e -1: e r r e u r (" f o r k ");
42 c a s e 0 : / * F i l s */
43 c l o s e ( fp [ 0 ] ) ; c l o s e ( pf [ 1 ] ) ; 44 i t e r a t i o n ( pf [0] , fp [ 1 ] ) ; 45 d e f a u l t : / * P `e r e */
46 a c t i o n . s a _ h a n d l e r = i n t e r r u p t i o n ; 47 s i g a c t i o n ( SIGINT ,& action , N U L L );
48 c l o s e ( pf [ 0 ] ) ; c l o s e ( fp [ 1 ] ) ; 49 i t e r a t i o n ( fp [0] , pf [ 1 ] ) ;
50 }
51 e x i t ( E X I T _ F A I L U R E );
52 }
Licence d’informatique 2004-2005 Cours syst`emes d’exploitation, p.4
Exercice 6 : tri distribu´e. Ecrire un programme o`´ u un processus p`ere cr´ee deux fils ; chaque fils attend un couple d’entiers (x, y) du p`ere et le renvoie dans l’ordre croissant.
Le p`ere demande quatre entiers a, b, c, d `a l’utilisateur et ordonne les couples (a0, b0) et (c0, d0). Puis il ordonne les couples (a0, c0) et (b0, d0), il obtient les couples (a00, c00) et (b00, d00). Finalement, il ordonne le couple (c00, b00) il obtient le couple (c000, b000) et affiche (a00, c000, b000, d00).