P rocesses � Recall: A pr ocess includes � Address space (Code, Dat a, Heap, St ack) 4: Threads � Regist er values (including t he P C) � Resources allocat ed t o t he process • Memor y, open f iles, net wor k connect ions Last Modif ied: � Recall: how pr ocesses ar e cr eat ed � I nit ializing t he P CB and t he address space (page t ables) 6/ 1/ 2004 11:52:45 AM t akes a signif icant amount of t ime � Experiment : Time N it erat ions of f ork or vf ork � Recall: Type of int er pr ocess communicat ion � I P C is cost ly also � Communicat ion must go t hrough OS (“OS has t o guard any doors in t he walls it builds around processes f or t heir prot ect ion”) -1 -2 P roblem needs > 1 independent Example: Web Server sequent ial process? � Some pr oblems ar e har d t o solve as a single � Web ser ver s list en on an incoming socket f or sequent ial pr ocess; easier t o expr ess t he solut ion r equest s as a collect ion of cooper at ing pr ocesses � Once it receives a request , it ignore list ening t o t he � Hard t o writ e code t o manage many dif f erent t asks all at incoming socket while it services t he request once � Must do bot h at once � How would you writ e code f or “make phone calls while � One solut ion: Cr eat e a child pr ocess t o handle t he making dinner while doing dishes while looking t hrough t he mail” r equest and allow t he par ent t o r et ur n t o list ening � Can’t be independent processes because share dat a (your f or incoming r equest s brain) and share resources (t he kit chen and t he phone) � Pr oblem: This is inef f icient because of t he � Can’t do t hem sequent ially because need t o make progress on all t asks at once addr ess space cr eat ion (and memor y usage) and � Easier t o writ e “algorit hm” f or each and when t here is a PCB init ializat ion lull in one act ivit y let t he OS swit ch bet ween t hem � Let OS manage t he wait ing and mult it asking � On a mult ipr ocessor , exploit par allelism in pr oblem -3 -4 Observat ion I dea � There are similarit ies in t he process t hat � Let t hese t asks share t he address space, are spawned of f t o handle request s privileges and resources � They share t he same code, have t he same � Give each t heir own regist ers (like t he PC), pr ivileges, shar e t he same r esour ces (ht ml f iles t heir own st ack et c t o r et ur n, cgi scr ipt t o r un, dat abase t o sear ch, et c.) � Process – unit of r esour ce allocat ion � But t here are dif f erences (address space, privileges, resources) � Oper at ing on dif f er ent r equest s � Thread – unit of execut ion (PC, st ack, local � Each one will be in a dif f er ent st age of t he “handle r equest ” algor it hm variables) -5 -6 1
Single-Threaded vs P rocess vs Thread Mult it hreaded P rocesses � Each t hread belongs t o one process � One process may cont ain mult iple t hreads � Threads are logical unit of scheduling � Processes are t he logical unit of resource allocat ion -7 -8 Address Space Map For Single- Address Space Map For Threaded P rocess Mult it hreaded P rocess St ack Biggest Biggest Thread 1 st ack SP (t hread 1) (Space f or local var iables et c. Vir t ual Vir t ual For each nest ed pr ocedur e call) Address Address St ack Point er Thread 2 st ack SP (t hread 2) Heap Heap (Space f or memor y dynamically (Space f or memor y dynamically allocat ed e.g. wit h malloc) allocat ed e.g. wit h malloc) St at ically declar ed var iables St at ically declar ed var iables (Global var iables) (Global var iables) PC Code Code P C (t hread 2) (Text Segment ) (Text Segment ) P C (t hread 1) Ox0000 Ox0000 -9 -10 Kernel support f or t hreads? User -level Threads � Some OSes suppor t t he not ion of mult iple t hr eads per pr ocess and ot her s do not � Even if no “ker nel t hr eads” can build t hr eads at user level � Each “mult i- t hreaded” program get s a single kernel in t he process � During it s t imeslice, it runs code f rom it s various t hreads � User - level t hread package schedules t hreads on t he kernel level process much like OS schedules processes on t he CPU • SAT quest ion? CPU is t o OS is t o pr ocesses like? • Ker nel t hr ead is t o User-level t hr ead package is t o user t hreads � User - level t hread swit ch must be programmed in assembly (rest ore of values t o regist ers, et c.) -11 -12 2
User -level t hreads Kernel vs User Threads � How do user level t hr ead packages avoid having � One might t hink, kernel level t hreads are best and only if kernel does not support one t hr ead monopolize t he pr ocesses t ime slice? t hreads use user level t hreads � Solve much like OS does � Solut ion 1: Non-preempt ive � I n f act , user level t hreads can be much � Rely on each t hread t o periodically yield f ast er � Yield would call t he scheduling f unct ion of t he library � Thr ead cr eat ion , “Cont ext swit ch” bet ween � Solut ion 2: OS is t o user level t hr ead package like t hr eads, communicat ion bet ween t hr eads all hardware is t o OS done at user level � Ask OS t o deliver a periodic t imer signal � Pr ocedur e calls inst ead of syst em calls (ver if icat ion of all user ar gument s, et c.) in all � Use t hat t o gain cont rol and swit ch t he running t hread t hese cases! -13 -14 P roblems wit h User -level Scheduler Act ivat ions t hreads � OS does not have inf ormat ion about t hread � I f have ker nel level t hr ead suppor t available t hen use ker nel t hr eads * and* user -level t hr eads act ivit y and can make bad scheduling � Each pr ocess r equest s a number of ker nel t hr eads decisions t o use f or r unning user -level t hr eads on � Examples: � Ker nel pr omises t o t ell user - level bef or e it blocks � I f t hr ead blocks, whole pr ocess blocks a ker nel t hr ead so user - level t hr ead package can • Kernel t hreads can t ake overlap I / O and comput at ion choose what t o do wit h t he r emaining ker nel level wit hin a process! t hreads � Ker nel may schedule a pr ocess wit h all idle � User level pr omises t o t ell ker nel when it no longer t hreads needs a given ker nel level t hr ead -15 -16 Thread Support P t hreads I nt erf ace � Pt hr eads is a user- level t hread library � Can use mult iple kernel t hreads t o implement it on � POSI X t hr eads, user-level libr ar y suppor t ed on plat f orms t hat have kernel t hreads most UNI X plat f orms � J ava t hr eads (ext end Thr ead class) r un by t he � Much like t he similar ly named pr ocess f unct ions J ava Vir t ual Machine � t hr ead = pt hread_creat e(procedure) � Ker nel t hr eads � pt hr ead_exit � Linux has ker nel t hr eads (each has it s own t ask_ st r uct) – � pt hr ead_wait (t hread) creat ed wit h clone syst em call � Each user level t hread maps t o a single kernel t hread (Windows 95/ 98/ NT/ 2000/ XP , OS/ 2) � Many user level t hreads can map ont o many kernel level Not e: To use pt hr eads libr ar y, t hreads like scheduler act ivat ions (Windows NT/ 2000 # include < pt hr ead.h> wit h ThreadFiber package, Solaris 2) compile wit h -lpt hr ead -17 -18 3
P t hreads I nt erf ace (con’t) P erf ormance Comparison � P t hreads suppor t a var iet y of f unct ions f or Pr ocesses For k/ Exit 251 t hread synchronizat ion/ coordinat ion � Used f or coor dinat ion of t hr eads (I TC ☺ ) – Ker nel Thr eads Pt hr ead_cr eat e/ 94 mor e on t his soon! Pt hr ead_j oin � Examples: � Condit ion Var iables ( pt hr ead_cond_wait , pt hr ead_signal) User-level Pt hr ead_cr eat e/ 4.5 � Mut exes(pt hr ead_ mut ex_lock, Threads Pt hr ead_j oin pt hr ead_ mut ex_unlock) I n microseconds, on a 700 MHz Pent ium, Linux 2.2.16, St eve Gribble, 2001. -19 -20 Windows Thread Windows Threads Synchronizat ion � Windows suppor t s a var iet y of obj ect s t hat can be used f or t hr ead synchr onizat ion HANDLE CreateThread( � Examples LPSECURITY_ATTRIBUTES lpThreadAttributes , � Event s (Creat eEvent , Set Event , Reset Event , DWORD dwStackSize , Wait ForSingleObj ect ) LPTHREAD_START_ROUTINE lpStartAddress , � Semaphores (Creat eSemaphore, ReleaseSemaphore, Wait ForSingleObj ect ) DWORD dwCreationFlags , � Mut exes (Creat eMut ex , ReleaseMut ex , LPVOID lpParameter , Wait ForSingleObj ect ) DWORD dwCreationFlags , � Wait ForMult ipleObj ect LPDWORD lpThreadId ); � More on t his when we t alk about synchronizat ion -21 -22 Warning: Threads may be Out t akes hazardous t o your healt h � One can ar gue (and J ohn Oust er hout did) t hat � P rocesses t hat j ust share code but do not t hr eads ar e a bad idea f or most pur poses communicat e � Anyt hing you can do wit h t hr eads you can do wit h � Wast ef ul t o duplicat e an event loop � Remember “make phone calls while making dinner while � Ot her ways ar ound t his t han t hr eads doing dishes while looking t hrough t he mail” � Oust er hout says t hr ead pr ogr amming t o har d t o get right -23 -24 4
Recommend
More recommend