53
7/21/2019 10 Multithreads f V2 http://slidepdf.com/reader/full/10-multithreads-f-v2 1/53 1 © Nokia Solutions and Networks 2014 <Change information classification in footer> multithreads

10 Multithreads f V2

Embed Size (px)

Citation preview

Page 1: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 1/53

1 © Nokia Solutions and Networks 2014

<Change information classification in footer>

multithreads

Page 2: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 2/53

2 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Agenda

• Thread Concets !"#$% concurrencies&

• Thread 'anagement• Thread S(nchroni)ation

• Threads and Signals

• Threadsafe S(stem Calls

Page 3: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 3/53

* © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Concepts

• "#$

• concurrencies

Page 4: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 4/53

4 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Concepts

• Thread + a se,uence of instructions e-ecuted within the conte-t of

and is scheduled to run .( the oerating s(stem• $thread / is the $S3 standard rogramming interface for t

• Single+threaded + restricting access to a single thread

• 'ultithreaded + allowing access to two or more threads

• ser+le5el or 6lication+le5el threads + threads managed .( the th

routines in user !as oosed to kernel& sace7•  "ightweight rocesses + threads in the kernel that e-ecute kernel c

s(stem calls !also called "#$s&

•  8ound threads + threads that are ermanentl( .ound to "#$s

•  n.ound threads + threads that attach and detach from among th

•  Counting semahore + a memor(+.ased s(nchroni)ation mechani

Page 5: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 5/53

9 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Process Level

Page 6: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 6/53

: © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Concepts

• nderl(ing threads of control that is used .( threads li.rar(

•  6cts as a 5irtual C$ that e-ecutes code or s(stem calls• 8ridge the user le5el and the kernel le5el

17 ser le5el threads / faster to create% with faster conte-t switching than kernel threa

ro.lem is that when a single thread rocess .locks% since the kernel isn;t aware o

the entire rocess / and e5er( other thread in that rocess / .locks7

27 ernel threads / the kernel is aware of threads and makes indeendent scheduling

each thread% regardless of the rocess of which it is a mem.er7

"ightweight $rocesses

Page 7: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 7/53

= © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Concepts

There are a num.er of situations where threads can greatl( simlif( w

elegant and efficient rograms7 The sorts of ro.lems where thread

5er( useful include7

17 8locking 7

27 'ultile $rocessors7

*7 ser nterface7

47 $riorit(real+time scheduling97 Ser5ers6s(nchronous e5ent handling7

8enefits of Concurrenc(

Page 8: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 8/53

? © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Concepts

• @owe5er there are ro.lems when multile threads share a commo

sace7 The .iggest ro.lem concerns data races7

• Consider the following codeA

T@B6 1 T@B6 2

a = data;b = data;

a++;b--;

data = a;data = b;

[data = data - 1]

8enefits of Concurrenc(

Page 9: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 9/53

D © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• Creating and Terminating Threads

• Eoining and etaching Threads• Stack 'anagement

Page 10: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 10/53

10 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• Creating Threads

#include <pthread.h>

int pthread_create (pthread_t * tid,pthread_attr_t * attr, vo

*(*tart_routine! (void *!, void *ar"!;

• Terminating Threads

#include <pthread.h>

void pthread_eit(void *retval!;

Creating and Terminating Threads

Page 11: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 11/53

11 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• The thread returns from its starting routine7

• The thread makes a call to the threadFe-it su.routine7• The thread is canceled .( another thread 5ia the threadFcancel ro

• The entire rocess is terminated due to a call to either the e-ec or

su.routines7

#a(s of terminating a Thread

Page 12: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 12/53

12 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• Eoining

#include <pthread.h>

int pthread_$oin (pthread_t tid, void **value_ptr!;

• etaching

int pthread_attr_etdetachtate (pthread_attr_t * attr, int

detachtate!;

int pthread_detach (pthread_t tid!;

Eoining and etaching Threads

Page 13: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 13/53

1* © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• #hen a thread is created% one of its attri.utes defines whether it is

detached7 nl( threads that are created as Goina.le can .e Goined7

is created as detached% it can ne5er .e Goined7

• To e-licitl( create a thread as Goina.le or detached% the attr argum

threadFcreate!& routine is used7

Eoina.le or NotH

Page 14: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 14/53

14 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

•  6ttri.ute Iour Ste $rocessA

17 eclare a thread attri.ute 5aria.le of the threadFattrFt data t(27 nitiali)e the attri.ute 5aria.le with threadFattrFinit!&

*7 Set the attri.ute detached status with threadFattrFsetdetachsta

47 #hen done% free li.rar( resources used .( the attri.ute with

threadFattrFdestro(!&

Eoina.le or NotH (cont...)

Page 15: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 15/53

19 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• The $S3 standard does not dictate the si)e of a threadJs stack7 T

imlementation deendent and 5aries7

• -ceeding the default stack limit is often 5er( eas( to do% with the u

resultsA rogram termination andor corruted data7

• Safe and orta.le rograms do not deend uon the default stack

instead% e-licitl( allocate enough stack for each thread .( using th

threadFattrFsetstacksi)e routine7

• The threadFattrFgetstackaddr and threadFattrFsetstackaddr rout

used .( alications in an en5ironment where the stack for a threa

laced in some articular region of memor(7

$re5enting Stack $ro.lemsA

Page 16: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 16/53

1: © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• Creating thread stack si)e

#include <pthread.h>

int pthread_attr_ettac%i&e(pthread_attr_t *attr, i&e_t

int pthread_attr_"ettac%i&e(cont pthread_attr_t *attr,

*tac%i&e!;

• SetKet thread creation stack address

int pthread_attr_ettac%addr(pthread_attr_t *attr, void *

int pthread_attr_"ettac%addr(cont pthread_attr_t *attr,

**tac%addr!;

Stack 'anagement Iunctions

Page 17: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 17/53

1= © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Synchronization

• Bace Condition

• Conte-t Switching

Page 18: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 18/53

1? © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• #hile the code ma( aear on the screen in the order (ou wish the

e-ecute% threads are scheduled .( the oerating s(stem and are e

random7

• t cannot .e assumed that threads are e-ecuted in the order the( a

• The( ma( also e-ecute at different seeds7 #hen threads are e-ec

!racing to comlete& the( ma( gi5e une-ected results !race condit

• 'ute-es and Goins must .e utili)ed to achie5e a redicta.le e-ecut

and outcome7

Bace Condition

Page 19: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 19/53

1D © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread ManagementBace Condition

Page 20: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 20/53

20 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

The kernel allows conte-t switch to occur under the following circums

•Thread e-its

• ThreadJs time slice has e-ired and a tra is generated7

• Thread uts itself to slee% while awaiting a resource7

• Thread uts itself into a de.ug or sto state

• Thread returns to user mode from a s(stem call or tra

•  6 higher+riorit( thread .ecomes read( to run

Conte-t switching

Page 21: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 21/53

21 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• 'ute- 5aria.le functions

• Condition 5aria.le functions

• Semahores

• $riorit( and Scheduling

• Thread Secific ata

#a(s of s(nchroni)ing threads

Page 22: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 22/53

22 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• 'ute- is an a..re5iation for Lmutual e-clusionM

• $rimar( means of imlementing thread s(nchroni)ation7 $rotects s

when multile write occurs7

•  6 mute- 5aria.le acts like a MlockM rotecting access to a shared re

17 nl( one thread can lock !or own& a mute- 5aria.le at an( gi5en time7

27 5en if se5eral threads tr( to lock a mute-% onl( one thread will .e successfu

threads are .locked until the owner release the lock7

• 'ute- is used to re5ent Lrace conditions M7+ race conditionA anomalous .eha5iour due to une-ected critical deendence on

timing of e5ents7

'ute- aria.les

Page 23: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 23/53

2* © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management-amle iagram !'ute-&

Global variables, etc

Page 24: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 24/53

24 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

CB6TNK 'T3S

threadFmute-attrFinit!&

• nitiali)e a mute- attri.ute o.Gect7 6 thread secifies a mute- attri.ute o.Gec

to threadFmute-Finit to set the characteristics of new mute-es7

int pthread_'uteattr_init (pthread_'uteattr_t *attr!;

threadFmute-Finit!&

• nitiali)e a mute- with the attri.utes secified in the secified mute- atrri.ut

attr is N""% the default attri.utes are used7

int pthread_'ute_init ( pthread_'ute_t *'ute, cont

pthread_'uteattr_t *attr!;

'ute- Iunction

Page 25: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 25/53

29 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

"CNK N"CNK 'T3S

threadFmute-Flock!&

•  6c,uired a lock on the secified mute- 5aria.le7

• f the mute- is alread( locked .( another thread% .lock the calling

the mute- is unlocked7

int pthread_'ute_loc% (pthread_'ute_t *'ute!;

'ute- Iunction (cont...)

Page 26: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 26/53

2: © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

threadFmute-Ftr(lock!&

•  6ttemt to lock a mute-7

• f the mute- is alread( locked% return immediatel( with a L.us(M erro

int pthread_'ute_trloc% ( pthread_'ute_t *'ute!;

threadFmute-Funlock!&

• nlocks a mute- if called .( the owning thread7int pthread_'ute_unloc% (pthread_'ute_t *'ute!;

'ute- Iunction (cont...)

Page 27: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 27/53

2= © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

STBONK 'T3S

threadFmute-Fdestro(!&

• estro(s a mute- o.Gect% freeing the resources it might hold7

int pthread_'ute_detro (pthread_'ute_t *'ute!;

threadFmute-attrFdestro(!&• estro(s a mute- attri.ute o.Gect7

int pthread_'uteattr_detro (pthread_'uteattr_t *attr!;

'ute- Iunction (cont...)

Page 28: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 28/53

2? © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

'ute- eadlock

• This condition occurs when a mute- is alied .ut then not Punlock

• This causes rogram e-ecution to halt indefinitel(7

• t can also .e caused .( oor alication of mute-es or Goins7 8e c

al(ing two or more mute-es to a section of code7

Besource eadlocks

Th d M t

Page 29: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 29/53

2D © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• #ithout condition 5aria.les% the rogrammer would need to ha5e th

continuall( olling to check if the condition is met7

 + This can .e 5er( resource consuming since the thread would .e

continuall( .us( in this acti5it(7

•  6 condition 5aria.le is alwa(s used in conGunction with a mute- lock

Condition aria.les

Th d M t

Page 30: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 30/53

*0 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

•  6 thread locks a mute- associated with a condition 5aria.le

• The thread tests the condition to see if it can roceed7

f it canA

17 Oour thread does its work

27 Oour thread unlocks the mute-

f it cannotA

17 The thread slees7 The mute- is automaticall( released7

27 Some other threads signals the condition 5aria.le7

*7 Oour thread wakes u from waiting with the mute- automaticall( locked% and it does i

47 Oour thread releases the mute- when it;s done7

@ow Condition aria.les #orkH

Th d M t

Page 31: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 31/53

*1 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

CB6TNK STBONK CNTN 6TTB8TS

threadFcondattrFinit!&

• initiali)es a condition 5aria.le attri.utes o.Gect attr with the defau

Qinclude <thread7h>

int threadFcondattrFinit!threadFcondattrFt Rattr&

threadFcondattrFdestro(!&

• estro(s a condition attri.ute 5aria.le% freeing the resources it mig

#include <pthread.h>

int pthread_cond_detro (pthread_cond_t *cond!;

Condition aria.les

Thread Management

Page 32: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 32/53

*2 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

CB6TNK STBONK CNTN

threadFcondFinit!&

• nitiali)es the condition 5aria.le cond% using the condition attri.utes

in condFattr% or default attri.utes if condFattr is N""7

int threadFcondFinit! threadFcondFt Rcond% threadFcondattrFt RcondFattr&

threadFcondFdestro(!&• estro(s a condition 5aria.le% freeing the resources it might hold7

pthread_cond_detro (pthread_cond_t *cond!;

Condition aria.les

Thread Management

Page 33: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 33/53

** © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

#6TNK and #6NK CNTNS

threadFcondFwait!&

• 8locks the calling thread until the secified condition is signalled7

• This would .e called while mute- is locked% and it will automaticall(

mute- while it waits7

int threadFcondFwait !threadFcondFt Rcond% threadFmute-Ft Rmute-&

Condition aria.les

Thread Management

Page 34: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 34/53

*4 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

#6TNK and #6NK CNTNS (cont...)

threadFcondFsignal!&

• Signals another thread which is waiting on the condition 5aria.le%

• Calling thread should ha5e a lock7

int pthread_cond_i"nal (pthread_cond_t *cond!;

Condition aria.les

Thread Management

Page 35: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 35/53

*9 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• #hen this otion is ena.led% each thread ma( ha5e its own schedu

roerties7

• Scheduling $olic(

The three main scheduling olicies are SC@FII % SC@FB

SC@FT@B

Thread Scheduling

Thread Management

Page 36: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 36/53

*: © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

• #e use threadFattrFsetschedolic( to set the scheduling olic(

#include <pthread.h>

int pthread_attr_etchedpolic (pthread_attr_t *attr, int po

•  and threadFattrFsetschedaram to set the scheduling riorit(7

#include <ched.h>

int pthread_attr_etchedpara' (pthread_attr_t *attr,

cont truct ched_para' *para'!;

Setting Scheduling $olic( and $riorit(

Thread Management

Page 37: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 37/53

*= © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

Thread+secific data allows a thread to maintain its own glo.al storag

hidden from the other threads7

• ach thread has a ri5ate memor( .lock% the TS area7

•  6n arra( of 5oid ointers% inde-ed .( ke(s7

• #ith mechanism that ena.les the same glo.al 5aria.le to ha5e a d

location in memor(7

• @as a wa( for the s(stem to automaticall( call a data destructor fun

cleans u the thread+secific data when a thread ends7

Thread Secific ata !TS&

Thread Management

Page 38: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 38/53

*? © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

threadFke(Fcreate!&

• Creates the thread secific data which is identified .( ke( and an o

ointer to a destructor function7

int pthread %e create(pthread %e t *%e, void

(*detr )unction! (void *!!;

threadFke(Fdelete!&

• Bemo5es the thread secific data which is identified .( ke(

int pthread %e delete (pthread %e t %e!;

Creating and estro(ing Thread Secific ata e(

Thread Management

Page 39: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 39/53

*D © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

threadFke(Fcreate!&

• Creates the thread secific data which is identified .( ke( and an o

ointer to a destructor function7

int pthread %e create(pthread %e t *%e, void

(*detr )unction! (void *!!;

threadFke(Fdelete!&

• Bemo5es the thread secific data which is identified .( ke(

int pthread %e delete (pthread %e t %e!;

Creating and estro(ing Thread Secific ata e(

Thread Management

Page 40: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 40/53

40 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Thread Management

threadFsetsecific!&

•  6ssigns the data ointed .( ointer to the memor( region defined .

int pthread etpeci)ic(pthread %e t %e, cont void *pointe

thread getsecific!&

• retrie5es the data ointed .( ointer to the memor( region defined

void * pthread "etpeci)ic(pthread %e t %e!;

Set Ket Thread Secific ata

Threads and Signals

Page 41: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 41/53

41 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Threads and Signals

• Signal @andling

• Signal @andling Iunction

Threads and Signals

Page 42: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 42/53

42 © Nokia Solutions and Networks 2014

<Change information classification in footer>

g

 6ll threads share the rocess signal handlers% .ut each thread has its

mask of the signals it .locks7

There are three t(es of signals with different methods of deli5er(A

• 6s(nchronous

• S(nchronous

• irected

Page 43: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 43/53

Threads and Signals

Page 44: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 44/53

44 © Nokia Solutions and Networks 2014

<Change information classification in footer>

g

threadFsigmask !&

• Changes the signal mask for the calling thread as descri.ed .( the

newmask arguments7 f oldmask is not N""% the re5ious signal mstored in the location ointed to .( oldmask7

#include <i"nal.h>

int pthread_i"'a%( int ho, cont i"et_t *ne'a%,

i"et_t *old- 'a%!;

 Note:

  parameter how: SIG_BLOCK ,SIG_SETMASK ,SIG_NBLOCK !"

$!"c&""e$ on top!c SIGNAL pre"entat!on.

 

Signal @andling Iunction

Threads and Signals

Page 45: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 45/53

49 © Nokia Solutions and Networks 2014

<Change information classification in footer>

threadFkill !&

• send signal num.er signo to the thread thread7

#include <i"nal.h>

int pthread_%ill(pthread_t thread, int i"no!;

sigwait!&

• Susends the calling thread until one of the signals in set is deli5er

calling thread7

int i"ait(cont i"et_t *et, int *i"!;

 

Signal @andling Iunction (cont...)

Threadsafe System Calls

Page 46: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 46/53

4: © Nokia Solutions and Networks 2014

<Change information classification in footer>

• Thread Safe Code

• Threadsafe Iailure $oints

Threadsafe System Calls

Page 47: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 47/53

4= © Nokia Solutions and Networks 2014

<Change information classification in footer>

•  6n( function that does not use static data or other shared resource

safe7

• There should .e no static or glo.al 5aria.les which other threads mor read assuming single threaded oeration7

• f static or glo.al 5aria.les are used then mute-es must .e alied

functions must .e re+written !reentrant functions& to a5oid the use o

5aria.les7

• Beentranc( is a characteristic of a rogram in memor( that does noitself in an( wa(7

Thread Safe Code

Threadsafe System Calls

Page 48: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 48/53

4? © Nokia Solutions and Networks 2014

<Change information classification in footer>

Class 1A Iailing to rotect shared 5aria.les

I3A se semahore oerations7

SSA S(nchroni)ation will slow down code7

Class 2A Bel(ing on the ersistence state across in5ocations7

I3A rewrite function so that caller asses in all the necessar( sta

-amleAchar *trtoupper(char *trin"! non-reentrant )unction

char *trtoupper_r(char *in_tr, char *out_tr!reentrant )u

Threadsafe Iailure $oints

Threadsafe System CallsTh d f I il $ i t

Page 49: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 49/53

4D © Nokia Solutions and Networks 2014

<Change information classification in footer>

Class *A Beturning a ointer to a static 5aria.le

*non reentrant )unction*

  truct hotent *"ethotbna'e(char* na'e!

  tatic truct hotent h;

  contact / and )ill up h

  return 0h;

 

 

Threadsafe Iailure $oints

Threadsafe System CallsTh d f I il $ i t

Page 50: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 50/53

90 © Nokia Solutions and Networks 2014

<Change information classification in footer>

  I3A

  17 Bewrite code so that caller asses ointer to struct7

  truct hotent * hotp = 'alloc(..!;  "ethotbna'e_r(na'e,hotp!; call reentrant )unctio

  27 "ock and co(  truct hotent *"ethotbna'e_t(char* na'e!   truct hotent *unhared = 'alloc(..!;  2(0'ute!; loc%  hared = "ethotbna'e(na'e!;  *unhared = *hared *cop*  3(0'ute!; unloc%  return hared; 

 

Threadsafe Iailure $oints

Threadsafe System CallsTh d f I il $ i t

Page 51: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 51/53

91 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Class 4A Calling thread+unsafe functions

Calling one thread+unsafe function makes an entire function thre

  I3A 'odif( the function so it calls onl( thread safe functions

Thread+unsafe function Beentrant 5ersion  strtok strtokFr   asctime asctimeFr   ctime ctimeFr 

  gethost.(addr gethost.(addrFr   gethost.(name gethost.(nameFr   localtime localtimeFr   rand randFr 

Threadsafe Iailure $oints

Question & Answer 

Page 52: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 52/53

92 © Nokia Solutions and Networks 2014

<Change information classification in footer>

Page 53: 10 Multithreads f V2

7/21/2019 10 Multithreads f V2

http://slidepdf.com/reader/full/10-multithreads-f-v2 53/53

9* © Nokia Solutions and Networks 2014

<Change information classification in footer>