Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads, SMP, and Microkernels Chapter 4. 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads.

Similar presentations


Presentation on theme: "Threads, SMP, and Microkernels Chapter 4. 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads."— Presentation transcript:

1 Threads, SMP, and Microkernels Chapter 4

2 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads

3 3 Process Characteristics n Unit of resource ownership - process is allocated: u a virtual address space to hold the process image u control of some resources (files, I/O devices...) n Unit of scheduling/execution - process is an execution path through one or more programs u execution may be interleaved with other process(es) u the process has an execution state and a dispatching priority

4 4 Process Characteristics – New Concept n These two characteristics are treated independently by some recent OS n The unit of dispatching is usually referred to a thread or a lightweight process n The unit of resource ownership is usually referred to as a process or task

5 5 Multithreading vs. Single threading n Multithreading: when the OS supports multiple threads of execution within a single process n Single threading: when the OS does not recognize the concept of thread n MS-DOS supports a single user process and a single thread n Traditional UNIX supports multiple user processes but only supports one thread per process n Solaris supports multiple threads

6 6 Threads and Processes

7 7 Processes n Have a virtual address space which holds the process image n Protected access to processors, other processes, files, and I/O resources n Have process-specific context u ID u State u Other attributes

8 8 Threads n What properties do threads have? n Have an execution state (running, ready, etc.) n Save thread context when not running n Have an execution stack and some per- thread static storage for local variables n Have access to the memory address space and resources of its process u all threads of a process share this u when one thread alters a (non-private) memory item, all other threads (of the process) sees that u a file open with one thread, is available to others

9 9 Single Threaded and Multithreaded Process Models Thread Control Block contains a register image, thread priority and thread state information

10 10 Benefits of Threads vs Processes Take less time to: n create a new thread than a process n terminate a thread than a process n switch between two threads within the same process n communicate with each other (via shared resources) u without invoking the kernel

11 11 Thread use in a Single-User System n Foreground and background work n Asynchronous processing n Speed of execution n Modular program structure

12 12 Applications of Threads n Example: a file server on a LAN n It needs to handle several file requests over a short period n Hence more efficient to create (and destroy) a single thread for each request n On a SMP machine: multiple threads can possibly be executing simultaneously on different processors n Example 2: one thread displays menu and reads user input while the other thread executes user commands

13 13 Application Benefits of Threads n Consider an application that consists of several independent parts that do not need to run in sequence n Each part can be implemented as a thread n Whenever one thread is blocked waiting for an I/O, execution could possibly switch to another thread of the same application (instead of switching to another process) u Thread switching is faster than process switching

14 14 Benefits and Challenges of Threads n Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel n Therefore necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data (to be discussed later)

15 15 Example of inconsistent view n 3 variables: A, B, C which are shared by thread T1 and thread T2 n T1 computes C = A+B n T2 transfers amount X from A to B, e.g., u T2 must do: A = A -X and B = B+X n What are the possible execution scenarios? n If T1 computes A+B after T2 has done A = A-X but before B = B+X u T1 will not obtain the correct result for C = A + B n Programmer’s responsibility to ensure program correctness u Race conditions could be difficult to identify and debug

16 16 Threads States and Actions n Three key states: running, ready, blocked n Several actions that affect all of the threads in a process u The OS must manage these at the process level. n They have no suspend state because all threads within the same process share the same address space n Suspending (i.e., swapping) a single thread involves suspending all threads of the same process (logically) n Termination of a process, terminates all threads within the process

17 17 Thread Operations n Operations associated with a change in thread state u Spawn (create another thread) u Block F Issue: will blocking a thread block other, or all, threads u Unblock u Finish (thread) F Deallocate register context and stacks

18 18 Example: Remote Procedure Call n Consider: u A program that performs two remote procedure calls (RPCs) u to two different hosts u to obtain a combined result.

19 19 RPC Using Single Thread

20 20 RPC Using One Thread per Server

21 21 Multithreading on a Uniprocessor

22 22 Adobe PageMaker

23 23 Categories of Thread Implementation n User Level Thread (ULT) n Kernel level Thread (KLT) also called: u kernel-supported threads u lightweight processes.

24 24 User-Level Threads (ULT) n The kernel is not aware of the existence of threads n All thread management is done by the application by using a thread library n Thread switching does not require kernel mode privileges (no mode switching) n Scheduling is application specific

25 25 Threads library n Contains code for: u creating and destroying threads u passing messages and data between threads u scheduling thread execution u saving and restoring thread contexts

26 26 Relationships between ULT Thread and Process Scheduling and States

27 27 Kernel activity for ULTs n The kernel is not aware of thread activity but it is still managing process activity n When a thread makes a system call, the whole process will be blocked n but for the thread library that thread is still in the running state n So thread states are independent of process states

28 28 Advantages and inconveniences of ULT n Advantages u Thread switching does not involve the kernel: no mode switching u Scheduling can be application specific: choose the best algorithm. u ULTs can run on any OS. Only needs a thread library n Inconveniences u Many system calls are blocking and the kernel blocks processes. So all threads within the process will be blocked u The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors.

