what is a thread? - columbia universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • the...

37
What is a Thread? Similar to a process Separate “thread of execution” Each thread has a separate stack, program counter, and run state Steven M. Bellovin February 1, 2006 1

Upload: others

Post on 21-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

What is a Thread?

• Similar to a process

• Separate “thread of execution”

• Each thread has a separate stack, program counter, and run state

Steven M. Bellovin February 1, 2006 1

Page 2: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Differences Between Processes and Threads

• Threads share the same address space

• Threads always have the same security context (i.e., UID)

• Threads share file state.

– A child process will inherit open files from the parent, but won’tsee newly-opened files. A child thread will see them.

• Non-preemptive scheduling

Steven M. Bellovin February 1, 2006 2

Page 3: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Non-preemptive Scheduling

• There is no timer to make a thread yield the CPU

• Threads must voluntarily yield control to let another thread run

• Thread history isn’t taken into account by the scheduler

• Threads are co-operative, not competitive

Steven M. Bellovin February 1, 2006 3

Page 4: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Why Threads?

• Perform operations in parallel on the same data

• Avoid complex explicit scheduling by applications

• Often more efficient than separate processes

Steven M. Bellovin February 1, 2006 4

Page 5: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Example: Web Browser

• Download page in one thread

• Actually, use a separate thread for each separately-downloadedimage

• Let another thread respond to mouse clicks

• Maybe have a separate thread for each tab or page image

Steven M. Bellovin February 1, 2006 5

Page 6: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

More Examples

• Web server

• Word processor

• Most graphical applications

• Anything where separate execution patterns need to share lots ofdata

Steven M. Bellovin February 1, 2006 6

Page 7: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Implementing Threads

• Three basic strategies

• User-level: possible on most operating systems, even ancient ones

• Kernel-level: looks almost like a process (i.e., Linux, Solaris)

• Scheduler activations (Digital Tru Unix 64, NetBSD, some Mach)

Steven M. Bellovin February 1, 2006 7

Page 8: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

User-Level Threads

• Thread creation, destruction, and scheduling done at user level

• To create a thread, must allocate storage for stack, program counter,registers, state, etc, in a thread table

• When a thread yields the CPU or blocks, the thread library saveseverything in the thread table

• The thread scheduler finds the highest-priority thread that’s ready torun

• Its registers, program counter, etc., are restored from its thread table

Steven M. Bellovin February 1, 2006 8

Page 9: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Efficiency Gains

• No system calls needed

• No context switch

• No copying data safely across a protection boundary

• No need to flush the hardware memory cache

Steven M. Bellovin February 1, 2006 9

Page 10: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Disadvantages of User-Level Threads

• Blocking system calls

• Page faults

• Inability to benefit from multiprocessor CPUs

• Kernel can’t automatically grow per-thread stacks

Steven M. Bellovin February 1, 2006 10

Page 11: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Growing the Stack

• The system allocates a certain amount of memory for the initial pieceof the stack

• References to other potential parts of the stack cause a trap

• If this trap looks like an attempt to use an unallocated part of thestack, the kernel allocates more memory and extends the stack

• This only works if the kernel knows where the stack is

Steven M. Bellovin February 1, 2006 11

Page 12: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Blocking System Calls

• What if a thread issues, say, a read() call and there’s no dataavailable?

• The kernel will make the whole process block because it’s unaware ofthe thread structure

Steven M. Bellovin February 1, 2006 12

Page 13: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Page Faults

• Part of the virtual memory system; won’t say much now

• Briefly, though — a trap occurs because referenced memory isn’tavailable

• No ability to suspend just one thread

Steven M. Bellovin February 1, 2006 13

Page 14: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Wrapper Functions

• The thread library can contain alternate versions of system callfunctions

• These versions check for blocking before calling the kernel

• Example: do a non-blocking select() or poll() before a read()

• If the call would block, fire up another thread instead

• If no threads are ready, do a blocking select() or poll() and letthe kernel run a different process

Steven M. Bellovin February 1, 2006 14

Page 15: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Kernel Threads

• Similar thread table, but in the kernel

☞ Often integrated with process table

• No problem with blocking system calls — ordinary processscheduling does the right thing

• Handles multi-CPU case easily

• But — thread-switching is expensive, since it needs a context switch

• Still cheaper than process switches, since you can use the samevirtual memory map

Steven M. Bellovin February 1, 2006 15

Page 16: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Other Schemes

• Hybrid: some kernel threads, with each kernel thread supportingseveral user-level threads

• Scheduler activations

• Pop-up threads

Steven M. Bellovin February 1, 2006 16

Page 17: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Scheduler Activations

• Get the best of both worlds — the efficiency of user-level threads andthe non-blocking ability of kernel threads

• Relies on upcalls

Steven M. Bellovin February 1, 2006 17

Page 18: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

What’s an Upcall?

• Normally, user programs call functions in the kernel

• Sometimes, though, the kernel calls a user-level process to report anevent

• Sometimes considered unclean — violates usual layering

Steven M. Bellovin February 1, 2006 18

Page 19: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Scheduler Activations

• Thread creation and scheduling is done at user level

• When a system call from a thread blocks, the kernel does an upcall tothe thread manager

• The thread manager marks that thread as blocked, and starts runninganother thread

• When a kernel interrupt occurs that’s relevant to the thread, anotherupcall is done to unblock it

Steven M. Bellovin February 1, 2006 19

