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

Slides:



Advertisements
Similar presentations
Threads Chapter 4 Threads are a subdivision of processes
Advertisements

Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 4 Threads Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
Threads, SMP, and Microkernels
Operating Systems: Internals and Design Principles
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Computer Systems/Operating Systems - Class 8
Threads, SMP, and MicroKernels
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Chapter 5 Processes and Threads Copyright © 2008.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process: Some Info. Motivation for threads! Two fundamental aspects of a “process”: Resource ownership Scheduling.
Operating Systems (OS) Threads, SMP, and Microkernel, Unix Kernel
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Threads, SMP, and Microkernels
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
UNIX Process Creation Every process, except process 0, is created by the fork() system call fork() allocates entry in process table and assigns a unique.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
A. Frank - P. Weisberg Operating Systems Threads Implementation.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
Operating Systems Lecture 09: Threads (Chapter 4)
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Operating System 4 THREADS, SMP AND MICROKERNELS
Threads, Thread management & Resource Management.
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Operating Systems 1 K. Salah Module 2.0: Processes Process Concept Trace of Processes Process Context Context Switching Threads –ULT –KLT.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Threads G.Anuradha (Reference : William Stallings)
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership: process includes a virtual address space to hold the process image (fig 3.16)
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
Threads, SMP, and Microkernels Chapter 4. Threads, SMP, and Microkernels 1. Processes and threads 2. Symmetric MultiProcessing (SMP) 3. Microkernel: structuring.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Operating System 4 THREADS, SMP AND MICROKERNELS.
Department of Computer Science and Software Engineering
Thread By Group III Kathryn Bean and Wafa’ Jaffal.
Chapter 4 Threads Seventh Edition By William Stallings Operating Systems: Internals and Design Principles.
Operating Systems: Internals and Design Principles
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Module 2.0: Threads.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Linux Development Lecture
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
Threads, SMP and Microkernels Process vs. thread: –Unit of resource ownership (process has virtual address space, memory, I/O channels, files) –Unit of.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 4 Threads.
Principles of Operating Systems Lecture 8
Threads, SMP, and Microkernels
Threads Chapter 4.
Lecture 4- Threads, SMP, and Microkernels
Operating System 4 THREADS, SMP AND MICROKERNELS
Threads Chapter 4.
Chapter 4 Threads, SMP, and Microkernels
Presentation transcript:

Threads, SMP, and Microkernels Chapter 4

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

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 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 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 Threads and Processes

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 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 Single Threaded and Multithreaded Process Models Thread Control Block contains a register image, thread priority and thread state information

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 Thread use in a Single-User System n Foreground and background work n Asynchronous processing n Speed of execution n Modular program structure

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 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 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 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 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 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 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 RPC Using Single Thread

20 RPC Using One Thread per Server

21 Multithreading on a Uniprocessor

22 Adobe PageMaker

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 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 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 Relationships between ULT Thread and Process Scheduling and States

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 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 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 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 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 Thread and Process Operation Latencies: an Example OperationUser-Level Threads Kernel- Level Threads Processes Null Fork ,300 Signal-Wait374411,840

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

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

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

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 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 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 Parallel Processor Architectures

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 Typical SMP Organization

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 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 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux

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 Kernel Architecture

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 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 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 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 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux

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 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 Linux Process/Thread Model

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