P rocesses Recall: A pr ocess includes Address space (Code, Dat a, - - PDF document

p rocesses
SMART_READER_LITE
LIVE PREVIEW

P rocesses Recall: A pr ocess includes Address space (Code, Dat a, - - PDF document

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


slide-1
SLIDE 1

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

I dea

Let t hese t asks share t he address space,

privileges and resources

Give each t heir own regist ers (like t he PC),

t heir own st ack et c

Process – unit of r esour ce allocat ion

(address space, privileges, resources)

Thread – unit of execut ion (PC, st ack, local

variables)

slide-2
SLIDE 2

2

  • 7

Single-Threaded vs Mult it hreaded P rocesses

  • 8

P rocess vs Thread

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

  • 9

Address Space Map For Single- Threaded P rocess

St ack

(Space f or local var iables et c. For each nest ed pr ocedur e call)

Heap

(Space f or memor y dynamically allocat ed e.g. wit h malloc)

St at ically declar ed var iables

(Global var iables)

Code

(Text Segment )

St ack Point er PC Ox0000 Biggest Vir t ual Address

  • 10

Address Space Map For Mult it hreaded P rocess

Heap

(Space f or memor y dynamically allocat ed e.g. wit h malloc)

St at ically declar ed var iables

(Global var iables)

Code

(Text Segment ) Thread 1 st ack

SP (t hread 1) P C (t hread 2) Ox0000 Biggest Vir t ual Address P C (t hread 1)

Thread 2 st ack

SP (t hread 2)

  • 11

Kernel support f or t hreads?

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.)

  • 12

User -level Threads

slide-3
SLIDE 3

3

  • 13

User -level t hreads

How do user level t hr ead packages avoid having

  • ne t hr ead monopolize t he pr ocesses t ime slice?

Solve much like OS does

Solut ion 1: Non-preempt ive

Rely on each t hread t o periodically yield Yield would call t he scheduling f unct ion of t he library

Solut ion 2: OS is t o user level t hr ead package like

hardware is t o OS

Ask OS t o deliver a periodic t imer signal Use t hat t o gain cont rol and swit ch t he running t hread

  • 14

Kernel vs User Threads

One might t hink, kernel level t hreads are

best and only if kernel does not support t hreads use user level t hreads

I n f act , user level t hreads can be much

f ast er

Thr ead cr eat ion , “Cont ext swit ch” bet ween

t hr eads, communicat ion bet ween t hr eads all done at user level

Pr ocedur e calls inst ead of syst em calls

(ver if icat ion of all user ar gument s, et c.) in all t hese cases!

  • 15

P roblems wit h User -level t hreads

OS does not have inf ormat ion about t hread

act ivit y and can make bad scheduling decisions

Examples:

I f t hr ead blocks, whole pr ocess blocks

  • Kernel t hreads can t ake overlap I / O and comput at ion

wit hin a process!

Ker nel may schedule a pr ocess wit h all idle

t hreads

  • 16

Scheduler Act ivat ions

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

Each pr ocess r equest s a number of ker nel t hr eads

t o use f or r unning user -level t hr eads on

Ker nel pr omises t o t ell user - level bef or e it blocks

a ker nel t hr ead so user - level t hr ead package can choose what t o do wit h t he r emaining ker nel level t hreads

User level pr omises t o t ell ker nel when it no longer

needs a given ker nel level t hr ead

  • 17

Thread Support

Pt hr eads is a user- level t hread library

Can use mult iple kernel t hreads t o implement it on

plat f orms t hat have kernel t hreads J ava t hr eads (ext end Thr ead class) r un by t he

J ava Vir t ual Machine

Ker nel t hr eads

Linux has ker nel t hr eads (each has it s own t ask_

st r uct) – 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

t hreads like scheduler act ivat ions (Windows NT/ 2000 wit h ThreadFiber package, Solaris 2)

  • 18

P t hreads I nt erf ace

POSI X t hr eads, user-level libr ar y suppor t ed on

most UNI X plat f orms

Much like t he similar ly named pr ocess f unct ions

t hr ead = pt hread_creat e(procedure) pt hr ead_exit pt hr ead_wait (t hread)

Not e: To use pt hr eads libr ar y, # include < pt hr ead.h> compile wit h -lpt hr ead

slide-4
SLIDE 4

4

  • 19

P t hreads I nt erf ace (con’t)

P

t hreads suppor t a var iet y of f unct ions f or t hread synchronizat ion/ coordinat ion

Used f or coor dinat ion of t hr eads (I TC ☺) –

mor e on t his soon! Examples:

Condit ion Var iables ( pt hr ead_cond_wait ,

pt hr ead_signal)

Mut exes(pt hr ead_ mut ex_lock,

pt hr ead_ mut ex_unlock)

  • 20

P erf ormance Comparison

4.5 Pt hr ead_cr eat e/ Pt hr ead_j oin User-level Threads 94 Pt hr ead_cr eat e/ Pt hr ead_j oin Ker nel Thr eads 251 For k/ Exit Pr ocesses

I n microseconds, on a 700 MHz Pent ium, Linux 2.2.16, St eve Gribble, 2001.

  • 21

Windows Threads

HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, DWORD dwCreationFlags, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);

  • 22

Windows Thread 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

Examples

Event s (Creat eEvent , Set Event , Reset Event ,

Wait ForSingleObj ect )

Semaphores (Creat eSemaphore, ReleaseSemaphore,

Wait ForSingleObj ect )

Mut exes (Creat eMut ex , ReleaseMut ex ,

Wait ForSingleObj ect )

Wait ForMult ipleObj ect More on t his when we t alk about synchronizat ion

  • 23

Warning: Threads may be hazardous t o your healt h

One can ar gue (and J ohn Oust er hout did) t hat

t hr eads ar e a bad idea f or most pur poses

Anyt hing you can do wit h t hr eads you can do wit h

an event loop

Remember “make phone calls while making dinner while

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

  • 24

Out t akes

P

rocesses t hat j ust share code but do not communicat e

Wast ef ul t o duplicat e Ot her ways ar ound t his t han t hr eads

slide-5
SLIDE 5

5

  • 25

Example: User I nt erf ace

Allow one t hread t o respond t o user input

while anot her t hread handles a long

  • per at ion

Assign one t hread t o print your document ,

while allowing you t o cont inue edit ing

  • 26

Benef it s of Concurrency

Hide lat ency of blocking I / O wit hout addit ional

complexit y

Wit hout concurrency

  • Block whole process
  • Manage complexit y of asynchr onous I / O (per iodically

checking t o see if it is done so can f inish pr ocessing)

Abilit y t o use mult iple pr ocessor s t o accomplish

t he t ask

Ser ver s of t en use concur r ency t o wor k on mult iple

r equest s in par allel

User I nt er f aces of t en designed t o allow int er f ace

t o be r esponsive t o user input while ser vicing long

  • per at ions
  • 27

Thread pools

What t hey are and how t hey avoid t hread

cr eat ion over head

  • 28

Experiment

St art up various processes under Windows

(Wor d, I E,..)

How many processes are st art ed? How many t hreads and of what priorit y?