2 threads smp microkernels

36
1 Threads, SMP, and Microkernels

Upload: suresh-kumar

Post on 30-Jan-2016

232 views

Category:

Documents


0 download

DESCRIPTION

Operating system notes

TRANSCRIPT

Page 1: 2 Threads SMP Microkernels

1

Threads, SMP, and Microkernels

Page 2: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 2

Processes vs. Threads• Traditional process characteristics:

– Resource ownership• Has a virtual address space to hold the process image

• Is allocated resources, as needed: I/O devices, files, etc.

– Scheduling/execution• Follows an execution path through one or more programs

• Has an execution state and a priority

• Is the entity that is scheduled and dispatched by the OS

• Modern OSs treat these characteristics independently– Unit of resource of ownership is referred to as a process or

task

– Unit of dispatching is referred to as a thread or lightweight process

Page 3: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 3

Multithreading• The OS supports multiple threads of execution within a single process• A process in a multithreaded environment has:

– A virtual address space that holds the process image– Protected access to processors, other processes, files, and I/O resources

• Within a process, there may be one or more threads and each thread has:– A thread execution state (Running, Ready, etc.)– A saved thread context when not running– An execution stack– Per-thread static storage for local variables– Access to the memory and resources of its process, shared with other threads

• Examples: W2K, Solaris, Linux, Mach, OS/2

• MS-DOS supports a single thread• UNIX supports multiple user processes but only supports one thread per process• Windows 2000, Solaris, Linux, Mach, and OS/2 support multiple threads

Page 4: 2 Threads SMP Microkernels
Page 5: 2 Threads SMP Microkernels
Page 6: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 6

Multithreading (Cont.)• Benefits of threads:

– Takes less time to create a new thread in an existing process than a new process

– Takes less time to terminate a thread than a process

– Takes less time to switch between two threads within the same process

– Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel

• Actions that affect all threads of a process– Suspending a process involves suspending all threads of the process (a

suspended process is swapped out of main memory)

– Termination of a process, terminates all threads within the process

Page 7: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 7

Examples of use of Threads in a Single-User Multiprocessing System

• Foreground and background work– Example: spreadsheet program

• One thread displays menus and reads user input

• Other thread executes user commands and updates spreadsheet

• Asynchronous processing– Example: word processor that saves to disk once every minute

• A second thread does periodic backups

• Speed execution– Example:

• One thread computes one batch of data

• Other thread reads next batch from a device

• Modular program structure– Example: programs that use a variety of sources and destinations

Page 8: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 8

Thread States and Operations• Thread states:

– Running, Ready, and Blocked

• Operations associated with a change in thread state– Spawn

• Spawn a thread for a new process

• A thread in an existing process spawns another thread

– Block• Thread blocks waiting on an event

– Unblock• Event occurred, thread is unblocked

– Finish• Thread completes, deallocate register context and stacks

Page 9: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 9

Remote Procedure Call Using Threads

Page 10: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 10

Remote Procedure Call Using Threads (Cont.)

Page 11: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 11

User-Level Threads (ULT)• All thread management is done by the application, the kernel is

not aware of the existence of threads

• An application is programmed by using a threads library, which contains code for:– Creating and destroying threads

– Passing messages and data between the threads

– Scheduling thread execution

– Saving and restoring thread contexts

• Operation:– Application starts as a single thread running within a process managed by

kernel

– Application spawns a new thread using Spawn in threads library

– Threads library creates new thread and passes control to one of the threads in Ready state using some scheduling algorithm

Page 12: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 12

User-Level Threads (Cont.)

• Advantages of using User Level Threads instead of Kernel Level Threads– Thread switching does not require kernel mode privileges

– Scheduling can be application specific

– User Level Threads can run on any OS

• Disadvantages of User Level Threads:– Many system calls are blocking: when a ULT executes a

system call, all threads within the process are blocked

– A multithreaded application cannot take advantage of multiprocessing: kernel assigns one processor to one process (I.e, all threads within that process)

Page 13: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 13

Kernel-Level Threads (KLT)• In a pure Kernel-Level Thread implementation all thread

management is done by the kernel• All the threads within an application are in a single process• Kernel maintains context information for the process and the

threads• Scheduling is done on a thread basis• Advantages:

– Kernel can simultaneously schedule multiple threads from the same process on multiple processors

– If one thread is blocked, the kernel can schedule another thread of the same process

– The kernel routines themselves can be multithreaded

• Disadvantages:– Transfer of control between threads within same process requires switch to

the kernel

• Examples: W2K, Linux, and OS/2

Page 14: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 14

Combined Approaches

• Multiple ULTs from a single application are mapped onto some (smaller or equal) number of KLTs

• Thread creation done in the user space• Most of scheduling and synchronization of threads

within an application are done in the user space• Advantages:

– Multiple threads within the same application can run in parallel on multiple processors

– A blocking system call does not block the entire process

• Example: Solaris

Page 15: 2 Threads SMP Microkernels
Page 16: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 16

Relationship Between Threads and ProcessesThreads:Process Description Example Systems

1:1 Each thread of execution is aunique process with its ownaddress space and resources.

Traditional UNIX implementations

M:1A process defines an addressspace and dynamic resourceownership. Multiple threadsmay be created and executedwithin that process.

Windows NT, Solaris, OS/2,OS/390, MACH

1:M A thread may migrate from one process environment to another. This allows a thread to be easily moved among distinct systems.

Ra (Clouds), Emerald

M:M Combines attributes of M:1 and 1:M cases

TRIX

Page 17: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 17

Symmetric MultiProcessing (SMP)• Categories of Computer Systems

