Docsity

Threads - Computer Systems - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Threads - Computer Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

CSE 410Computer Systems

L^

t^

Th

d

Lecture 16 – Threads

What

’s in a process?

What s in a process?•^

A process consists of (at least):– an address space– the code for the running program– the data for the running program– an execution stack and stack pointer (SP)

  • traces state of procedure calls made
    • the program counter (PC), indicating the next

p^

g^

(^

)^

g

instruction

  • a set of general-purpose processor registers and their

values

t^

f OS

  • a set of OS resources
    • open files, network connections, sound channels,

That’s a lot of concepts bundled together!

-^

That s a lot of concepts bundled together!

3

What

’s needed?

What s needed?•^

In each of these examples of concurrency (web server,

b^

li^

t^

ll l

web client, parallel program):– Everybody wants to run the same code– Everybody wants to access the same data

E^

b d

h^

th

i il

  • Everybody has the same privileges– Everybody uses the same resources (open files,

network connections, etc.)

B t

o ’d like to ha e m ltiple hard

are e ec tion states

-^

But you’d like to have multiple hardware execution states:– an execution stack and stack pointer (SP)

  • traces state of procedure calls madeth^

t^

(PC) i di

ti^

th

t

  • the program counter (PC), indicating the next

instruction

  • a set of general-purpose processor registers and their

valuesvalues

5

How could we achieve this?How

could we achieve this?

-^

Given the process abstraction as we know it:– fork several processes– cause each to map to the same address space to

share data

-^

This is like making a pig fly – it’s really inefficient– space: PCB, page tables, etc.

time: creating OS structures fork and copy addr space

  • time: creating OS structures, fork and copy addr space,

etc.

-^

Some equally bad alternatives for some of the cases:

E ti

l^

t^

b

  • Entirely separate web servers– Asynchronous programming (explicit programming of

non-blocking I/Os) in the web client (browser)

6

Threads and processesThreads

and processes

-^

Most modern OS’s (Mach, Chorus, Win/XP, modern Unix)therefore support two entities:– the process, which defines the address space and

general process attributes (such as open files, etc.)

  • the thread, which defines a sequential execution

stream within a process

-^

A thread is bound to a single process

g^

p

  • processes, however, can have multiple threads

executing within them

  • sharing data between threads is cheap: all see same

sharing data between threads is cheap: all see sameaddress space

-^

Threads become the unit of scheduling

processes are just containers in which threads execute

  • processes are just containers in which threads execute

8

The design spaceThe

design space

key

MS/DOS

olderUNIXes

addressspace

one thread/processmany processes

one thread/process

one process

thread

Java

Mach NT

many threads/process

many threads/process

Mach

, NT,

Chorus,Linux, …

many processes

one process

9

(new) Address space with threads(new)

Address space with threads

0xFFFFFFFF

thread 1 stack

SP (T1)SP (T2)

thread 2 stack

address space

thread 3 stack

SP (T3)

heap(dynamic allocated mem)

0x

static data(data segment)code

PC (T2)

11

code(text segment)

(^

PC (T1)

PC (T3)

Process/thread separationProcess/thread

separation

-^

Concurrency (multithreading) is useful for:– handling concurrent events (e.g., web servers and

clients)

  • building parallel programs (e.g., matrix multiply, ray

tracing)tracing)

  • improving program structure (the Java argument) -^

Multithreading is useful even on a uniprocessor

e en tho gh onl

one thread can r n at a time

  • even though only one thread can run at a time -^

Supporting multithreading – that is, separating the conceptof a process (address space, files, etc.) from that of aminimal thread of control (execution state) is a big winminimal thread of control (execution state), is a big win– creating concurrency does not require creating new

processes

  • “faster better cheaper”

aste

bette

c eape

12

User

-Level Threads

User Level Threads•^

Threads can also be managed at the user level (that is,entirely from within the process)– a library linked into the program manages the threads

  • because threads share the same address space,

p^

the thread manager doesn’t need to manipulateaddress spaces (which only the kernel can do)

  • threads differ (roughly) only in hardware contexts

(^

g^

y)

y

(PC, SP, registers), which can be manipulated byuser-level code

  • Thread package multiplexes user-level threads on

p^

g^

p

top of kernel thread(s), which it treats as “virtualprocessors”

  • we call these user-level threads

14

The design spaceThe

design space

MS/DOS

olderUNIXes

addressspace

one thread/processmany processes

one thread/process

one process

thread

Java

Mach NT

many threads/process

many threads/process

Mach

, NT,

Chorus,Linux, …

many processes

one process

15

User

-level threads conceptually

user

level

User level threads, conceptually

user

-level thread library

address

Mach, NT,Chorus,

(thread create, destroy,signal, wait, etc.)

add essspace

Linux, …

os kernel

thread

os kernelCPU

17

User

-level threads

really

user

level

User level threads,

really

user

-level thread library

address

Mach, NT,Chorus,

(thread create, destroy, signal, wait,etc.)

add essspace

Linux, …

os kernel

kernel threads

thread

os kernel

( kernel

thread create, destroy, signal, wait, etc.)

CPU

18

Kernel threadsKernel

threads

•^

OS now manages threads and processes

ll h

d^

i^

i^

l^

d i

h^

k^

l

  • all thread operations are implemented in the kernel– OS schedules all of the threads in a system
    • if one thread in a process blocks (e.g., on I/O), the OS

knows about it and can run other threads from thatknows about it, and can run other threads from thatprocess

  • possible to overlap I/O and computation inside a process

•^

Kernel threads are cheaper than processes– less state to allocate and initialize

-^

But, they’re still pretty expensive for fine-grained use (e.g.,orders of magnitude more expensive than a procedure call)

thread operations are all system calls

  • thread operations are all system calls
    • context switch• argument checks
      • must maintain kernel state for each thread

must maintain kernel state for each thread

20

User

-level threads

User level threads•^

To make threads cheap and fast, they need to be

p^

y

implemented at the user level– managed entirely by user-level library, e.g. pthreads

-^

User level threads are small and fast

-^

User-level threads are small and fast– each thread is represented simply by a PC, registers, a

stack, and a small thread control block (TCB)

  • creating a thread, switching between threads, and

synchronizing threads are done via procedure calls• no kernel involvement is necessary!

no kernel involvement is necessary!

  • user-level thread operations can be 10-100x faster

than kernel threads as a result

21