Slideshare.net (beta)

 
Post: 
Myspace Hi5 Friendster Xanga LiveJournal Facebook Blogger Tagged Typepad Freewebs BlackPlanet gigya icons



All comments

Add a comment on Slide 1

If you have a SlideShare account, login to comment; else you can comment as a guest


Showing 1-50 of 0 (more)

Tutorial4 Threads

From tech2click, 8 months ago

781 views  |  0 comments  |  0 favorites  |  228 downloads
 

Tags

No tags to display

 
 

Groups/Events

Not added to any group/event

 
 

Privacy InfoNew!

This slideshow is Public

 
Embed in your blog
Embed (wordpress.com)
custom

Slideshow Statistics
Total Views: 781
on Slideshare: 781
from embeds: 0* * Views from embeds since 21 Aug, 07

Slideshow transcript

Slide 1: Th re a ds

Slide 2: P roc e s s • P roc e s s e s c onta in informa tion a bout prog ra m re s ourc e s a nd prog ra m e xe c ution s ta te , inc luding : – P roc e s s ID, proc e s s g roup ID, us e r ID, a nd g roup ID – E nvironme nt – Working dire c tory. – P rog ra m ins truc tions – R e g is te rs – S ta c k – He a p – F ile de s c riptors – S ig na l a c tions – S h a re d libra rie s – Inte r-proc e s s c ommunic a tion tools (s uc h a s me s s a g e que ue s , pipe s , s e ma ph ore s , or s h a re d me mory).

Slide 3: UNIX P roc e s s

Slide 4: Th re a ds • Th re a ds u s e a n d e xis t with in th e s e p roc e s s re s o urc e s , ye t a re a b le to b e s c h e d u le d b y th e op e ra tin g s ys te m a n d ru n a s in d e p e n d e n t e n titie s la rg e ly b e c a u s e th e y d u p lic a te o n ly th e b a re e s s e n tia l re s o u rc e s th a t e na b le th e m to e xis t a s e xe c u ta b le c od e .

Slide 5: C ont. • Th is in d e p e nd e n t flo w o f c o n trol is a c c o m p lis h e d b e c a u s e a th re a d m a in ta in s its o wn : – S ta c k p ointe r – R e g is te rs – S c h e duling pro pe rtie s (s uc h a s polic y or priority) – S e t of pe nding a nd b lo c ke d s ig na ls – Th re a d s pe c ific da ta (th re a d ID)

Slide 6: UNIX Th re a d

Slide 7: UNIX Th re a ds • In th e UNIX e nvironm e nt a th re a d: – E xis ts with in a proc e s s a nd us e s th e proc e s s re s ourc e s – Ha s its own inde pe nde nt flow of c ontrol a s long a s its pa re nt proc e s s e xis ts a nd th e OS s upports it – Duplic a te s only th e e s s e ntia l re s ourc e s it ne e ds to be inde pe nde ntly s c h e dula ble – Ma y s h a re th e proc e s s re s ourc e s with oth e r th re a ds th a t a c t e qua lly inde pe nde ntly (a nd de pe nde ntly) – Die s if th e pa re nt proc e s s die s - or s ome th ing s im ila r – Is "lig h twe ig h t" be c a us e m os t of th e ove rh e a d h a s a lre a dy be e n a c c omplis h e d th roug h th e c re a tion of

Slide 8: P th re a ds • A th re a d lib ra ry th a t p ro vid e s th e p rog ra m m e r a n AP I fo r c re a tin g a n d m a n a g in g th re a d s • P th re a d s re fe rs to th e P OS IX s ta n d a rds d e fin ing a n AP I fo r th re a d c re a tio n a n d s ync h ron iz a tio n

Slide 9: P th re a ds • P th re a d s a re d e fin e d a s a s e t o f C la ng u a g e p ro g ra m m in g type s a nd p roc e d ure c a lls , im ple m e n te d with a p th re a d.h h e a d e r/ c lud e file a n d a th re a d in lib ra ry

Slide 10: C re a ting th re a ds • Initia lly, yo u r m a in() p ro g ra m c om p ris e s a s in g le , d e fa u lt th re a d. All o th e r th re a d s m u s t b e e xp lic itly c re a te d b y th e p rog ra m m e r.