Page 20: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Upcall

Thread2

Thread2

Thread ThreadSched

Thread Thread1

ThreadSched

Kernel

1 Sched

Systemcall

UpcallInterrupt

Steven M. Bellovin February 1, 2006 20

Page 21: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Pop-Up Threads

• When a message arrives, the kernel creates a new thread

• Sometimes, the thread can run entirely in the kernel

• Messy and delicate — what process should own the thread, whatsorts of events result in thread creation, etc.

Steven M. Bellovin February 1, 2006 21

Page 22: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Using Threads

• Library routines may be used by more than one thread simultaneously

• Not all routines are prepared for this

• Static variables can be overwritten by another thread

• Must use reentrant routines

Steven M. Bellovin February 1, 2006 22

Page 23: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

What is a Reentrant Routine?

• A routine that can be invoked more than once simultaneously

• Can only use local variables or dynamically allocated variables

• Must not use static variables

• Many C library routines do use static buffers!

Steven M. Bellovin February 1, 2006 23

Page 24: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Normal and Reentrant Versions

Normal struct tm *localtime(const time t *timep);Reentrant struct tm *localtime r(const time t *timep, struct tm *result);

The normal version stores store the result in a static buffer and passes apointer back; the reentrant version requires the caller to supply a buffer.

Steven M. Bellovin February 1, 2006 24

Page 25: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

What if You Need Global Variables?

• Sometimes, there are global variables you have to use

• The whole purpose of threads is to share access to certain data

• Must synchronize access, but that’s a topic for another day

Steven M. Bellovin February 1, 2006 25

Page 26: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Signals

• Short messages to a process

• More accurately, an interrupt sent to a process, much like hardwareinterrupts to the OS

• Often, signals not explicitly fielded will terminate the process

Steven M. Bellovin February 1, 2006 26

Page 27: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Types of Signals

Environment Hangup, ˆ C, coredump, power failure

Bugs Assertion failed, memory error, floating point error, resoureexceeded, pipe closed, bad system call, timeout

Debugging Tracing, profiling

I/O I/O possible, urgent message, window size change, backgroundprocess wants to talk to terminal

Other Many more

Steven M. Bellovin February 1, 2006 27

Page 28: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Default Signal Actions

• Ignore — harmless

• Terminate program

• Create a core dump and terminate program

• Suspend program

• Resume program

Steven M. Bellovin February 1, 2006 28

Page 29: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Programs and Signals

• Send a signal to a process or thread

• Call a subroutine when a signal occurs

• Temporarily block signals

• Test for or wait for a signal

Steven M. Bellovin February 1, 2006 29

Page 30: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Sending a Signal

• Signals can be generated by the kernel

• Example: when the terminal handler sees ˆ C, it sends SIGINT to allprocesses associated with that terminal

• Signals can be sent explicitly by another process:kill(pid, signum)

• The shell’s kill command translates directly into the kill()

system call

• The default signal is catchable; you have to be more forceful to dealwith that if you really want to terminate the process. . .

Steven M. Bellovin February 1, 2006 30

Page 31: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Catching Signals

• The sigaction() subroutine specifies what to do when a signaloccurs

• Choices: default action, call a subroutine, ignore the signal

• SIGKILL can’t be caught; it always terminates the program

• When a signal is caught, other signals can be blocked until thesubroutine returns

Steven M. Bellovin February 1, 2006 31

Page 32: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Blocking Signals

• sigprocmask() blocks some signals

• The signals remain pending; when unblocked, they will occur

• SIGKILL can’t be blocked, either

Steven M. Bellovin February 1, 2006 32

Page 33: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Suspending a Process

• sigsuspend() suspends a process and changes the set of blockedsignals

• Cannot accomplish the same thing with

sigprocmask(...);

sleep(...);

• There’s a race condition — what if the signal occurs between the twostatements?

Steven M. Bellovin February 1, 2006 33

Page 34: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Race Conditions

sigprocmask(...);

Signal occurssleep(...);

Never wakes up

sigprocmask(...);

sleep(...);

Signal occursProcess awakened by signal

Correct behavior here depends on which event occurs first. Testing isunlikely to find the flaw, since it’s possible but improbable. To avoid racecondition problems, you need to rely on atomic operations, such assigsuspend()

Steven M. Bellovin February 1, 2006 34

Page 35: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Signals and Processes

• Signal state is inherited by child processes

• Signal state is partially preserved across exec() calls:

– Ignored signals remain ignored

– Caught signals are reset to default action

• Forgetting about ignored signals can cause problems for childprocesses. (I have a script that resets the action for six differentsignals before running gv — I sometimes need this with my browser.)

Steven M. Bellovin February 1, 2006 35

Page 36: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Signals and Threads

• Signal handlers are shared among all threads in a process

• Each thread can have its own signal mask

• If a signal occurs, it’s randomly sent to a single thread that doesn’tblock it

Steven M. Bellovin February 1, 2006 36

Page 37: What is a Thread? - Columbia Universitysmb/classes/s06-4118/l05.pdf · 2006. 2. 1. · • The thread manager marks that thread as blocked, and starts running another thread • When

Signals and Interrupts

• Unix signals are the closest thing to hardware interrupts that you seein application programs

• Dealing with them can be just as messy. . .

• Issues include race conditions, blocking signals, reentrancy, and more

Steven M. Bellovin February 1, 2006 37