Lab 10 Threads PDF
Lab 10 Threads PDF
Threads
To suggest more improvements and corrections please feel free to email to [email protected]. All rights
are reserved by the author. Manufactured in Pakistan. Except as permitted under the Pakistan States Copyright
Act of 1971, no part of this report may be reproduced or distributed in any form or by any means, or stored in a
database or retrieval system, without the prior written consent of the author ([email protected]) including,
but not limited to, network or other electronic storage or transmission, or broadcast for distance learning.
7 JOINING THREADS
2. The pthread_join() subroutine blocks the calling thread until the specified threa-
did thread terminates.
3. The programmer is able to obtain the target thread’s termination return status if
it was specified in the target thread’s call to pthread_exit().
4. A joining thread can match one pthread_join() call. It is a logical error to attempt
multiple joins on the same thread.
EXAMPLE 02
# include < s t d i o . h>
# include < s t d l i b . h>
# include <pthread . h>
i n t main ( void ) {
pthread_t tid1, tid2;
p t h r e a d _ c r e a t e (& t i d 1 , NULL, t h r _ f n 1 , NULL ) ;
p t h r e a d _ c r e a t e (& t i d 2 , NULL, t h r _ f n 2 , NULL ) ;
void *t r e t 1 ;
void *t r e t 2 ;
why p a s s i n g a d r e s s o f p o i n t e r t r e t 1 ?
p t h r e a d _ j o i n ( t i d 1 , &t r e t 1 ) ;
p t h r e a d _ j o i n ( t i d 2 , &t r e t 2 ) ;
c a s t i n g t r e t 1 t o c h a r*
p r i n t f ( " Thread 1 e x i t code i s : %s\n " , ( char* ) t r e t 1 ) ;
p r i n t f ( " Thread 2 e x i t code i s : %l d \n " , ( long ) t r e t 2 ) ;
exit(0);
}
4
8 PASSING ARGUMENTS TO THREADS
The pthread_create() routine permits the programmer to pass one argument to the
thread start routine.
For cases where multiple arguments must be passed, this limitation is easily overcome
by creating a structure which contains all of the arguments, and then passing a pointer
to that structure in the pthread_create() routine.
EXAMPLE 03
# include < s t d i o . h>
# include < s t d l i b . h>
# include <pthread . h>
/* t h i s f u n c t i o n i s run by t h e s e c o n d t h r e a d /
void *i n c _ x ( void *arg ) {
/* i n c r e m e n t x t o
p r i n t f ( " Thread : P r o c e s s Id i s %d and Thread Id i s %l d \n " , g e t p i d ( )
,pthread_self());
i n p u t =( i n t * ) arg ;
while ( + + (* i n p u t ) < 1 0 0 ) ;
p r i n t f ( " x i n cr e m e n t f i n i s h e d \n " ) ;
/* t h e f u n c t i o n must r e t u r n s o m e t h i n g NULL w i l l
p t h r e a d _ e x i t (NULL ) ;
}
void main ( ) { / / main T h r e a d ;
int x= 0, y= 0;
5
Operating Systems Lab Spring, 2019 KFUEIT, RYK
/* show t h e i n i t i a l v a l u e s o f x and y * /
p r i n t f ( " x : %d , y : %d\n " , x , y ) ;
p r i n t f ( " main : P r o c e s s Id i s %d and Thread Id
i s %l d \n " , g e t p i d ( )
,pthread_self());
/* this variable is our reference to the second thread*/
pthread_t thread_2;
/ * c r e a t e a s e c o n d t h r e a d w h i c h e x e c u t e s i n c _ x (&x ) */
( p t h r e a d _ c r e a t e (& thread_2 , NULL, i n c _ x , &x ) ) ;
/ i n c r e m e n t y t o 100 i n t h e f i r s t t h r e a d /
while (++y < 100);
p r i n t f ( " y i n cr e m e n t f i n i s h e d \n " ) ;
/*wait for the second thread to finish*/
( p t h r e a d _ j o i n ( thread_2 , NULL ) ) ;
p t h r e a d _ e x i t (NULL ) ;
EXAMPLE 04
# include <pthread . h>
# include < s t d i o . h>
# include < s t d l i b . h>
struct student_database{
i nt rollno;
f l o a t cgpa ;
};
void *p r i n t _ f n ( void *t h r e a d a r g ) {
s t r u c t s t u d e n t _ d a t a b a s e *temp_data ;
temp_data = ( s t r u c t s t u d e n t _ d a t a b a s e* ) t h r e a d a r g ;
i n t r o l l n o = temp_data ->r o l l n o ;
f l o a t cgpa = temp_data ->cgpa ;
p r i n t f ( " I n s i d e Thread\n " ) ;
p r i n t f ( " R o l l no %d has cgpa :% f \n " , r o l l n o , cgpa ) ;
p t h r e a d _ e x i t (NULL ) ;
}
6
Operating Systems Lab Spring, 2019 KFUEIT, RYK
9 EXAMPLES
EXAMPLE 05
# include <pthread . h>
# include < s t d i o . h>
# include < s t d l i b . h>
void *t h r _ f n 1 ( void * t h r e a d i d ) {
sleep(1);
p r i n t f ( " t h r e a d 1 : #%l d ! and Iam not w a i t i n g \n " , p t h r e a d _ s e l f ( ) ) ;
int i=0;
f o r ( i ; i < 5 ; i + + ) { p r i n t f ( " v a lu e o f i i s %d\n " , i ) ; }
p t h r e a d _ e x i t (NULL ) ;
}
void *t h r _ f n 2 ( void * t h r e a d i d ) {
p r i n t f ( " t h r e a d 2 : #%l d ! w a i t i n g f o r t h r e a d 1\n " , p t h r e a d _ s e l f ( ) ) ;
p t h r e a d _ j o i n ( t h r e a d _ i d 1 ,NULL ) ;
p r i n t f ( " t h r e a d 2 : t h r e a d 1 e x i t e d and now me e x i t i n g \n " ) ;
p t h r e a d _ e x i t (NULL ) ;
}
EXAMPLE 06
# include <pthread . h>
# include < s t d i o . h>
# define NUM_THREADS 5
void *P r i n t H e l l o ( void *t h r e a d i d ) {
long *t i d ;
t i d = ( long * ) t h r e a d i d ;
p r i n t f ( " I t ’ s me , t h r e a d #%l d \n " ,
p t h r e a d _ e x i t (NULL ) ;
}
7
Operating Systems Lab Spring, 2019 KFUEIT, RYK
/ * L a s t t h i n g t h a t main ( ) s h o u l d do * /
p t h r e a d _ e x i t (NULL ) ;
}
EXAMPLE 07
# include <pthread . h>
# include < s t d i o . h>
# include < s t d l i b . h>
char *message ;
struct thread_data
{
pthread_t*thread
_id;
char *message ;
};
struct thread_data thread_data;
void *t h r _ f n ( void *t h r e a d a r g ) {
s t r u c t t h r e a d _ d a t a *my_data ;
my_data = ( s t r u c t t h r e a d _ d a t a ) t h r e a d a r g ;
p t h r e a d _ t *t a s k i d = my_data ->t h r e a d _ i d ;
char *hello_msg= my_data ->message ;
p r i n t f ( " Thread i d i s : %l d \nMessage : %s " , hello_msg ) ;
p t h r e a d _ e x i t ( ( void *) " S u c c e s s f u l l " ) ;