








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









t 6 : = 2 ; t 8 : = 3 ; S1 ; f o r k p2 ; f o r k p5 ; f o r k p7 ; q u i t ; p2 : S2 ; f o r k p3 ; f o r k p4 ; q u i t ; p5 : S5 ; j o i n t 6 , p6 ; q u i t ; p7 : S7 ; j o i n t 8 , p8 ; q u i t ; p3 : S3 ; j o i n t 8 , p8 ; q u i t ; p4 : S4 ; j o i n t 6 , p6 ; q u i t ; p6 : S6 ; j o i n t 8 , p8 ; q u i t ; p8 : S8 ; q u i t
p a r b e g i n p1 : a : = b + 1 ; p2 : c : = d + 1 ; parend p3 : e : = a + c ;
1 s ha re d i n t t u r n ; 2 l o o p { 3 w h i l e ( t u r n != i ) / ∗ do n o t h i n g ∗ / ; 4 / ∗ c r i t i c a l s e c t i o n h e r e ∗ / 5 t u r n = j ; 6 / ∗ r e m a i n d e r s e c t i o n h e r e ∗ / 7 }
1 s ha re d b o o l inCS [ 0.. 1 ] = { f a l s e , f a l s e } ; 2 l o o p { 3 w h i l e ( inCS [ j ] ) / ∗ do n o t h i n g ∗ / ; 4 inCS [ i ] = t r u e ; 5 / ∗ c r i t i c a l s e c t i o n h e r e ∗ / 6 inCS [ i ] = f a l s e ; 7 / ∗ r e m a i n d e r s e c t i o n h e r e ∗ / 8 }
1 s ha re d b o o l i n t e r e s t e d [ 0.. 1 ] = { f a l s e , f a l s e } ; 2 l o o p { 3 i n t e r e s t e d [ i ] = t r u e ; 4 w h i l e ( i n t e r e s t e d [ j ] ) / ∗ do n o t h i n g ∗ / ; 5 / ∗ c r i t i c a l s e c t i o n h e r e ∗ / 6 i n t e r e s t e d [ i ] = f a l s e ; 7 / ∗ r e m a i n d e r s e c t i o n h e r e ∗ / 8 }
1 s ha re d b o o l i n t e r e s t e d [ 0.. 1 ] ; = { f a l s e , f a l s e } ; 2 s ha re d i n t t u r n ; 3 l o o p { 4 i n t e r e s t e d [ i ] = t r u e ; 5 t u r n = j ; 6 w h i l e ( i n t e r e s t e d [ j ] && t u r n == j ) / ∗ do n o t h i n g ∗ / ; 7 / ∗ c r i t i c a l s e c t i o n h e r e ∗ / 8 i n t e r e s t e d [ i ] = f a l s e ; 9 / ∗ r e m a i n d e r s e c t i o n h e r e ∗ / 10 }
b o o l TaS ( b o o l ∗ Lock ) { b o o l tmp = ∗ Lock ; ∗ Lock = t r u e ; r e t u r n ( tmp ) ; }
1 s ha re d b o o l w a i t i n g [ 0.. n −1] ; 2 s ha re d b o o l Lock ; 3 i n t ( 0.. n −1) j ; 4 b o o l key ; 5 l o o p { / ∗ p r o c e s s P i ∗ / 6 w a i t i n g [ i ] = t r u e ; 7 key = t r u e ; 8 w h i l e w a i t i n g [ i ] and key 9 key = TaS ( Lock ) ; 10 w a i t i n g [ i ] = f a l s e ; 11 / ∗ c r i t i c a l s e c t i o n h e r e ∗ / 12 j = i + 1 mod n ; 13 w h i l e j != i and n ot w a i t i n g [ j ] 14 j = ( j + 1 ) mod n ; 15 i f ( j == i ) 16 Lock = f a l s e ; 17 e l s e 18 w a i t i n g [ j ] = f a l s e ; 19 / ∗ r e m a i n d e r s e c t i o n h e r e ∗ / 20 }
1 semaphore f u l l = 0 , empty = n , mutex = 1 ; 2 i t e m n e x t p , n e x t c ; 3 s ha re d i t e m b u f f e r [ 0.. n −1] ; 4 5 p a r b e g i n 6 l o o p { / ∗ p r o d u c e r p r o c e s s ∗ / 7 / ∗ p r o d u c e an i t e m i n n e x t p ∗ / 8 down ( empty ) ; 9 down ( mutex ) ; 10 / ∗ d e p o s i t n e x t p i n b u f f e r ∗ / 11 up ( mutex ) ; 12 up ( f u l l ) ; 13 } 14 l o o p { / ∗ consumer p r o c e s s ∗ / 15 down ( f u l l ) ; 16 down ( mutex ) ; 17 / ∗ e x t r a c t an i t e m i n n e x t c ∗ / 18 up ( mutex ) ; 19 up ( empty ) ; 20 / ∗ consume t h e i t e m i n n e x t c ∗ / 21 } 22 parend
1 monitor b u f f e r { 2 i t e m s l o t s [ 0.. n −1] ; 3 i n t c o u n t , i n , o u t ; 4 c o n d i t i o n n o t empty , n o t f u l l ; 5 6 e n t r y d e p o s i t ( it em d a t a ) { 7 i f ( c o u n t == n ) 8 n o t f u l l. w a i t ; 9 s l o t s [ i n ] = d a t a ; 10 i n = ( i n + 1 ) % n ; 11 c o u n t ++; 12 n o t e m p t y. s i g n a l ; 13 } 14 15 e n t r y e x t r a c t ( it em ∗ d a t a ) { 16 i f ( c o u n t == 0 ) 17 n o t e m p t y. w a i t ; 18 ∗ d a t a = s l o t s [ o u t ] ; 19 o u t = ( o u t + 1 ) % n ; 20 c o u n t −−; 21 n o t f u l l. s i g n a l ; 22 } 23 24 c o u n t = i n = o u t = 0 ; 25 }
1 monitor r e a d e r w r i t e r { 2 i n t r e a d c o u n t ; 3 b o o l w r i t i n g ; 4 c o n d i t i o n o k t o r e a d , o k t o w r i t e ; 5 6 e n t r y b e g i n r e a d ( v o i d ) { 7 r e a d c o u n t ++; 8 i f ( w r i t i n g ) 9 o k t o r e a d. w a i t ; 10 } 11 12 e n t r y e n d r e a d ( v o i d ) { 13 r e a d c o u n t −−; 14 i f ( r e a d c o u n t == 0 ) 15 o k t o w r i t e. s i g n a l ; 16 } 17 18 e n t r y b e g i n w r i t e ( v o i d ) { 19 i f ( ( r e a d c o u n t > 0 ) | | w r i t i n g ) 20 o k t o w r i t e. w a i t ; 21 w r i t i n g = t r u e ; 22 } 23 24 e n t r y e n d w r i t e ( v o i d ) { 25 i n t i ; 26 27 w r i t i n g = f a l s e ; 28 i f ( r e a d c o u n t > 0 ) { 29 f o r ( i = 1 ; i <= r e a d c o u n t ; i ++) 30 o k t o r e a d. s i g n a l ; 31 } 32 e l s e 33 o k t o w r i t e. s i g n a l ; 34 } 35 36 r e a d c o u n t = 0 ; 37 w r i t i n g = f a l s e ; 38 }
1 monitor a l a r m c l o c k { 2 i n t now ; 3 c o n d i t i o n wakeup ; 4 5 e n t r y wakeme ( i n t n ) { 6 a l a r m s e t t i n g = now + n ; 7 w h i l e ( now < a l a r m s e t t i n g ) 8 wakeup. w a i t ( a l a r m s e t t i n g ) ; 9 wakeup. s i g n a l ; 10 } 11 12 e n t r y t i c k ( v o i d ) { 13 now ++; 14 wakeup. s i g n a l ; 15 } 16 }
1 v o i d p r o d u c e r ( v o i d ) { 2 i t e m n e x t p ; 3 4 w h i l e ( 1 ) { 5 / ∗ p r o d u c e i t e m i n n e x t p ∗ / 6 s e n d ( ” C o n s u m e r p r o c e s s ” , n e x t p ) ; 7 } 8 } 9 10 v o i d c ons ume r ( v o i d ) { 11 i t e m n e x t c ; 12 13 w h i l e ( 1 ) { 14 r e c e i v e ( ” P r o d u c e r p r o c e s s ” , n e x t c ) ; 15 / ∗ consume i t e m i n n e x t c ∗ / 16 } 17 } 18 19 p r o d u c e r P r o d u c e r p r o c e s s ; 20 c ons ume r C o n s u m e r p r o c e s s ; 21 22 p a r b e g i n 23 P r o d u c e r p r o c e s s ; 24 C o n s u m e r p r o c e s s ; 25 parend