• Aucun résultat trouvé

ipc

N/A
N/A
Protected

Academic year: 2022

Partager "ipc"

Copied!
7
0
0

Texte intégral

(1)

communication interprocessus

Novembre 2008

On examine deux autres moyens de communication interprocessus offerts par les syst`emes autres que lestubes : file de message, m´emoire partag´es. Un dernier moyen tr´es puissant concernant les communications processus distants (socket ) sera vu dans le cours de programmation r´eseau.

1 File de message

L’ex´ecution des commandes de la cible demo1 est illustr´ee par la figure (1).

Trois messages sont envoy´es dans une file de messages identifi´ees par le nom de l’ex´ecutable, puis lus avec des priorit´es diff´erentes. La commande ipcs est utilis´ee pour tracer l’´evolution de la file de messages.

(a) Expliquer le rˆole des appels syst`emes.

(b) Commenter la gestion dutype des messages.

1 #include <s t d i o . h>

2 #include <s t d l i b . h>

3 #include <s y s / t y p e s . h>

4 #include <s y s / i p c . h>

5 #include <s y s /msg . h>

6 #include <s t r i n g . h>

7 #include <u n i s t d . h>

8 #define MAX 256

9 i n t CTRL=0 , SEND=0 , RECV = 0 ; 10

11 typedef unsigned short i n t u s h o r t ; 12

13 typedef s t r u c t { 14 long t y p e ; 15 char d a t a [MAX] ; 16 } m es s ag e ;

17

18 // mark=main

19 // kwds=f t o k , msgget , msgsnd , msgrecv , m e s g c t l 20 // c a p t=Envoyer un m es s ag e

21 i n t main (i n t a r g c , char∗ a r g v [ ] )

(2)

22 { i n t opt , t y p e ;

23 char ∗o p t l i s t =" cd : r : s : "; 24 k e y t key ;

25 m es s age msg ; 26 i n t f i l e ;

27 while ( ( o p t = g e t o p t ( a r g c , a r g v , o p t l i s t ) ) > 0 )

28 switch( o p t ){

29 case ’r ’: RECV = 1 ; t y p e = a t o i ( o p t a r g ) ; break; 30 case ’s ’: SEND = 1 ; msg . t y p e = a t o i ( o p t a r g ) ; break;

31 case ’c ’: CTRL = 1 ; break;

32 case ’d ’: s t r n c p y ( msg . data , o p t a r g , MAX ) ; ; break; 33 d e f a u l t : p r i n t f (" \ nusage % s : % s ", a r g v [ 0 ] , o p t l i s t ) ;

34 e x i t ( 1 ) ;

35 }

36

37 i f ( ( key = f t o k( a r g v [ 0 ] , 0 ) ) == −1 ) 38 p e r r o r (" ftok ") , e x i t ( 1 ) ;

39

40 i f ( ( f i l e = msgget( key , IPC CREAT | 0600 ) ) == −1 ) 41 p e r r o r (" msget ") , e x i t ( 1 ) ;

42

43 i f ( SEND ) {

44 i f ( msgsnd( f i l e , ( void∗) & msg , MAX , 0 ) < 0 ) 45 p e r r o r (" msgsnd ") , e x i t ( 1 ) ;

46 }

47 i f ( RECV ) {

48 i f ( msgrcv ( f i l e , ( void∗) & msg , MAX , type , 0 ) >= 0 ) 49 p r i n t f (" type =% ld data =% s ", msg . type , msg . d a t a ) ; 50 e l s e p e r r o r (" msgrecv ") , e x i t ( 1 ) ;

51 }

52

53 i f ( CTRL )

54 m s g c t l( f i l e , IPC RMID , NULL ) ; 55 p r i n t f (" \ n ") ;

56 return 0 ; 57 }

2 M´ emoire partag´ ee

L’ex´ecution des commandes de la cibledemo2est illustr´ee par la figure (2). Une zone de 26 octets est partag´ee par deux processus. La commande ipcs est utilis´ee pour tracer la m´emoire partag´ee.

