1 Lecture 4: Threads Advanced Operating System Fall 2010.

Slides:



Advertisements
Similar presentations
OPERATING SYSTEMS Threads
Advertisements

Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 4: Multithreaded Programming
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Chapter 4: Multithreaded Programming
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
A. Frank - P. Weisberg Operating Systems Threads Implementation.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Chapter 4: 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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating System Concepts
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

1 Lecture 4: Threads Advanced Operating System Fall 2010

2 Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread and Kernel Thread Multithreading Models Threading Issues

3 Process Resource ownership process is allocated a virtual address space to hold the process image Process may be allocated control or ownership of resources, e.g. I/O and files Protection function by OS Scheduling/execution The execution of a process follows an execution path(trace) through one or more programs The execution of a process may be interleaved with other processes Execution state and a dispatching priority These two characteristics are treated independently by the operating system

4 Processes & Threads Resource ownership – Process or Task Scheduling/execution – Thread or lightweight process One process, one thread (MS-DOS) One process, multiple threads (Java Runtime) Multiple processes, multiple threads (W2K, Solaris, Linux) Multiple processes, one thread per process (Unix)

5 Processes & Threads (cont.) In a multithreaded environment, the followings are associated with a process: Address space to hold the process image Protected access to processors, other processes (IPC), files, and I/O resources (devices & channels) Within a process, there may be one or more threads, each with the following: A thread execution state (Running, Ready, etc) A saved context when not running – a separate program counter An execution stack Some static storage for local variables for this thread Access to memory and resources of its process, shared with all other threads in that process (global variables)

6 Single Threaded and Multithreaded Process Models

7 Process Address Space Revisited OS Code Globals Stack Heap OS Code Globals Stack Heap Stack (a) Single-threaded address space(b) Multi-threaded address space

8 Multi-Threading (cont) Implementation Each thread is described by a thread-control block (TCB) A TCB typically contains Thread ID Space for saving registers Pointer to thread-specific data not on stack Observation Although the model is that each thread has a private stack, threads actually share the process address space  There’s no memory protection!  Threads could potentially write into each other’s stack

9 Benefits of Threads Responsiveness Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the use. Resource Sharing Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel

10 Benefits of Threads (cont.) Economy Takes less time to create a new thread than a process Less time to terminate a thread than a process Less time to switch between two threads within the same process Utilization of Multiprocessor Architectures Threads within the same process may be running in parallel on different processors

11 Uses of Threads in a Single- User Multiprocessing System Foreground and background work For example, in a spreadsheet program, one thread could display menus and read user input, while another thread executes user commands and updates the spreadsheet. Asynchronous processing Asynchronous elements in the program can be implemented as threads. For example, as a protection against power failure, a word processor may write its buffer to disk once every minute. A thread can be created whose sole job is periodic backup and that schedules directly with the OS. Speed execution On a multiprocessor system, multiple threads from the same process may be able to execute simultaneously. Modular program structure Programs that involve a variety of activities or a variety of sources and destinations of input and output may be easier to design and implement using threads.

12 Thread States Running Ready Blocked Note: Suspend is at process-level

13 Diagram of Thread State

14 Thread Operations Spawn – create new thread Block – when a thread needs to wait for an event, it will block Unblock – when the event for which a thread is blocked occurs, the thread is moved to the ready queue. Finish – when a thread completes, its register context and stack are deallocated.

15 Context Switching Suppose a process has multiple threads …uh oh … a uniprocessor machine only has 1 CPU … what to do? In fact, even if we only had one thread per process, we would have to do something about running multiple processes … We multiplex the multiple threads on the single CPU At any instance in time, only one thread is running At some point in time, the OS may decide to stop the currently running thread and allow another thread to run This switching from one running thread to another is called context switching

16 Context Switching (cont) How to do a context switch? Save state of currently executing thread Copy all “live” registers to thread control block For register-only machines, need at least 1 scratch register points to area of memory in thread control block that registers should be saved to Restore state of thread to run next Copy values of live registers from thread control block to registers

17 Context Switching (cont) When does context switching occur? When the OS decides that a thread has run long enough and that another thread should be given the CPU Remember how the OS gets control of the CPU back when it is executing user code? When a thread performs an I/O operation and needs to block to wait for the completion of this operation To wait for some other thread Thread synchronization: we’ll talk about this lots in a couple of lectures

18 Threads & Signals What happens if kernel wants to signal a process when all of its threads are blocked? When there are multiple threads, which thread should the kernel deliver the signal to? OS writes into process control block that a signal should be delivered Next time any thread from this process is allowed to run, the signal is delivered to that thread as part of the context switch What happens if kernel needs to deliver multiple signals?

19 Threads Suspending a process involves suspending all threads of the process since all threads share the same address space Termination of a process, terminates all threads within the process

20 Question? If one thread in a process is blocked, does this prevent other threads in the process even if that other thread is in a ready state?

21 Answer Depends on whether OS is involved when the thread is blocked. If OS is involved, then answer is “yes”.

22 Thread Synchronization All of the threads of a process share the same address space and other resources such as open files. Any alternation of a resource by one thread affects the environment of the other threads in the same process. It is therefore necessary to synchronize the activities of the various threads. Will be covered later.

23 User Thread and Kernel Thread

24 User Threads All of the work of thread management is done by the application. The kernel is not aware of the existence of threads An application can be programmed to be multi-threaded by using a threads library, which is a package of routines for user thread management. The thread library contains code for creating and destroying threads, for passing messages and data between threads, for scheduling thread execution and for saving and restoring thread contexts. Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

25 Pure User Threads Advantages: Thread switching does not require user/kernel mode switching. Thread scheduling can be application specific. User Threads can run on any OS through a thread library. Disadvantages: When a ULT executes a system call, not only the thread is blocked, but all of the threads within the process are blocked. Multithreaded application cannot take advantage of multiprocessing since kernel assign one process to only one processor at a time.

26 Kernel Threads Supported and managed directly by the OS. W2K, Linux, and OS/2 are examples of this approach In a pure Kernel Thread facility, all of the work of thread management is done by the kernel. There is no thread management code in the application area, simply an application programming interface to the kernel thread facility.

27 Pure Kernel Threads Advantages Kernel can simultaneously schedule multiple threads from the same process on multiple processors If one thread in a process is blocked, kernel can schedule another thread of the same process Disadvantage More overhead

28 Multithreading Models Many-to-One One-to-One Many-to-Many

29 Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads

30 One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

31 Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

32 Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

33 Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

34 Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads?

35 Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled

36 Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process

37 Thread Pools Create a number of threads in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool

38 Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

39 Scheduler Activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads

40 End of lecture 4 Thank you!