1
- 1
4: Threads
Last Modif ied: 6/ 1/ 2004 11:52:45 AM
- 2
P rocesses
Recall: A pr ocess includes
Address space (Code, Dat a, Heap, St ack) 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
Recall: how pr ocesses ar e cr eat ed
I nit ializing t he P
CB and t he address space (page t ables) 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”)
- 3
P roblem needs > 1 independent sequent ial process?
Some pr oblems ar e har d t o solve as a single
sequent ial pr ocess; easier t o expr ess t he solut ion as a collect ion of cooper at ing pr ocesses
Hard t o writ e code t o manage many dif f erent t asks all at
- nce
How would you writ e code f or “make phone calls while
making dinner while doing dishes while looking t hrough t he mail”
Can’t be independent processes because share dat a (your
brain) and share resources (t he kit chen and t he phone)
Can’t do t hem sequent ially because need t o make
progress on all t asks at once
Easier t o writ e “algorit hm” f or each and when t here is a
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
- 4
Example: Web Server
Web ser ver s list en on an incoming socket f or
r equest s
Once it receives a request , it ignore list ening t o t he
incoming socket while it services t he request
Must do bot h at once
One solut ion: Cr eat e a child pr ocess t o handle t he
r equest and allow t he par ent t o r et ur n t o list ening f or incoming r equest s
Pr oblem: This is inef f icient because of t he
addr ess space cr eat ion (and memor y usage) and PCB init ializat ion
- 5
Observat ion
There are similarit ies in t he process t hat
are spawned of f t o handle request s
They share t he same code, have t he same
pr ivileges, shar e t he same r esour ces (ht ml f iles t o r et ur n, cgi scr ipt t o r un, dat abase t o sear ch, et c.) But t here are dif f erences
Oper at ing on dif f er ent r equest s Each one will be in a dif f er ent st age of t he
“handle r equest ” algor it hm
- 6