Slide 11: C re a ting th re a ds (c ont.) in t pthre ad_c re ate (p th re a d_t *thread, c o n s t p th re a d_a ttr_t *attr, vo id *(*start_routine) (vo id *), vo id *arg); De s c riptio n: • pthre ad_c re ate () c re a te s a n e w th re a d a n d m a ke s it e xe c u ta b le . Th is ro u tin e c a n b e c a lle d a n y n u m b e r o f tim e s from a n ywh e re with in yo u r c o d e .

Slide 12: C re a ting th re a ds (c ont.) • pthre ad_c re ate () a rg um e nts : – thread: An opa que , unique ide ntifie r for th e ne w th re a d re turne d by th e s ubroutine . – attr: An opa que a ttribute obje c t th a t m a y be us e d to s e t th re a d a ttribute s . You c a n s pe c ify a th re a d a ttribute s obje c t, or NULL for th e de fa ult va lue s . – s tart_routine : th e C routine th a t th e th re a d will e xe c ute onc e it is c re a te d. – arg: A s ing le a rg ume nt th a t ma y be pa s s e d to start_routine. It mus t be pa s s e d by re fe re nc e a s a pointe r c a s t of type void. NULL ma y be us e d if no a rg ume nt is to be pa s s e d.

Slide 13: C ont. • R e tu rn va lue : – If s uc c e s s ful, th e pthre ad_c re ate () routine s h a ll re turn z e ro – oth e r- wis e , a n e rror num b e r s h a ll b e re tu rne d to indic a te th e e rror.

Slide 14: Th re a d a ttrib ute s • Th re a d a ttrib ute s inc lude (de fine m e m b e rs of th e s truc t pth re a d_a ttr_t): – de ta c h e d s ta te – s c h e duling polic y – s c h e duling pa ra me te r – inh e rits c h e d a ttribute – S c ope – g ua rd s iz e – s ta c k a ddre s s – s ta c k s iz e

Slide 15: S c h e duling th re a ds • Afte r a thre ad has be e n c re ate d, ho w do yo u kno w whe n it will be s c he dule d to run by the o pe rating s ys te m? – It is up to th e im ple m e nta tion a nd/ ope ra ting or s ys te m to de c ide wh e re a nd wh e n th re a ds will e xe c ute .

Slide 16: Te rm ina ting th re a ds • pthre ad_e xit() is us e d to e xp lic itly e xit a th re a d . • If m a in () fin is h e s b e fo re th e th re a d s it h a s c re a te d, a n d e xits with pthre ad_e xit(), th e o th e r th re a d s will c on tin ue to e xe c ute . Oth e rwis e , th e y will b e a u to m a tic a lly te rm in a te d wh e n m a in () fin is h e s .

Slide 17: J oining Th re a ds int pthre ad_jo in(pth re a d_t th re a d, void **va lue _ptr); De s c riptio n: • Th e pthre ad_jo in() func tion s h a ll s us pe nd e xe c ution of th e c a lling th re a d until th e ta rg e t th re a d te rm ina te s . • R e turn va lue : – If s uc c e s s ful, th e pthre ad_jo in() func tion s h a ll re turn z e ro; oth e rwis e , a n e rror num be r s h a ll be re turne d to indic a te th e e rror.

Slide 18: J oining Th re a ds (C ont.)

Slide 19: E xa m ple -1-

Slide 20: #inc lude <pth re a d.h > #inc lude <s tdio.h > #de fine NUM_THR E ADS 5 void *P rintHe llo(void *th re a did) { int tid; tid = (int)th re a did; printf("He llo Wo rld! It's m e , th re a d #% d! tid); n", pth re a d_e xit(NULL); } int m a in (int a rg c , c h a r *a rg v[]) { pth re a d_t th re a ds [NUM_THR E ADS ]; int rc , t; for(t=0; t<NUM_THR E ADS ; t++){ printf("In m a in: c re a ting th re a d % d t); n", rc = pth re a d_c re a te (&th re a ds [t], NULL, P rintHe llo, (void *)t); if (rc ){ printf("E R R OR ; re turn c ode from pth re a d_c re a te () is % d rc ); n", e xit(-1); } } pth re a d_e xit(NULL);}

Slide 21: C ode E xpla na tion

