Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Chapter 3 Process Description and Control
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.
Scheduler Activations Effective Kernel Support for the User-Level Management of Parallelism.
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Threads CSCI 444/544 Operating Systems Fall 2008.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
Process Description and Control A process is sometimes called a task, it is a program in execution.
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.
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
CS 153 Design of Operating Systems Spring 2015
Operating Systems CMPSC 473 Threads September 16, Lecture 7 Instructor: Bhuvan Urgaonkar.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Operating Systems 1 K. Salah Module 2.0: Processes Process Concept Trace of Processes Process Context Context Switching Threads –ULT –KLT.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Threads G.Anuradha (Reference : William Stallings)
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
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.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Operating Systems: Internals and Design Principles
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Major OS Components CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Processes and threads.
Process concept.
Interrupts and signals
Operating Systems CMPSC 473
CS 6560: Operating Systems Design
Scheduler activations
Chapter 4 Threads.
Intro to Processes CSSE 332 Operating Systems
Threads & multithreading
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process & its States Lecture 5.
Architectural Support for OS
CS510 Operating System Foundations
Architectural Support for OS
CS703 – Advanced Operating Systems
Threads CSE 2431: Introduction to Operating Systems
CS Introduction to Operating Systems
Presentation transcript:

Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University

2 Rutgers UniversityCS 416: Operating Systems Threads Thread of execution: stack + registers (which includes the PC) Informally: where an execution stream is currently at in the program and the method invocation chain that brought the execution stream to the current place Example: A called B which called C which called B which called C The PC should be pointing somewhere inside C at this point The stack should contain 5 activation records: A/B/C/B/C Thread for short Process model discussed last time implies a single thread

3 Rutgers UniversityCS 416: Operating Systems Multi-Threading Why limit ourselves to a single thread? Think of a web server that must service a large stream of requests If only have one thread, can only process one request at a time What to do when reading a file from disk? Multi-threading model Each process can have multiple threads Each thread has a private stack Registers are also private All threads of a process share the code and heap Objects to be shared across multiple threads should be allocated on the heap

4 Rutgers UniversityCS 416: Operating Systems 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

5 Rutgers UniversityCS 416: Operating Systems 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

6 Rutgers UniversityCS 416: Operating Systems Thread Creation thread_create() thread_create() code PCBs TCBs stacks new_thread_starts_here PC SP

7 Rutgers UniversityCS 416: Operating Systems 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

8 Rutgers UniversityCS 416: Operating Systems Diagram of Thread State

9 Rutgers UniversityCS 416: Operating Systems 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 When does context switching take place?

10 Rutgers UniversityCS 416: Operating Systems 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

11 Rutgers UniversityCS 416: Operating Systems How Is the Switching Code Invoked? user thread executing  clock interrupt  PC modified by hardware to “vector” to interrupt handler  user thread state is saved for restart  clock interrupt handler is invoked  disable interrupt checking  check whether current thread has run “long enough”  if yes, post asynchronous software trap (AST)  enable interrupt checking  exit interrupt handler  enter “return-to-user” code  check whether AST was posted  if not, restore user thread state and return to executing user thread; if AST was posted, call context switch code Why need AST?

12 Rutgers UniversityCS 416: Operating Systems How Is the Switching Code Invoked? (cont) user thread executing  system call to perform I/O  user thread state is saved for restart  OS code to perform system call is invoked  I/O operation started (by invoking I/O driver)  set thread status to waiting  move thread’s TCB from run queue to wait queue associated with specific device  call context switching code

13 Rutgers UniversityCS 416: Operating Systems Context Switching At entry to CS, the return address is either in a register or on the stack (in the current activation record) CS saves this return address to the TCB instead of the current PC To thread, it looks like CS just took a while to return! If the context switch was initiated from an interrupt, the thread never knows that it has been context switched out and back in unless it looking at the “wall” clock BC CS BC UC BCCS

14 Rutgers UniversityCS 416: Operating Systems Context Switching (cont) Even that is not quite the whole story When a thread is switched out, what happens to it? How do we find it to switch it back in? This is what the TCB is for. System typically has A run queue that points to the TCBs of threads ready to run A blocked queue per device to hold the TCBs of threads blocked waiting for an I/O operation on that device to complete When a thread is switched out at a timer interrupt, it is still ready to run so its TCB stays on the run queue When a thread is switched out because it is blocking on an I/O operation, its TCB is moved to the blocked queue of the device

15 Rutgers UniversityCS 416: Operating Systems Ready Queue And Various I/O Device Queues

16 Rutgers UniversityCS 416: Operating Systems Switching Between Threads of Different Processes What if switching to a thread of a different process? Caches, TLB, page table, etc.? Caches Physical addresses: no problem Virtual addresses: cache must either have process tag or must flush cache on context switch TLB Each entry must have process tag or must flush TLB on context switch Page table Typically have page table pointer (register) that must be reloaded on context switch

17 Rutgers UniversityCS 416: Operating Systems 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?

18 Rutgers UniversityCS 416: Operating Systems Thread Implementation Kernel-level threads (lightweight processes) Kernel sees multiple execution context Thread management done by the kernel User-level threads Implemented as a thread library which contains the code for thread creation, termination, scheduling and switching Kernel sees one execution context and is unaware of thread activity Can be preemptive or not

19 Rutgers UniversityCS 416: Operating Systems User-Level vs. Kernel-Level Threads Advantages of user-level threads Performance: low-cost thread operations (do not require crossing protection domains) Flexibility: scheduling can be application specific Portability: user-level thread library easy to port Disadvantages of user-level threads If a user-level thread is blocked in the kernel, the entire process (all threads of that process) are blocked Cannot take advantage of multiprocessing (the kernel assigns one process to only one processor)

20 Rutgers UniversityCS 416: Operating Systems User-Level vs. Kernel-Level Threads process processor user-level threads thread scheduling process scheduling kernel-level threads thread scheduling kernel user processor threads process scheduling

21 Rutgers UniversityCS 416: Operating Systems User-Level vs. Kernel-Level Threads No reason why we shouldn’t have both Most systems now support kernel threads User-level threads are available as linkable libraries kernel-level threads processor user-level threads thread scheduling thread scheduling kernel user process scheduling

22 Rutgers UniversityCS 416: Operating Systems Threads vs. Processes Why multiple threads? Can’t we use multiple processes to do whatever that is that we do with multiple threads? Of course, we need to be able to share memory (and other resources) between multiple processes … But this sharing is already supported – see later in the lecture Operations on threads (creation, termination, scheduling, etc..) are cheaper than the corresponding operations on processes This is because thread operations do not involve manipulations of other resources associated with processes Inter-thread communication is supported through shared memory without kernel intervention Why not? Have multiple other resources, why not threads

23 Rutgers UniversityCS 416: Operating Systems Thread/Process Operation Latencies OperationUser-level Threads (  s) Kernel Threads (  s) Processes (  s) Null fork ,300 Signal-wait374411,840

24 Rutgers UniversityCS 416: Operating Systems Next Time Synchronization Read Silberschatz