(a) Expliquer le rˆole des appels syst`emes.

(b) Commenter l’ex´ecution.

(c) L’ensemble est-t-il correct ?

(3)

1 #include <s t d i o . h>

2 #include <s t d l i b . h>

3 #include <s y s / t y p e s . h>

4 #include <s y s / i p c . h>

5 #include <s y s /shm . h>

6 #include <l i m i t s . h>

7 #include <u n i s t d . h>

8 #define MAX 26

9 void p t a b l e ( i n t s , char ∗ t ) 10 { i n t i ;

11 i f ( s ) return; 12 p r i n t f (" \ n % d : ", s ) ; 13 f o r( i = 0 ; i < MAX; i ++) 14 p r i n t f (" % c ", t [ i ] ) ; 15 }

16 void i t a b l e ( char ∗ t ) 17 { i n t i ;

18 f o r( i = 0 ; i < MAX; i ++) 19 t [ MAX− i − 1 ] = 65 + i ; 20 }

21 i n t t e s t ( char ∗t ) 22 { i n t i ;

23 f o r( i = 0 ; i <MAX− 1 ; i ++) 24 i f ( t [ i ] >= t [ i +1] ) return 1 ; 25 return 0 ;

26 }

27 void maps ( void ) 28 {

29 char b f r [ 2 5 6 ] ;

30 s p r i n t f ( b f r , " cat / proc /% d / maps | grep rw - s ", g e t p i d ( ) ) ; 31 s y s t e m ( b f r ) ;

32 }

33 // mark=main

34 // kwds=f t o k , shmget , shmat , shmdt 35 i n t main (i n t a r g c , char ∗a r g v [ ] ) 36 {

37 k e y t key ;

38 i n t shm ;

39 char ∗ t = NULL ;

40 i n t i , s = 0 , tmp , ch ange = 1 , c p t ; 41

42 i f ( ( key = f t o k( a r g v [ 0 ] , 0 ) ) == −1 ) 43 p e r r o r (" ftok ") , e x i t ( 1 ) ;

44

45 i f ( ( shm =shmget( key , MAX, IPC CREAT | 0 6 0 0 ) ) < 0 ) 46 p e r r o r (" shmget ") , e x i t ( 1 ) ;

47

48 i f ( ( t = shmat( shm , NULL, 0 ) ) == NULL ) 49 p e r r o r (" shmat ") , e x i t ( 1 ) ;

50

(4)

51 s = ∗( a r g v [ 1 ] ) − ’0 ’; 52 i f ( s ) i t a b l e ( t ) ; 53 e l s e s y s t e m (" ./ shm . exe 1 & ") ; 54

55 maps ( ) ; 56

57 c p t = 1 << 2 5 ; 58

59 while ( cpt−− ) {

60 i f ( change ) p t a b l e ( s , t ) ;

61 change = 0 ;

62 f o r( i = s ; i + 1 <MAX; i +=2) 63 i f ( t [ i ] > t [ i +1] ) {

64 tmp = t [ i ] ;

65 t [ i ] = t [ i + 1 ] ;

66 t [ i +1] = tmp ;

67 change = 1 ;

68 }

69 }

70 p u t c h a r (’\ n ’) ; 71 return 0 ; 72 }

(a) (b)

3 Makefile

c i b l e = msg . e x e shm . e x e a l l : $ ( c i b l e ) i p c . p d f

$ ( c i b l e ) : % . e x e : % . c

g c c −Wall −g $<−o $@

m k l i s t i n g s : m k l i s t i n g s . l e x f l e x m k l i s t i n g s . l e x

g c c −Wall l e x . yy . c −o m k l i s t i n g s −l f l i p c . p d f : l i s t i n g s . t e x i p c . t e x

p d f l a t e x i p c . t e x p d f l a t e x i p c . t e x l i s t i n g s . t e x : $ ( c i b l e )

. / m k l i s t i n g s msg . c > l i s t i n g s . t e x . / m k l i s t i n g s shm . c >> l i s t i n g s . t e x

(5)

demo1 :

. / msg . e x e −s 1 −d h e l l o 1 . / msg . e x e −s 2 −d h e l l o 2 . / msg . e x e −s 3 −d h e l l o 3 . / msg . e x e −s 4 −d h e l l o 4 i p c s −q

. / msg . e x e −r 2 . / msg . e x e −r−3 . / msg . e x e −r 0 i p c s −q . / msg . e x e −c i p c s −q demo2 :

. / shm . e x e 0 i p c s −m | head −3 i p c s −m | g r e p 26 o u t p u t :

make demo1 | g r e p −v make > msg . o u t make demo2 | g r e p −v make > shm . o u t

(6)

. / msg . e x e −s 1 −d h e l l o 1 . / msg . e x e −s 2 −d h e l l o 2 . / msg . e x e −s 3 −d h e l l o 3 . / msg . e x e −s 4 −d h e l l o 4 i p c s −q

−−−−−− Queues de m e s s a g e s −−−−−−−−

c l msqid p r o p r i t a i r e perms o c t e t s−u t i l i s s m e s s a g e s

0 x 0 0 0 0 f 5 9 8 557056 l a n g e v i n 600 1024 4 . / msg . e x e −r 2

t y p e=2 d a t a=h e l l o 2 . / msg . e x e −r−3 t y p e=1 d a t a=h e l l o 1 . / msg . e x e −r 0 t y p e=3 d a t a=h e l l o 3 i p c s −q

−−−−−− Queues de m e s s a g e s −−−−−−−−

c l msqid p r o p r i t a i r e perms o c t e t s−u t i l i s s m e s s a g e s

0 x 0 0 0 0 f 5 9 8 557056 l a n g e v i n 600 256 1 . / msg . e x e −c

i p c s −q

−−−−−− Queues de m e s s a g e s −−−−−−−−

c l msqid p r o p r i t a i r e perms o c t e t s−u t i l i s s m e s s a g e s

Figure 1: msg.out

(7)

. / shm . e x e 0

b 7 f f a 0 0 0−b 7 f f b 0 0 0 rw−s 0 0 0 0 0 0 0 0 0 0 : 0 8 2 6 5 4 2 3 3 / SYSV0000f558 ( d e l e t e d )

b 7 f 8 f 0 0 0−b 7 f 9 0 0 0 0 rw−s 0 0 0 0 0 0 0 0 0 0 : 0 8 2 6 5 4 2 3 3 / SYSV0000f558 ( d e l e t e d )

0 : Z X Y V W T U R S P Q N O L M J K H I F G D E B C A 0 : X Z V Y T W R U P S N Q L O J M H K F I D G B E A C 0 : X V Z T Y R W P U N S L Q J O H M F D K B I A G C E 0 : V X T Z R Y P W N U L S J Q H O F M D K B I A G C E 0 : V T X R Z P Y N W L U J H S F Q D O B M A K C I E G 0 : T V R X P Z N Y L W J U H S F Q D O B M A K C I E G 0 : T R V P X N Z L Y J W H U F S D Q B A O C M E K G I 0 : R T P V N X L Z J Y H W F U D S B Q A O C M E K G I 0 : R P T N L V J X H Z F Y D W B U A S C Q E O G M I K 0 : P R N T L V J X H Z F Y D W B U A S C Q E O G M I K 0 : P N R L T J H V F X D Z B Y A W C U E S G Q I O K M 0 : N P L R J T H V F X D Z B Y A W C U E S G Q I O K M 0 : N L P J R H T F V D X B Z A C Y E W G U I S K Q M O 0 : L N J P H R F T D V B X A Z C Y E W G U I S K Q M O 0 : L J N H P F R D B T A V C X E Z G Y I W K U M S O Q 0 : J L H N F P D R B T A V C X E Z G Y I W K U M S O Q 0 : J H L F N D P B R A T C E V G X I Z K Y M W O U Q S 0 : H J F L D N B P A R C T E V G X I Z K Y M W O U Q S 0 : F H D J B L A N C P E R G T I V K X M Z O Y Q W S U 0 : D F B H A J C L E N G P I R K T M V O X Q Z S Y U W 0 : D B A F C H E J G L I N K P M R O T Q V S X U Z W Y 0 : B D A F C H E J G L I N K P M R O T Q V S X U Z W Y 0 : B A D C F E H G J I L K N M P O Q R S T U V W X Y Z 0 : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

i p c s −m | head −3

−−−−−− Segment de m m o i r e p a r t a g −−−−−−−−

c l shmid p r o p r i t a i r e perms o c t e t s n a t t c h t a t s

i p c s −m | g r e p 26

0 x 0 0 0 0 0 0 0 0 262144 l a n g e v i n 600 393216

2 d e s t

0 x 0 0 0 0 0 0 0 0 622607 l a n g e v i n 600 393216

2 d e s t

0 x 0 0 0 0 0 0 0 0 2 3 2 6 5 4 6 l a n g e v i n 600 393216

2 d e s t

0 x 0 0 0 0 f 5 9 c 2 5 8 8 6 9 2 l a n g e v i n 600 26 0 0 x 0 0 0 0 f 5 5 6 2 6 2 1 4 6 2 l a n g e v i n 600 26 0 0 x 0 0 0 0 f 5 5 8 2 6 5 4 2 3 3 l a n g e v i n 600 26 0

Figure 2: shm.out

Références

Documents relatifs

Dans le cas du système Unix/Linux, un processus utilisateur peut également envoyer un signal à un autre processus.. Les deux processus appartiennent au même propriétaire, ou

Les tubes sans nom sont, en général, utilisés pour la communication entre un processus père et ses processus fils, avec un d'entre eux qui écrit sur le tube, appelé processus

Pour r´ esoudre un syst` eme, on va faire des combinaisons lin´ eaires d’´ equations, et produire ainsi des ´ equations plus simples... Equations horizontales

[r]

On a donc trouv´ e une condition suffisante pour qu’un syst` eme de trois ´ equations lin´ eaires ` a deux inconnues, dont les deux premi` eres ont une solution commune, ait

Pour r´ esoudre un syst` eme, on va faire des combinaisons lin´ eaires d’´ equations, et produire ainsi des ´ equations plus simples... Equations horizontales

[r]

On a donc trouv´ e une condition suffisante pour qu’un syst` eme de trois ´ equations lin´ eaires ` a deux inconnues, dont les deux premi` eres ont une solution commune, ait