360 likes | 652 Views
Threads, SMP, and Microkernels. 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
E N D
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
Remote Procedure Call Using Threads CS-550: Threads, SMP, and Microkernels
Remote Procedure Call Using Threads (Cont.) CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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) CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
Relationship Between Threads and Processes Threads:Process Description Example Systems Each thread of execution is a unique process with its own address space and resources. 1:1 Traditional UNIX implementations A process defines an address space and dynamic resource ownership. Multiple threads may be created and executed within that process. Windows NT, Solaris, OS/2, OS/390, MACH M:1 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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
W2K Process Object CS-550: Threads, SMP, and Microkernels
W2K Thread Object CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels
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 CS-550: Threads, SMP, and Microkernels