Download presentation
Presentation is loading. Please wait.
1
Threads, SMP, and Microkernels Chapter 4
2
Two Characteristics of Processes (1) Resource ownership zA process is allocated a virtual address space to hold the process image zA process, from time to time, may be assigned main memory plus control of other resources, such as I/O channels, I/O devices, and files.
3
Two Characteristics of Processes (2) Dispatching zA process is an execution path (trace) through one or more programs. yexecution may be interleaved with other processes (parent process and child process) zA process has an execution state (Running, Ready, Blocked, Blocked Suspend, Read Suspend)
4
Dispatching and Thread zThese two characteristics are treated independently by the operating system zResource of ownership is referred to as a process or task zDispatching is referred to as a thread
5
Thread zA thread is a piece of program code executed in a serial fashion. zA thread is a dispatchable of work. It executes sequentially and is interruptable so that the processor can turn to another thread. zA process is a collection of one or more threads and associated system resources.
6
Multithreading zMultithreading Operating system supports multiple threads of execution within a single process
7
Threads and Processes one process one thread multiple processes one thread per process one process multiple threads multiple processes multiple threads per process
8
Single Threaded and Multithreaded Process Models Thread Control Block User Stack User Stack Kernel Stack Kernel Stack User Address Space User Address Space Process Control Block Process Control Block Thread Single-Threaded Process Model Multithreaded Process Model Thread Control Block User Stack Kernel Stack Thread Control Block User Stack Kernel Stack Thread
9
Relationship Between Threads and Processes Threads:ProcessDescriptionExample Systems 1:1Each thread of execution is a unique process with its own address space and resources. Most UNIX implementations M:1 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
10
Relationship Between Threads and Processes Threads:ProcessDescriptionExample Systems 1:MA thread may migrate from one process environment to another. This allows a thread to be easily moved among distinct systems. Ra (Clouds), Emerald M:MCombines attributes of M:1 and 1:M cases TRIX
11
Examples of threading & Multithreading zMS-DOS supports a single process and a single thread. zJava support a single process with multiple threads zUNIX supports multiple user processes but only supports one thread per process zSolaris and Windows NT supports multiple user processes, each of which supports multiple threads
12
fork System Call 1. Syntax of fork System Call Include File(s): Summary: pid_t fork (void); Return Success : 0 in child, child PID in parent Failure: -1 Set errno: yes 2. Function of fork System Call When a fork system call is made, the operating system generates a copy of the parent process which becomes the child process. Process Dr. Ming Zhang
13
thr_create API (Application Program Interface) (1) 1. The thr_create Function Prototype Include File: int thr_create(void* stackp, size_t stack_size, void* (*funcp)(void*), void* argp, long flags, thread_t* tid_p); 2. Function of thr_create( ) The function creates a new thread to execute a function whose address is given in the funcp argument. Dr. Ming Zhang
14
thr_create API (2) 3. Return Success : 1 Failure: 0 4. void* stackp The address of a user-defined memory region. This memory is used as the new thread run- time stack. If the stackp value is NULL, the function allocates a stack region of stack_size bytes. 5 size_t stack_size The size of a user-defined memory region. This memory is used as the new thread run-time stack. If the stack_size value is 0, the function uses a system default value that is one megabyte. Dr. Ming Zhang
15
thr_create API (3) 6. void* (*funcp) (void*) The function specified in funcp should accept one void*-typed input argument and return void* data. 7. void* argp The actual argument to be passed to the funcp function, when the new thread starts executing, is specified in the argp argument. Dr. Ming Zhang
16
thr_create API (4) 8. long flags The flags argument value may be zero, meaning that no special attributes are assigned to the new thread. The flags value may using one or more of the following bit-flags: THR_SUSPENDED: suspends the execution of the new thread until another thread calls the thr_continue function to enable it to execute THR_NEW_LWP: Create a new LWP (LightWeight Process) along with the new thread. Dr. Ming Zhang
17
thr_create API (5) 9. thread_t* tid_p); The new thread ID is return via the tid_p argument. If the actual value of the tid_p argument is assigned NULL, no thread ID is returned. The thread ID data type is thread_t. Dr. Ming Zhang
18
thr_create API (6 ) # include........ thread_t tid; //for return thread ID void* send_msg(void* ptr);// function executed by thread MSGREC * pREC ; // MSGRC: struct name int main ( ) {.................. thr_create( 0, 0, send_msg, pRec, THR_SUSPENDD, &tid);................} Dr. Ming Zhang
19
Threads differ from Chile Processes (1) zThreads may be managed by either user- level library functions or the operating system kernel. zChild processes as created by the fork system call are managed by the operating system kernel
20
Threads differ from Chile Processes (2) zAll threads in a process share the same data and code segment. zA child process has its own copy of virtual address space that is separate from its parent process.
21
Threads differ from Chile Processes (3) zIf a thread calls the exit or exec function, it terminates all the threads in the same process. zIf a child process calls the exit or exec function, its parent process is not affected.
22
Threads differ from Chile Processes (4) zIf a thread modifies a global variable in a process, the changes are visible to other threads in the same process. zIf a child process modifies a global variable. the changes are NOT visible to the parent process.
23
Benefits of Threads (1) zThreads is more efficient, and require much less kernel attention, to create and manage than do child process, because threads are managed by user-level library function and kernel. zThreads use much less system resources than do child process, because thread can share the data in same process.
24
Benefits of Threads (2) zTakes less time to create a new thread than a process zLess time to terminate a thread than a process zLess time to switch between two threads within the same process zSince threads within the same process share memory and files, they can communicate with each other without invoking the kernel
25
Thread Structure zA thread ID zA run-time stack zA set of register (e.g., program counter and stack pointer) zA signal mask zA schedule priority zA thread-specific storage
26
Thread Features (1) zHas an execution state (running, ready, etc.) zSaves thread context when not running zHas an execution stack zHas some per-thread static storage for local variables zHas access to the memory and resources of its process yall threads of a process share this
27
Thread Features (2) zSuspending a process involves suspending all threads of the process since all threads share the same address space zTermination of a process, terminates all threads within the process
28
Synchronization of Using Threads zSome synchronization methods are needed for threads accessing shared data, because if a thread modifies a global variable in a process, the changes are visible to other threads in the same process.
29
Benefits of Using Multithreaded Programming zIt allows a process to make use of any available multiprocessor hardware on any system it is run on. zIt allows programmers to structure their code into independently executable units and maximize concurrency. zThreads reduce the need to use fork to create child processes, thus improving each process performance (less context switching).
30
User-Level Threads zAll thread management is done by the application zThe kernel is not aware of the existence of threads zThread switching does not require kernel mode privileges zScheduling is application specific
31
Kernel-Level Threads zThere is no thread management code in the application area, simply an application programming interface (API) to the kernel facility. (Windows NT) zKernel maintains context information for the process and the threads zSwitching between threads requires the kernel
32
Combined Approaches for Threads zSolaris operating systems provide a combined ULT/KLT facility zThread creation is done completely in the user space, as is the bulk of scheduling and synchronization of threads within an application. zThe multiple ULTs from a single application are mapped onto smaller or equal number KLTs.
33
Solaris Multithreaded Architecture process 1 process 2 process 3 User L L L L L Kernel Hardware P P P L: Lightweight Process; P: Processor; :ULT; : KLT
34
Symmetric Multiprocessing zMIMD A set of processors simultaneously execute different instruction sequences on different data sets zShared memory multiprocessor (Distributed Memory MIMD is clusters) zKernel can execute on any processor zTypically each processor does self- scheduling form the pool of available process or threads
35
Symmetric Multiprocessor Organization Main Memory Processor Cache I/O Subsystem...
36
Categories of Computer Systems (1) zSingle Instruction Single Data (SISD) ysingle processor executes a single instruction stream to operate on data stored in a single memory zSingle Instruction Multiple Data (SIMD) yone instruction is executed on a different set of data by the different processors
37
Categories of Computer Systems (2) zMultiple Instruction Single Data (MISD) ya sequence of data is transmitted to a set of processors, each of which executes a different instruction sequence. zMultiple Instruction Multiple Data (MIMD) ya set of processors simultaneously execute different instruction sequences on different data sets
38
Microkernel zSmall operating system core zContains only essential operating systems functions zMany services traditionally included in the operating system are now external subsystems ydevice drivers yfile systems yvirtual memory manager ywindowing system and security services
39
Benefits of a Microkernel Organization (1) zUniform interface on request made by a process yall services are provided by means of message passing zExtensibility yallows the addition of new services zFlexibility yexisting features can be subtracted
40
Benefits of a Microkernel Organization (2) zPortability ychanges needed to port the system to a new processor is changed in the microkernel - not in the other services zReliability ymodular design ysmall microkernel can be rigorously tested
41
Benefits of Microkernel Organization (3) zDistributed system support ymessage are sent without knowing what the target machine is zObject-oriented operating system ycomponents are objects with clearly defined interfaces that can be interconnected to form software
42
Solaris zProcess includes the user’s address space, stack, and process control block zUser-level threads zLightweight processes A lightweight process can be viewed as a mapping between ULTs and KLTs. zKernel threads
43
Solaris User Level Threads Stop Sleep Dispatch Stop Wakeup Continue Preempt Stopped Runnable Active Sleeping
44
Solaris Lightweight Processes Timeslice or Preempt Wakeup Stop Blocking System Call Wakeup Dispatch Runnable Running Blocked Stopped Continue Stop
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.