– Single Instruction Single Data (SISD)• Single processor executes a single instruction stream to operate on data

stored in a single memory

– Single Instruction Multiple Data (SIMD)• Each instruction is executed on a different set of data by the different

processors

– Multiple Instruction Single Data (MISD)• A sequence of data is transmitted to a set of processors, each of which

executes a different instruction sequence. Never implemented

– Multiple Instruction Multiple Data (MIMD)• A set of processors simultaneously execute different instruction

sequences on different data sets

Page 18: 2 Threads SMP Microkernels
Page 19: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 19

Symmetric MultiProcessing (Cont.)• OS implementation

– Kernel can execute on any processor

– Typically each processor does self-scheduling from the pool of available processes or threads

– Kernel can be constructed as multiple processes or multiple threads that can execute in parallel

• OS must prevent– Two processors choosing the same process

– A process getting lost from the queue

Page 20: 2 Threads SMP Microkernels
Page 21: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 21

Multiprocessor Operating System Design Issues• Simultaneous concurrent processes or threads

– Several processors can execute the same kernel code simultaneously: kernel routines must be reentrant

– Management of kernel tables must avoid deadlock

• Scheduling– With scheduling done by each processor, kernel-level multithreading must

avoid conflicts in scheduling multiple threads from the same process simultaneously

• Synchronization– Mutual exclusion and event ordering must by provided for multiple active

processes accessing shared memory and I/O

• Memory Management– Paging on different processors must be coordinated when several

processors share a page or segment and decide on page replacement

• Reliability and Fault Tolerance– Graceful degradation in case of processor failure

Page 22: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 22

Microkernels• Microkernel is a small operating system core containing only

essential operating systems functions• All other OS services are implemented as server processes,

execute in user mode, and communicate through messages (device drivers, file systems, virtual memory manager, windowing system, and security services)

• The microkernel architecture replaces the traditional vertical layered architecture and leads to a client/server architecture within a single computer– Example:

• An application opens a file by sending a message to the file system server, creates a process or thread by sending a message to the process server

• Each of the servers can send messages to other servers (through the kernel) and can invoke the primitive operations in the kernel

Page 23: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 23

Benefits of a Microkernel Organization• Uniform interface on requests made by a process

– Both kernel-level and user-level services are provided by means of message passing

• Extensibility– Allows the addition of new services and provision of multiple services in

the same functional area

• Flexibility– New features added, existing features can be subtracted

• Portability– Most of the processor-specific code is in the microkernel

• Reliability– A small microkernel can be rigorously tested

• Distributed system support, including clusters controlled by a distributed operating system– The client/server architecture within a single computer maps easily into a

distributed architecture

Page 24: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 24

Windows 2000 (W2K) Thread and SMP Management

• Process design driven by the need to provide support for a variety of operating system environments

• Native process structures and services are relatively simple and general purpose, allowing each operating system subsystem to emulate a particular process structure and functionality

• Process characteristics– Processes implemented as objects

– An executable process may contain one or more threads

– Both process and thread objects have built-in synchronization capabilities

Page 25: 2 Threads SMP Microkernels
Page 26: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 26

W2K Process Object

Page 27: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 27

W2K Thread Object

Page 28: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 28

W2K Thread States• Ready:

– May be scheduled for execution

• Standby: – Has been selected to run on a particular processor and waits until processor

is available

• Running– Has been allocated a processor and is executing

• Waiting– Blocked on an event or waits for synchronization purposes

• Transition– Ready to run but resources not available

• Terminated– Terminated by itself, by another thread, or when the parent process

terminates

Page 29: 2 Threads SMP Microkernels
Page 30: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 30

W2K SMP Support

• The threads of any process, including those of the executive, can run on any processor

• Microkernel assigns a ready thread to the next available processor, except for– Soft affinity: dispatcher tries to assign a ready thread to the

same processor it last ran on– Hard affinity: application can restrict its thread execution to

certain processors

• Benefits– No processor is idle or is executing a lower-priority thread

when a higher priority thread is ready– Multiple threads from the same process can be executing

simultaneously on multiple processors

Page 31: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 31

Solaris Thread Management• Four separate thread-related concepts:

– Process: • Normal UNIX process, includes the user’s address space, stack, and

process control block

– User-Level Threads (ULTs) • Implemented through a threads library in the address space of a

process• Invisible to the OS

– Lightweight processes (LWPs)• A mapping between ULTs and kernel threads• Each LWP supports one or more ULTs and maps to one kernel thread• Scheduled by the kernel independently• May execute in parallel on multiprocessors

– Kernel threads• Fundamental entities that can be scheduled and dispatched to run on

one of the system processors

Page 32: 2 Threads SMP Microkernels
Page 33: 2 Threads SMP Microkernels
Page 34: 2 Threads SMP Microkernels
Page 35: 2 Threads SMP Microkernels

CS-550: Threads, SMP, and Microkernels 35

Linux Process and Thread Management• A process or task in Linux is represented by a task_struct data

structure. It contains the following information about the process:– State: executing, ready, suspended, stopped, and zombie

– Scheduling information: a process can be normal or real-time and has a priority

– Identifiers: unique process identifier, and user and group identifiers

– Interprocess communication: IPC mechanisms

– Links: a link to its parent process, links to its siblings, and links to all its children

– Times and timers: process creation time and the amount of processor time so far consumed by the process

– File system: pointers to any files open by this process

– Virtual memory: virtual memory assigned to this process

– Processor-specific context: registers and stack information that represent the context of this process

Page 36: 2 Threads SMP Microkernels