Operating Systems CSE 411 CPU Management Sept. 27 2006 - Lecture 10 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

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
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
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.
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.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Chapter 5 Processes and Threads Copyright © 2008.
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
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.
Threads vs. Processes April 7, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
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.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
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.
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.
System Calls 1.
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.
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.
Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:
Implementing Processes and Process Management Brian Bershad.
Scheduling Basic scheduling policies, for OS schedulers (threads, tasks, processes) or thread library schedulers Review of Context Switching overheads.
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.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
Threads G.Anuradha (Reference : William Stallings)
Operating Systems CMPSC 473 Lecture 8: Threads September Instructor: Bhuvan Urgaonkar.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
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.
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.
CSE 153 Design of Operating Systems Winter 2015 Lecture 4: Threads.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
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.
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 ≠
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.
CS 6560: Operating Systems Design
Scheduler activations
Threads & multithreading
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
Chapter 4: Threads.
CS703 – Advanced Operating Systems
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Presentation transcript:

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

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 swithing 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

Threads

Concurrent Programming Many programs want to do many things “at once” Web browser –Download Web pages, read cache files, accept user input, … Web server –Handle incoming connections from multiple clients at once Scientific programs –Process different parts of a data set on different CPUs In each case, would like to share memory across these activities –Web browser: Share buffer for HTML pages and inline images –Web server: Share memory cache of recently-accessed pages –Scientific programs: Share memory of data set being processed Can’t we simply do this with multiple processes?

Why processes are not always ideal Processes are not very efficient –Each process has its own PCB and OS resources –Typically high overhead for each process: e.g., 1.7 KB per task_struct on Linux! –Creating a new process is often very expensive Process don’t (directly) share memory –Each process has its own address space –Parallel and concurrent programs often want to directly manipulate the same memory E.g., when processing elements of a large array in parallel –Note: Many Oses provide some form of inter-process shared memory UNIX shmget() and shmat() system calls Still, this requires more programmer work and does not address the efficiency issues

Can we do better? What can we share across all of these tasks? –Same code - generally running the same or similar programs –Same data –Same privileges –Same OS resources (files, sockets, etc.) What is private to each task? –Execution state: CPU registers, stack, and program counter Key idea of this lecture: –Separate the concet of a process from a thread of control –The process is the address space and OS resources –Each thread has its own CPU execution state

Processes and Threads Each process has one or more threads “within” it –Each thread has its own stack, CPU registers, etc. –All threads within a process share the same address space and OS resources Threads share memory, so can communicate directly! A basic unit of CPU utilization like a process (not necessarily known to the OS though) Shares code + data + some other OS resources with other threads that belong to the same process

(Old) Process Address Space

(New) Address Space with Threads All threads in a process share the same address space

Implementing Threads Given what we know about processes, implementing threads is “easy” Idea: Break the PCB into two pieces: –Thread-specific stuff: Processor state –Process-specific state: Address space and OS resources (e.g., open files)

Thread Control Block (TCB) TCB contains info on a single thread –Just processor state and pointer to corresponding PCB PCB contains info on the containing process –Address space and OS resources, but NO processor state!

Thread Control Block (TCB) TCBs are smaller and cheaper than PCBs –Linux TCB (thread_struct) has 24 fields –Linux PCB (task_struct) has 106 fields

Context Switching TCB is now the unit of a context switch –Ready queue, wait queues, etc. now contain pointers to TCBs –Context switch causes CPU state to be copied to/from the TCB Context switch between two threads of the same process –No need to change address space Context switch between two threads of different processes –Must change address space, sometimes invalidating cache –This will become relevant when we talk about virtual memory

User-level Threads Early UNIX designs did not support threads at kernel level –OS only knew about processes with separate address spaces However, can still implement threads as a user-level library –OS does not need to know anything about multiple threads in a process How is this possible? –Recall: All threads in a process share the same address space –So, managing multiple threads only requires switching the CPU state (PC, other registers, etc.) –And this can be done directly by a user program without OS help!

Implementing User-level Threads Alternate to kernel-level threads –Implement all thread functions as a user-level library E.g., libpthread.a –OS thinks the process has a single thread Use the same PCB structure as when we studied processes –OS need not know anything about multiple threads in a process! How to create a user-level thread? –Thread library maintains a TCB for each thread in the process Just a linked list –Allocate a separate stack for each thread (e.g., with malloc)

User-level thread address space Stacks must be allocated carefully and managed by the thread library

User-level context switching How to switch between user-level threads? Need some way to swap CPU state Fortunately, this does not require any privileged instructions –So the threads library can use the same instructions as the OS to save or load the CPU state into the TCB Why is it safe to let the user switch the CPU state?

setjmp() and longjmp() C standard library routines for saving and restoring processor state int setjmp (jmp_buf env); –Save current CPU state in the “jmp_buf” structure void longjmp (jmp_buf env, int retval); –Restore CPU state from “jmp_buf” structure, causing corresponding setjmp() call to return with return value “retval” struct jmp_buf { … } –Contains CPU specific fields for saving registers, PC.

setjmp() and longjmp()

Preemptive and nonpreemptive threads How to prevent a single user-level thread from hogging the CPU? Strategy 1: Require threads to cooperate –Called non-preemptive threads –Each thread must call back into the thread library periodically This gives the thread library control over the thread’s execution –yield() operation Thread voluntarily gives up the CPU –What happens when a thread calls yield() ? Strategy 2: Use preemption –Thread library tells OS to send it a signal periodically –Causes the process to jump into a signal handler –The signal handler gives control back to the thread library Thread library then context switches to a new thread

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