29 29 Kernel-Level Threads (KLT) n All thread management is done by kernel n No thread library but an API to the kernel thread facility n Kernel maintains context information for the process and the threads n Switching between threads requires the kernel n Scheduling on a thread basis n Ex: Windows NT and OS/2

30 30 Advantages and inconveniences of KLT n Advantages u the kernel can simultaneously schedule many threads of the same process on many processors u blocking is done on a thread level u kernel routines can be multithreaded n Inconveniences u thread switching within the same process involves the kernel. We have 2 mode switches per thread switch u this results in a slow down

31 31 Combined ULT/KLT Approaches n Thread creation done in the user space n Bulk of scheduling and synchronization of threads done in the user space n The programmer may adjust the number of KLTs n May combine the best of both approaches n Example is Solaris n NOTE: modern threads are “smart”

32 32 Thread and Process Operation Latencies: an Example OperationUser-Level Threads Kernel- Level Threads Processes Null Fork3494811,300 Signal-Wait374411,840

33 Review Quiz 4-1 True or False? 1. Processes generally are faster than threads, because processes have all the resources. 2. Thread switching for user-level threads generally are faster than that of kernel-level threads. 3. Threads generally are more reliable than processes. Which of the following programs could be higher performance using multiple threads? Program 1 … read message 1 from server A; // blocking call read message 2 from server B; // blocking call compare message 1 and message 2 …. 33

34 Review Quiz 4-1 Program 2: …. for (i = 0; i < 1000000; i++) { list [i] = i; } Program 3: … for (i = 1; i < 1000000; i++) { list [i] = list [i – 1] + i; } 34

35 35 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux

36 36 Traditional View n Traditionally, the computer has been viewed as a sequential machine. u A processor executes instructions one at a time in sequence u Each instruction is a sequence of operations n Two popular approaches to providing parallelism u Symmetric MultiProcessors (SMPs) u Clusters (ch 16)

37 37 Categories of Computer Systems n Based on Instruction and data streams: n Single Instruction Single Data (SISD) stream u Single processor executes a single instruction stream to operate on data stored in a single memory n Single Instruction Multiple Data (SIMD) stream u Each instruction (same instruction) is executed on a different set of data by the different processors

38 38 Categories of Computer Systems Multiple Instruction Single Data (MISD) stream (Never implemented) F A sequence of data is transmitted to a set of processors, each of execute a different instruction sequence n Multiple Instruction Multiple Data (MIMD) F A set of processors simultaneously execute different instruction sequences on different data sets

39 39 Parallel Processor Architectures

40 40 Symmetric Multiprocessing n Kernel can execute on any processor u Allowing portions of the kernel to execute in parallel n Typically each processor does self- scheduling from the pool of available process or threads

41 41 Typical SMP Organization

42 42 Multiprocessor OS Design Considerations n The key design issues include u Simultaneous and concurrent processes or threads u Scheduling u Synchronization u Memory Management u Reliability and Fault Tolerance

43 43 Windows SMP Support n Threads can run on any processor u But an application can restrict affinity n Soft Affinity u The dispatcher tries to assign a ready thread to the same processor it last ran on. Why? u This helps reuse data still in that processor’s memory caches from the previous execution of the thread. n Hard Affinity u An application restricts threads to certain processor

44 44 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux

45 45 Microkernel n A microkernel is a small OS core that provides the foundation for modular extensions. n Big question is how small must a kernel be to qualify as a microkernel u Must drivers be in user space? n In theory, this approach provides a high degree of flexibility and modularity.

46 46 Kernel Architecture

47 47 Microkernel Design: Memory Management n Low-level memory management - Mapping each virtual page to a physical page frame u Most memory management tasks occur in user space

48 48 Microkernel Design: Interprocess Communication n Communication between processes or threads in a microkernel OS is via messages. n A message includes: u A header that identifies the sending and receiving process and u A body that contains direct data, a pointer to a block of data, or some control information about the process.

49 49 Microkernal Design: I/O and interrupt management n Within a microkernel it is possible to handle hardware interrupts as messages and to include I/O ports in address spaces. u a particular user-level process is assigned to the interrupt and the kernel maintains the mapping.

50 50 Benefits of a Microkernel Organization n Uniform interfaces on requests made by a process. n Extensibility n Flexibility n Portability n Reliability n Distributed System Support n Object Oriented Operating Systems

51 51 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux

52 52 Different Approaches to Processes n Differences between different OS’s support of processes include u How processes are named u Whether threads are provided u How processes are represented u How process resources are protected u What mechanisms are used for inter-process communication and synchronization u How processes are related to each other

53 53 Linux Tasks n A process, or task, in Linux is represented by a task_struct data structure n This contains a number of categories including: u State u Scheduling information u Identifiers u Interprocess communication u And others

54 54 Linux Process/Thread Model

55 55 Linux Threads n Traditional Unix: single thread n Modern Unix: KLTs n Linux: ULTs pthread (POSIX thread) libraries n ULTs belonging to the same process are mapped into kernel-level processes that share the same group ID. n Shared ID is used for resource sharing and to avoid context switching


Download ppt "Threads, SMP, and Microkernels Chapter 4. 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads."

Similar presentations


Ads by Google