Operating Systems CSE 411 CPU Management Sept. 29 2006 - Lecture 11 Instructor: Bhuvan Urgaonkar.

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

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.
Chapter 5 Threads os5.
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.
Processes CSCI 444/544 Operating Systems Fall 2008.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
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.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Process Concept An operating system executes a variety of programs
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
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.
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.
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.
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.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Threads G.Anuradha (Reference : William Stallings)
Operating Systems CMPSC 473 Lecture 8: Threads September Instructor: Bhuvan Urgaonkar.
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 Lecture 4: Threads Advanced Operating System Fall 2010.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
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.
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 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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Operating Systems CMPSC 473 Processes (5) September Lecture 11 Instructor: Bhuvan Urgaonkar.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Operating System Concepts
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Processes Chapter 3. Processes in Distributed Systems Processes and threads –Introduction to threads –Distinction between threads and processes Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
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.
Operating Systems CMPSC 473
CS 6560: Operating Systems Design
Scheduler activations
Chapter 4: Multithreaded Programming
Process Management Presented By Aditya Gupta Assistant Professor
Lecture 10: Threads Implementation
Threads and Concurrency
Chapter 3: Processes.
Lecture 10: Threads Implementation
CS703 – Advanced Operating Systems
Presentation transcript:

Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar

Threads

What’s wrong with a process? Multi-programming was developed to allow multiplexing of CPU and I/O –Multiple processes given the illusion of running concurrently Several applications would like to have multiple processes –Web server: a process blocks on a file I/O call, then another process can run on CPU –What would be needed? Ability to create/destroy processes on demand –We already know how the OS does this We may want control over the scheduling of related processes –This is totally controlled by the OS scheduler Processes may need to communicate with each other –Message passing (e.g., signals) or shared memory (coming up) both need OS assistance Processes may need to be synchronized with each other (coming up) –Consider two Web server processes updating the same data –Things not very satisfactory with multi-process applications: 1. Communication needs help from the OS (system calls) 2. Duplication of same code may cause wastage of memory 3. PCBs are large and eat up precious kernel memory 4. Process context-switching imposes overheads 5. No control over scheduling of processes comprising the same application

Kernel-level threads 1. Communication between related processes needs OS system calls OS intervention can be avoided if the processes were able to share some memory without any help from the OS –That is, we are looking for a way for multiple processes to have the same address space (almost) –Address space: Code, data (global variables and heap), stack –Option #1: Share global variables Problem: We don’t know in advance what communication may occur, so we do not know how much memory need to be shared in advance –Option #2: Share data (globals and heap) 2. Duplication of code may cause waste of memory –Option #3: Share code and data Note: Not all processes may want to execute the same code Expose the same code to all, let each execute whatever part they want to –Different threads may execute different parts of the code What we have now are called kernel-level threads –Cycle through the same 5 states that we had studied for a process –OS provides system calls (analogous to fork, exit, exec) for kernel-level threads

Kernel Threads PCB can contain things common across threads belonging to the process Have a Thread Control Block (TCB) for things specific to a thread Side-effect: TCBs are smaller than PCBs, occupy less memory

Things not very satisfactory with a process and how we can address these Kernel-level threads help fix some problems with processes 1. Share data, efficient communication made possible 2. Share code 3. TCBs are smaller than PCBs => take less kernel memory Now let us consider the remaining problems with processes 4. Process context-switching imposes overhead - Do threads impose a smaller overhead? 5. No control over scheduling of processes comprising the same process/application - Do threads help us here?

Context Switch Revisited Context switch involves –Save all registers and PC in PCB: same for a kernel-level thread –Save process state in PCB: same for a kernel-level thread Do not confuse “process state” (ready, waiting, etc.) with “processor state” (registers, PC) and “processor mode” (user or kernel) –Flush TLB (not covered yet): no if threads belong to same process –Run scheduler to pick the next process, change address space: same for kernel-level threads belonging to different processes Context switch between threads of the same process is faster than a process context switch - Due to address space change related operations Context switch between threads of different processes is almost as expensive as a process context switch Note: SGG: Thread creation and context switching faster than process creation and context switching - Only creation faster not necessarily context switching!!

How can the context switch overhead be reduced? If a multi-threaded application were able to switch between its threads without involving the OS … Can this be achieved? What would it involve? The application would have to –Maintain separate PC and stack for each thread Easy to do: allocate and maintain PCs and stacks on the heap –Be able to switch from thread to thread in accordance with some scheduling policy Need to save/restore processor state (PC, registers) while in user mode Possible using setjmp()/longjmp() calls

setjmp() and longjmp()

Reducing the context switch overhead Requirement 1: Application maintains separate PC and stack for each thread Requirement 2: Application has a way to switch from thread to thread without OS intervention Final missing piece: How does a thread scheduler get invoked? That is, when is a thread taken off the CPU and another scheduled? Note: Only concerned with threads of the same process. Why? Strategy 1: Require all threads to yield the CPU periodically Strategy 2: Set timers that send SIGALRM signals to the process “periodically” E.g., UNIX: settimer() system call Implement signal handler Handler saves CPU state for prev. running thread using setjmp() into jmp_buf struct Copies the contents of jmp_buf into TCB on the heap Calls the thread scheduler that picks the next thread to run Copies the CPU state of chosen thread from heap into jmp_buf, calls longjmp() What we have now are called user-level threads

Kernel-level threads Pro: OS knows about all the threads in a process –Can assign different scheduling priorities to each one –Can context switch between multiple threads in one process Con: Thread operations require calling the kernel –Creating, destroying, or context switching require system calls

User-level threads Pro: Thread operations very fast –Typically X faster than going through kernel Pro: Thread state is very small –Just CPU state and stack Con: If one thread blocks, stall entire process Con: Can’t use multiple CPUs! –Kernel only knows one CPU context Con: OS may not make good scheduling decisions –Could schedule a process with only idle threads –Could de-schedule a process with a thread holding a lock

Signal Handling with Threads Recall: Signals are used in UNIX systems to notify a process that a particular event has occurred Recall: A signal handler is used to process signals - Signal is generated by particular event - Signal is delivered to a process - 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

Signal Handling (more) When does a process handle a signal? –Whenever it gets scheduled next after the generation of the signal We said the OS marks some members of the PCB to indicate that a signal is due –And we said the process will execute the signal handler when it gets scheduled –But its PC had some other address! The address of the instruction the process was executing when it was scheduled last –Complex task due to the need to juggle stacks carefully while switching between user and kernel mode

Signal Handling (more) Remember that signal handlers are functions defined by processes and included in the user mode code segment –Executed in user mode in the process’s context The OS forces the handler’s starting address into the program counter –The user mode stack is modified by the OS so that the process execution starts at the signal handler

Combining the benefits of kernel and user-level threads Read from text: Sections 4.2, 4.3, 4.4.6

Inter-process Communication (IPC) Two fundamental ways –Shared-Memory E.g., playing tic-tac-toe or chess –Message Passing E.g., Letter, Any communication involves a combination of these two

IPC: Message Passing OS provides system calls that processes/threads can use to pass messages to each other A thread library could provide user-level calls for the same –OS not involved

IPC: Shared Memory OS provides system calls using which processes can create shared memory that they can read to/write from Threads: Can share memory without OS intervention

Process/Thread synchronization Fundamental problem that needs to be solved to enable IPC Will study it next time