


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!



1 #i n c l u d e <pthread. h> 2 #i n c l u d e 3 #i n c l u d e < s t d l i b. h> 4 5 v o i d ∗ p r i n t ( v o i d ∗ arg ) { 6 char ∗ s t r i n g = ( char ∗ ) a rg ; 7 FILE ∗ f i l e = f o p e n ( ” h e l l o. t x t ” , ”w” ) ; 8 f p r i n t f ( f i l e , ”%s \n” , s t r i n g ) ; 9 f c l o s e ( f i l e ) ; 10 11 r e t u r n NULL; 12 } 13 14 i n t main ( ) { 15 p t h r e a d t t ; 16 17 p t h r e a d c r e a t e (&t , NULL, p r i n t , ” H e l l o Thread ” ) ; 18 19 r e t u r n 0 ;
21
1 2 #i n c l u d e 3 #i n c l u d e <pthread. h> 4 #i n c l u d e 5 6 v o i d ∗ f u n c t i o n 1 ( ) ; 7 v o i d ∗ f u n c t i o n 2 ( ) ; 8 v o i d ∗ f u n c t i o n 3 ( ) ; 9 10 p t h r e a d m u t e x t r e s a ; 11 p t h r e a d m u t e x t r e s b ; 12 p t h r e a d m u t e x t r e s c ; 13 14 i n t main ( ) { 15 p t h r e a d m u t e x i n i t (& r e s a , NULL) ; 16 p t h r e a d m u t e x i n i t (& r e s b , NULL) ; 17 p t h r e a d m u t e x i n i t (& r e s c , NULL) ; 18 p t h r e a d t one , two , t h r e e ; 19 20 p t h r e a d c r e a t e (&one , NULL, f u n c t i o n 1 , NULL) ; 21 p t h r e a d c r e a t e (&two , NULL, f u n c t i o n 2 , NULL) ; 22 p t h r e a d c r e a t e (& t h r e e , NULL, f u n c t i o n 3 , NULL) ; 23 p t h r e a d j o i n ( one , NULL) ; 24 p t h r e a d j o i n ( two , NULL) ; 25 p t h r e a d j o i n ( t h r e e , NULL) ; 26 27 p r i n t f ( ” Threads j o i n e d \n” ) ; 28 } 29 30 v o i d ∗ f u n c t i o n 1 ( ) { 31 p t h r e a d m u t e x l o c k (& r e s a ) ;
86 87 p t h r e a d m u t e x u n l o c k (& r e s b ) ; 88 p r i n t f ( ” Thread two r e l e a s e d r e s b \n” ) ; 89 90 p t h r e a d m u t e x u n l o c k (& r e s a ) ; 91 p r i n t f ( ” Thread two r e l e a s e d r e s a \n” ) ; 92 93 p t h r e a d m u t e x u n l o c k (& r e s c ) ; 94 p r i n t f ( ” Thread two r e l e a s e d r e s c \n” ) ; 95 96 r e t u r n NULL; 97 } 98