Slide 22: int m a in (int a rg c , c h a r *a rg v[]) • De fin e s a n a rra y of th re a ds { (n ote : th re a ds a re no t ye t pth re a d_t c re a te d!) o f o pa q ue typ e th re a ds [NUM_THR E ADS ]; p th re a d_t int rc , t; • In fo r loo p: for(t=0; t<NUM_THR E ADS ; t++){ – Th re a d s will b e c re a te d printf("In m a in: c re a ting th re a d u s ing pthre ad_c re ate . % d t); n", Th e ro utine PrintHe llo () is rc = pth re a d_c re a te (&th re a ds [t], NULL, P rintHe llo, (void *)t); th e s ta rtin g rou tin e fo r a ll n e wly c re a te d th re a ds if (rc ){ – Th e re turn e d va lu e fo r printf("E R R OR ; re turn c ode from pth re a d_c re a te () is % d n", e a c h th re a d c re a tio n will rc ); b e c h e c ke d a n d p rints a n d e xit(-1); e rro r m e s s a g e if th e } re turne d va lue is n o t z e ro }

Slide 23: C ont. • Th e routine P rintHe llo void *P rintHe llo(void *th re a did) will s im ply print, { “He llo World! It’s me , th re a d# threadnum int tid; tid = (int)th re a did; • It will th e n te rmina te th e printf("He llo World! th re a d us ing It's me , th re a d pthread_exit. #% d! tid); n", pth re a d_e xit(NULL); }

Slide 24: P os s ib le Outputs In ma in: c re a ting th re a d 0 In ma in: c re a ting th re a d 1 In ma in: c re a ting th re a d 0 In ma in: c re a ting th re a d 2 In ma in: c re a ting th re a d 1 In ma in: c re a ting th re a d 3 He llo World! It's me , th re a d#0! In ma in: c re a ting th re a d 4 In ma in: c re a ting th re a d 2 He llo World! It's me , th re a d#0! He llo World! It's me , th re a d #1! He llo World! It's me , th re a d He llo World! It's me , th re a d #2! #1! In ma in: c re a ting th re a d 3 He llo World! It's me , th re a d In ma in: c re a ting th re a d 4 #2! He llo World! It's me , th re a d #3! He llo World! It's me , th re a d #3! He llo World! It's me , th re a d #4! He llo World! It's me , th re a d #4!

Slide 25: E xa m ple -2-

Slide 26: #inc lude <s tdio.h > #inc lude <s tdlib .h > #inc lude <pth re a d.h > void *print_m e s s a g e _func tion( void *ptr ); m a in() { pth re a d_t th re a d1, th re a d2; c h a r *m e s s a g e 1 = "Th re a d 1"; c h a r *m e s s a g e 2 = "Th re a d 2"; int ire t1, ire t2; / Cre a te inde pe nde nt th re a ds e a c h of wh ic h will e xe c ute func tion */ * ire t1 = pth re a d_c re a te ( &th re a d1, NULL, print_m e s s a g e _func tion, (void*) m e s s a g e 1); ire t2 = pth re a d_c re a te ( &th re a d2, NULL, print_m e s s a g e _func tion, (void*) m e s s a g e 2); / Wa it till th re a ds a re c om ple te b e fore m a in c ontinue s . Unle s s we */ * / wa it we run th e ris k of e xe c uting a n e xit wh ic h will te rm ina te */ * / th e proc e s s a nd a ll th re a ds be fore th e th re a ds h a ve c om ple te d. */ * pth re a d_join( th re a d1, NULL); pth re a d_join( th re a d2, NULL); printf("Th re a d 1 re turns : % dn",ire t1); printf("Th re a d 2 re turns : % dn",ire t2); e xit(0); } void *print_m e s s a g e _func tion( void *ptr ) { c h a r *m e s s a g e ; m e s s a g e = (c h a r *) ptr; printf("% s m e s s a g e ); n", }

Slide 27: C ode E xpla na tion

Slide 28: m a in() { • De fin e s a n a rra y of th re a ds pth re a d_t th re a d1, th re a d2; (n ote : th re a ds a re no t ye t c h a r *m e s s a g e 1 = "Th re a d 1"; c re a te d!) o f o pa q ue typ e c h a r *m e s s a g e 2 = "Th re a d 2"; p th re a d_t int ire t1, ire t2; ire t1 = pth re a d_c re a te ( &th re a d1, NULL, print_m e s s a g e _func tion, • Th re a d s will b e c re a te d us ing (void*) m e s s a g e 1); pthre ad_c re ate (). Th e ro u tin e ire t2 = pth re a d_c re a te ( &th re a d2, print_me s s ag e _func tio n() is NULL, print_m e s s a g e _func tion, th e s ta rtin g rou tin e fo r th e two (void*) m e s s a g e 2); n e wly c re a te d rou tin e s . No tic e th a t a n a rg u m e nt wa s s e nt to pth re a d_join( th re a d1, NULL); print_me s s ag e _func tio n() a s pth re a d_join( th re a d2, NULL); a n a rg um e nt to pthre ad_c re ate () printf("Th re a d 1 re turns : % dn",ire t1); printf("Th re a d 2 re turns : % dn",ire t2); • Th e two th re a d s ne wly c re a te d e xit(0); will b e jo in e d u s in g } pthre ad_jo in()

Slide 29: C ont. • Th e routine P rintHe llo void * will s im ply print, print_me s s a g e _func tio “He llo World! It’s m e , n( void *ptr ) { th re a d # threadnum c h a r *me s s a g e ; me s s a g e = (c h a r *) ptr; • It will th e n te rm ina te printf me s s a g e ); n", } ("% s th e th re a d us ing pthre ad_e xit.

Slide 30: Output Th re a d 1 Th re a d 2 Th re a d 1 re turns : 0 Th re a d 2 re turns : 0

Slide 31: E xa m ple -3- E xa m ple 1 wh ile u s in g pth re a d _jo in ()

Slide 32: #inc lude <pth re a d.h > #inc lude <s tdio.h > #de fine NUM_THR E ADS 5 void *P rintHe llo(void *th re a did) { int tid; tid = (int)th re a did; printf("He llo World! It's m e , th re a d #% d! tid); n", pth re a d_e xit(NULL); } int m a in (int a rg c , c h a r *a rg v[]) { pth re a d_t th re a ds [NUM_THR E ADS ]; int rc , t; for(t=0 ; t<NUM_THR E ADS ; t++){ printf("In m a in: c re a ting th re a d % d t); n", rc = pth re a d_c re a te (&th re a ds [t], NULL, P rintHe llo, (void *)t); if (rc ){ printf("E R R OR ; re turn c ode from pth re a d_c re a te () is % d rc ); n", e xit(-1); } pthre ad_jo in(thre ads [t], NULL) ;} pth re a d_e xit(NULL);}

Slide 33: P os s ib le Outputs In ma in: c re a ting th re a d 0 He llo World! It's me , th re a d#0! In ma in: c re a ting th re a d 1 He llo World! It's me , th re a d #1! In ma in: c re a ting th re a d 2 He llo World! It's me , th re a d #2! In ma in: c re a ting th re a d 3 He llo World! It's me , th re a d #3! In ma in: c re a ting th re a d 4 He llo World! It's me , th re a d #4!

Slide 34: E xa m ple -4-

Slide 35: #inc lude <pth re a d.h > #inc lude <s tdlib .h > #inc lude <s tdio.h > void *th re a d_func ( void *vptr_a rg s ); int m a in( void ){ int i, j; pth re a d_t th re a d; pth re a d_c re a te ( &th re a d, NULL, &th re a d_func , NULL ); for( j= 0; j < 6; ++j ){ fprintf( s tdout, "a ); n" for( i= 99999999; i; --i ); / us e s om e CP U tim e */ * } pth re a d_join( th re a d, NULL ); e xit( E XIT_S UCCE S S ); } void *th re a d_func ( void *vptr_a rg s ){ int i, j; for( j= 0; j < 6; ++j ){ fprintf( s tde rr, " b ); n" for( i= 99999999; i; --i ); / us e s om e CP U tim e */ * } re turn NULL; }

Slide 36: Output • Wh a t h a ppe ns if we c h a ng e th e a pos ition of pthre ad_jo in() in th e b a c ode ? a b a b a b b a b

Slide 37: #inc lude <pth re a d.h > #inc lude <s tdlib .h > #inc lude <s tdio.h > void *th re a d_func ( void *vptr_a rg s ); int m a in( void ){ int i, j; pth re a d_t th re a d; pth re a d_c re a te ( &th re a d, NULL, &th re a d_func , NULL ); pth re a d_join( th re a d, NULL ); for( j= 0; j < 6; ++j ){ fprintf( s tdout, "a ); n" for( i= 99999999; i; --i ); / us e s om e CP U tim e */ * } / P re vious pos ition in c ode * pth re a d_join( th re a d, NULL ); */ e xit( E XIT_S UCCE S S ); } void *th re a d_func ( void *vptr_a rg s ){ int i, j; for( j= 0; j < 6; ++j ){ fprintf( s tde rr, " b ); n" for( i= 99999999; i; --i ); / us e s om e CP U tim e */ * } re turn NULL;

Slide 38: Output • Wh a t h a ppe ne d? b b b b b b a a a a a a