processes & threads introduction to operating systems: module 5

25
Processes & Threads Introduction to Operating Systems: Module 5

Upload: augustus-fox

Post on 19-Jan-2016

264 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Processes & Threads Introduction to Operating Systems: Module 5

Processes & Threads

Introduction to Operating Systems: Module 5

Page 2: Processes & Threads Introduction to Operating Systems: Module 5

CPU scheduling queue

Enqueuer ReadyQueue

DispatcherContextSwitcher

Ready ProcessReady Process PCBs

CPUCPU

Remove the Running ProcessFromOtherStates

Page 3: Processes & Threads Introduction to Operating Systems: Module 5

Schedulers Short-term scheduler (STS)

Selects a process from ready queue and give it CPU (dispatch) Determine if the running process should be preempted (preempt)

Medium-term scheduler (MTS) When needed, chooses ready processes to be saved to disk

(suspend), or restored from disk (activate) Long-term scheduler (LTS)

Initiates process (activate) LTS and MTS determine the degree of multiprogramming

Processes may be either I/O-bound or CPU-bound want to keep a good mix of each type of process to maximize

resource utilization

Page 4: Processes & Threads Introduction to Operating Systems: Module 5

join

Process queues

ready queue cpu

I/O queuei/o I/O request

time sliceexpires

fork achild

resourcerequestresource queue

condition queue

childexecutes

terminated

Page 5: Processes & Threads Introduction to Operating Systems: Module 5

Queues as linked lists of PCBs

RunningReadyDisk1Disk2Printer

Page 6: Processes & Threads Introduction to Operating Systems: Module 5

Process switching

Switching from one process to another Often tens of microseconds (must be fast!) Increases utilization of CPU

I/O and processing in parallel incurs minimal overhead CPU may have a process switch instruction

Page 7: Processes & Threads Introduction to Operating Systems: Module 5

Process switching

A process switch may occur whenever the OS is invoked system call

explicit request by the program, such as open file the process may be blocked

• If so, OS will dispatch a new process

Trap (non-system call) an error resulted from the last instruction

• may cause the process to be moved to the terminate state

Interrupt the cause is external to the execution of the current instruction control is transferred to the exception handler After servicing the exception, a new process may be dispatched

Page 8: Processes & Threads Introduction to Operating Systems: Module 5

Process switching interrupts

Clock process uses all of its time slice

the exception handler will preempt the process

I/O an I/O device has completed a transfer

wakeup processes waiting for this event and resume interrupted process, or

preempt interrupted process and dispatch a ready process with higher priority

Page 9: Processes & Threads Introduction to Operating Systems: Module 5

Mode switching

Not all interrupts entail process switching control can just return to the interrupted program only processor state information needs to be saved

This is called mode switching move from user mode to protected mode

Less overhead than process switching no need to update PCB

Page 10: Processes & Threads Introduction to Operating Systems: Module 5

Stop the current process (process A) Save enough state information (or context) so that

process A can be restarted later Select a ready process (process B) Load B’s state

memory mapping info, program counter, general registers, open file table (pointer), etc.

(Re)start B

Process switching steps

Page 11: Processes & Threads Introduction to Operating Systems: Module 5

Threads A sequential execution stream within a process

sometimes called a lightweight process (LWP)

The major advantages of threads low cost of thread switching easy mechanism for shared resources easily take advantage of multiprocessor system

The major disadvantages harder to debug unneeded overhead if threads aren’t used

Page 12: Processes & Threads Introduction to Operating Systems: Module 5

Threads Threads within a process (or task) share

text segment data segment OS resources (open files and signals)

Each thread has its own program counter register set stack space

Page 13: Processes & Threads Introduction to Operating Systems: Module 5

A Process (kernel view)

Kernel Support

ProgramText

Data

Process Status Resources

Allocate resources to processes when they are needed

Page 14: Processes & Threads Introduction to Operating Systems: Module 5

A task and its family of threads

ProgramText

Process Status

Global data

Thread Status

Stack

Thread

ResourcesResourcesResources

Task (Process)

Program counters

Page 15: Processes & Threads Introduction to Operating Systems: Module 5

Why threads become popular now?

SMPs (Symmetric Multiprocessors) 2 to 128 processors sharing

System bus I/O system Main memory

One operating system for all processors

Page 16: Processes & Threads Introduction to Operating Systems: Module 5

Three types of thread systems

Kernel-supported threads (Mach, OS/2, NT) User-level threads; supported above the kernel, via a

set of library calls at the user level Hybrid approach implements both user-level and

kernel-supported threads (Solaris)

Page 17: Processes & Threads Introduction to Operating Systems: Module 5

A simple view

Thread 0

Thread 1

Thread 3

Thread 2

Thread run time libraries

Kernel (see process)

User-level

Kernel (see thread)

Kernel-level

System call

A User Program A User Program

Thread 0

Thread 1

Thread 3

Thread 2

Page 18: Processes & Threads Introduction to Operating Systems: Module 5

Kernel-level versus User-level threads

User-level thread User-level activities; no kernel involvement Basic scheduling unit in OS is process Threads of the same process can not run on different CPUs in

SMP in parallel

Kernel-level thread Each process consists of several threads Basic scheduling unit is thread Can run on different CPUs in SMP in parallel

Page 19: Processes & Threads Introduction to Operating Systems: Module 5

Advantages of kernel threads

Higher application throughput if there were no kernel thread support

need I/O means the process goes into waiting state and wait until the I/O is complete

with multiple kernel threads per task Block the I/O requesting thread and continue to work on another thread Increases the overall throughput of the application

Page 20: Processes & Threads Introduction to Operating Systems: Module 5

Advantages of user level threads

Threads are cheap can be implemented at user levels, no kernel resources

Threads are fast no system calls, switching modes involved

Page 21: Processes & Threads Introduction to Operating Systems: Module 5

Using Threads - Windowing System

Window Threads

Application

Minimized thread switching timeBetter response time

Page 22: Processes & Threads Introduction to Operating Systems: Module 5

Other Examples

Robot control: single program, multiple concurrent operations

Airline reservations: one thread per customer thread per task

Network server: single program, must handle concurrent requests from multiple users (examples: Web server) thread pool

Page 23: Processes & Threads Introduction to Operating Systems: Module 5

Sun Solaris 2

Mixed approach OS schedules light-weight process (LWP) User-level library schedules user-level threads

User threads are cheap, can be thousands per task Each LWP supports one or more user threads

LWPs are what we’ve been calling kernel threads Solaris has entities called kernel threads; they are

scheduling artifacts contained in the OS

Page 24: Processes & Threads Introduction to Operating Systems: Module 5

Sun Solaris 2 (Mixed)

Kernel thread

Task 1 Task 2 Task 3

CPU

KERNEL

Light weightprocess (LWP)

User-level thread

CPU CPU CPU

Page 25: Processes & Threads Introduction to Operating Systems: Module 5

Examples of threads packages

POSIX-style threads: OSF/DCE, Chorus threads, POSIX P1003.4a pthreads SunOS Multi-Thread Architecture (Solaris 2) IBM AIX 4.x, SCO UnixWare 2.0

Microsoft-Style threads: WIN32 threads (Window95, NT) OS/2 threads (IBM OS/2)

Others: C Threads in Mach OS (now part of Macintosh OS X)