Threads, SMP, and MicroKernels (Chapter4)

Download Report

Transcript Threads, SMP, and MicroKernels (Chapter4)

Threads, SMP, and MicroKernels
• Processes and threads
– The two characteristics of a process
• unit of resource ownership
– virtual address space for the process image
– I/O channels, devices, files
• unit of dispatching/scheduling/execution
– This is the execution path through one or more modules.
– It is also the entity that is being scheduled and dispatched by
the OS.
– A process may have many dispatching units. A unit of
dispatching is commonly called a thread or lightweight
process.
– New notion of Process : unit of resource
ownership
1
Threads
– Single process, single thread
• DOS
– Multiple processes, single thread per process
• UNIX
– One process, multiple threads
• Java run-time (actually not an OS)
– Multiple processes, multiple threads per process
• Solaris, Windows 2000/XP, Windows XP, Mach, OS/2, Linux
– Multithreading
• a process
– unit of protection and unit of resource allocation
» virtual address space, process image
» protected access to processors, interprocess communication (IPC),
files, I/O resources
2
3
4
Threads (cont.)
– Multithreading (cont.)
• Each thread has
– a thread execution state (running, ready, etc.)
– a separate control block, with priority, some thread related state
information, and saved processor context when not running
(with program counter)
– an execution stack
– some per-thread static storage for local variables
– access to the memory and resources of its process, shared with
all other threads in the process,
• A single application logically doing several functions,
especially in GUI systems.
– Example application -- a file server entertaining requests to
create, open, read, and write on files.
» One thread per request
» Two threads cannot write on the same file at the same
time.
5
Benefits of threads
• less time to create (10 times) and terminate than doing
the same thing to a process
• less time to switch between two threads within the
same process
• parallel processing -- multiple threads executing
simultaneously on different processors.
• communication between different executing modules
within the same process
– In most OS, communication between independent processes
requires the intervention of the kernel to provide protection
and the mechanism needed for communication.
– Because threads within the same task share memory and
files, they can communicate with each other without
invoking the kernel.
6
7
8
9
Example applications using threads
• Threads in a spreadsheet program
– One thread displays menus and read user input (foreground work).
– Another thread executes user commands and updates the spreadsheet
(background work).
• Adobe PageMaker
–
–
–
–
–
Writing, design, and production tool for desktop publishing
service thread
event-handling thread
screen-drawing thread
When the event-handling thread (e.g., doing a large computation, etc.)
or the screen-drawing thread is busy, the service thread (e.g., printing,
file importing, etc.) restricts user activity by disabling menu items and
displaying a “busy” cursor. The user is free to switch to other
applications, or even kill the computation through the service thread.
10
Example applications using threads (cont.)
• Adobe PageMaker (cont.)
– Dynamic scrolling
• Redrawing the screen as the user drags the scroll
indicator -- is possible. The event-handling thread
monitors the scroll bar and redraws the margin ruler
(which can be done relatively quickly).
• Meanwhile, the screen-redraw thread constantly tries
to redraw the page and catch up. (Each user click on
the scroll bar aborts the previous drawing and starts a
new one.) In this example, the event-handling thread
can be considered as doing foreground work while
the screen-redraw thread is considered the
background work.
11
Example applications using threads (cont.)
• Asynchronous processing
– Have a backup thread periodically saving the
current user data to disk when the main thread is
doing some computation.
• Speedy execution
– In a multiprocessor system, one thread reads in
data while another thread does the computation.
12
Threads (cont.)
• Because all threads in a task share the same address
space, all threads must enter a suspend state at the
same time.
• Thread functionality
– thread states
• running, ready, blocked
• no suspend state
• thread operations
– spawn, block, unblock, finish
– thread synchronization
• The alteration of one resource by a thread affects other threads.
– e.g., opening files
• same techniques as process synchronization
13
14
User-level threads (ULT)
• thread management done by a threads library at user
mode/space
– thread spawning, destruction, scheduling, message
passing, switching--saving and restoring thread context
– setting state of each thread
• kernel not aware of existence of threads
• Examples in Fig. 4.7
– Fig. 4.7b: thread 2 invokes I/O action.
– Fig. 4.7c: process B’s clock quantum expires.
– Fig. 4.7d: thread 2 needs some action performed by thread
1; thread switching occurs and is managed by threads
library.
15
16
ULT vs KLT
• advantages of ULT over KLT
– Thread switching does not require kernel mode
privileges, i.e., no mode switches.
– Scheduling can be application specific.
• round-robin for one application, priority-based for another
– ULT runs on any OS, even ones not supporting
multithreading.
• disadvantages of ULT
– In a typical OS, many system calls are blocking. When a
ULT executes a system call (I/O, etc.) , all of the threads
within the process are blocked.
– A multithreaded application cannot take advantage of
multiprocessing: the kernel assigns only one CPU to the
process.
17
Kernel-level threads (KLT)
•
•
•
•
also called lightweight processes
thread management done by kernel
Examples: Windows 2000, Linux, OS/2
advantages of KLT
– The kernel can assign multiple CPUs to different
threads of the same process, i.e., true multiprocessing.
– If one thread in a process is blocked, the kernel can
schedule another thread of the same process.
– Kernel routines themselves can be multithreaded.
• disadvantages of KLT
– The transfer of control from one thread to another
within the same process requires a mode switch to the
kernel. This is very time consuming. See table 4.1.
18
Combined ULT and KLT
• example: Solaris
• Thread creation, scheduling, and synchronization
are done completely in user space.
• The multiple ULTs from a single application are
mapped onto some (smaller or equal) number of
KLTs.
• advantages
– Multiple threads within the same application can run in
parallel on multiple CPUs.
– A blocking system call need not block the entire
process.
19
User and Kernel-Level Threads Performance
• Performance
• Null fork: the time to create, schedule, execute, and
complete a process/thread that invokes the null procedure.
• Signal-Wait: the time for a process/thread to signal a
waiting process/thread and then wait on a condition.
• Procedure call: 7 s Kernel Trap:17 s
• Thread Operation Latencies (taken on VAX (1992))
20
User and Kernel-Level Threads Performance
• Observations
– While there is a significant speedup by using KLT
multithreading compared to single-threaded processes,
there is an additional significant speedup by using ULTs.
– However, whether or not the additional speedup is
realized depends on the nature of the applications
involved.
– If most of the thread switches require kernel mode
access, then ULT may not perform much better than
KLT.
21
Symmetric multiprocessing
• SISD (Single instruction, single data stream)
• SIMD (multiple data stream)
– vector and array processors
• MIMD (multiple instruction, multiple data stream)
– general purpose processors, each capable of processing any
instruction
– distributed memory (loosely coupled)
– shared memory (tightly coupled)
• master/slave
– The OS kernel always runs on a particular processor (master).
– relatively simple OS (compared to SMP)
– Disadvantages: single point of failure, bottleneck
22
23
Symmetric multiprocessing (cont.)
– shared memory (tightly coupled) (cont.)
• symmetric (SMP)
– kernel executed as multiple processes or threads
– Each processor may execute these kernel threads; self
scheduling.
» Complicated OS : synchronization, conflict
resolution, etc.
• SMP organization
– shared memory, bus, I/O subsystem
– separate cache : cache coherence problem
24
25
Multiprocessor OS design considerations
• simultaneous concurrent processes or threads
– reentrant code for kernel routines
– no deadlock for kernel tables and management
structures
• scheduling
• synchronization
– mutual exclusion, locks, event ordering
• memory management
– coordinate paging of processor/cache couple
• reliability
– graceful degradation during processor failure
26
Microkernels
• microkernel architecture
– Old OS’s are big
• IBM OS/360: 5000 programmers, 1 million lines, 5 years
• Multics: 20 million lines
• Difficulty of maintenance
– New OS’s: object-oriented architecture
– absolutely essential core OS functions
• kernel (supervisor) mode
– external subsystems
• built on the microkernel
• executed in user mode as server processes (that are part of the
OS)
– interaction via message passing through microkernel
• device drivers, file systems, virtual memory manager,
windowing system, security services
27
28
Benefits of a microkernel organization
• uniform interface
– no distinction between user-level and kernel-level services: all done
by message passing
• extensibility
– e.g., addition of new types of disks
• flexibility
– easiness of modification to adapt to different environment
– addition/deletion of services for different types of users
• portability
– minimal effort to port to different machines
• reliability
– microkernel can be rigorously tested because of its small size; small
number of API library functions
• distributed system support
– A process can send a message (with service provider ID) without
knowing on which machine the target service resides.
29
• Object-oriented OS
30
Microkernels (cont.)
• Performance of microkernel
– It takes longer to build and send messages than to make
supervisor calls.
– more user/kernel mode switch than traditional OS
• Microkernel design
– no absolute rules on services included
– hardware dependent functions
– functions needed to support servers and applications running
in user mode
– Low-level memory management
• mapping a virtual page to a physical page frame
• outside microkernel
– protection of address space of one process from another at process level
– page replacement algorithm
– application-specific memory sharing policies
31
Microkernels (cont.)
• Microkernel design (cont.)
– Interprocess communication (IPC)
• messages
– header (with sender and receiver ID), data
– between threads: send location of data
– between processes: memory-to-memory copy
• The microkernel maintains ports where queues of messages are
associated; ports also indicate which other processes can
communicate to them.
• Ports are assigned to processes for IPC.
– I/O and interrupt management
•
•
•
•
I/O ports address space
recognition of interrupts
only assignment of interrupts to certain interrupt handlers
The interrupt handlers are external to the microkernel.
32
33
34
35
36
37
38
39
40
41