Review The Joys and Pains of Threads and Multithreading –what is a thread –threads vs. processes –opportunities and risks.

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 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
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.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
Threads. Announcements CS 4410 Homework is TODAY (11:59pm) –available and turn in via CMS CS 4411 initial design documents yesterday/today –Project due.
Threads Clients Servers Code Migration Software Agents Summary
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
Threads CSCI 444/544 Operating Systems Fall 2008.
Threads. Announcements CSUGLab accounts are ready Fixed homework 1 submissions using CMS.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
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
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.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
CS 153 Design of Operating Systems Spring 2015
1 From Processes to Threads. 2 Processes, Threads and Processors Hardware can interpret N instruction streams at once  Uniprocessor, N==1  Dual-core,
Operating Systems Lecture 09: Threads (Chapter 4)
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.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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.
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.
Introduction to Operating Systems and Concurrency.
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.
Processes & Threads Introduction to Operating Systems: Module 5.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
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-
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 4 – Thread Concepts
Threads & Multithreading
Processes and threads.
CS 6560: Operating Systems Design
Scheduler activations
Chapter 4 – Thread Concepts
Chapter 4 Threads.
Review An OS in action Processes and Programs
Threads & multithreading
Threads Chapter 4.
CS510 Operating System Foundations
CS703 – Advanced Operating Systems
Threads.
Presentation transcript:

Review The Joys and Pains of Threads and Multithreading –what is a thread –threads vs. processes –opportunities and risks

Outline More Joys and Pains

Threads & Multithreading (Continued)

Thread Hazards int a = 1, b = 2, w = 2; main() { CreateThread(fn, 4); while(w) ; } fn() { int v = a + b; w--; } What happens here? Lesson 7: Concurrency

Concurrency Problems A statement like w-- in C (or C++) is implemented by several machine instructions: ldr4, #w addr4, r4, -1 str4, #w Now, imagine the following sequence, what is the value of w? ldr4, #w C.S. ______________ addr4, r4, -1 str4, #w ______________ ldr4, #w addr4, r4, -1 str4, #w

Thread Hazards In a classical process (only one thread), the process’s stack segment serves to be the stack of the root thread: –The stack segment is far away from the code and data segments –It grows implicitly during function calls up to a maximum size In a multithreaded process, each thread needs a stack –Where should the stack be? –What size? –One important restriction: A thread’s stack has to be part of the process’s address space

Thread Hazards Option 1: –allocate the stacks on the heap (using malloc) –easier, hazardous Option 2: –allocate the stacks away from the other segments –require OS support, safer code static data heap stack code static data heap stack

Other Hazards? If there is a signal, who should get it? –One? –All? –Some designated thread? If there is an exception: –Kill only offending thread? –Kill process? Hidden concurrency problems: –Sharing through files

Signaling The O.S. translates some events into asynchronous signals: –input/output –alarms The O.S. also translates exceptions into signals, e.g.: –Memory access violations, illegal instructions, overflow, etc. Signals also used for process control, e.g.: –SIGKILL to kill a process, SIGSTOP to stop a process, etc. Rudimentary process communications, e.g.: –processes may use the kill() system call to send signals to each other (SIGUSR1, SIGUSR2)

Signal Handling A process can: –rely on the default signal handler (e.g. core dump in UNIX) –ignore signal (temporarily or permanently, dangerous) –sets its own handler Fundamental difference between signals & regular I/O –A process gets regular I/O when it chooses to do so (synchronous) –A process is interrupted and forced to handle a signal whenever a signal is posted (and not ignored). This is asynchronous Signals are very difficult to deal with and are better avoided

How Does it Work? Function h Program sets signal handler to h Signals work very much like interrupts hence the name software interrupts 1. An event of interest occurs 2. If process masks event out queue the signal 3. else, stop process, push its context on stack, force process to jump to handler Signal posted instruction

Thread Variations Threads differ according to three dimensions: Implementation: kernel-level versus user-level Execution: uniprocessor vs. multi-processor cooperative vs. uncooperative This gives up to 8 combinations

Cooperative Threads Each thread runs until it decides to give up the CPU main() { tid t1 = CreateThread(fn, arg); … Yield(t1); } fn(int arg) { … Yield(any); }

Cooperative Threads By their nature, cooperative threads use non pre-emptive scheduling (e.g. Windows 3.1) Advantages: Disadvantages: The scheduler gets invoked only when Yield is called with the argument any (or when a thread blocks) Depending on the thread package semantics, a thread could yield the processor when it blocks for I/O

Non-Cooperative Threads No explicit control passing among threads Rely on a scheduler to decide which thread to run A thread can be pre-empted at any point Often called pre-emptive threads Most modern thread packages use this approach

Execution on Uni vs Multi Processors Programmers often “exploit” the fact that a program is designed to run on a single processor to simplify the solution to problems such as concurrency control, etc. However, this is very bad programming style VALUABLE ADVICE: Always write your multithreaded program as if it were to run on a true, honest-to-goodness multiprocessor

Kernel Threads Simply stated, a kernel thread is one that is implemented and supported by the kernel (often called lightweight processes (LWP)) Each thread has its “Thread Control Block” (tcb) The tcb becomes the unit of scheduling in the system It is similar in “spirit” to the pcb, but contains much less information tcb contains: –placeholder for the context –the thread id –queueing support –thread state What are the modifications that are needed now to the pcb?

User-Level Threads User-level threads? You bet! –the thread scheduler is part of the program (a library, outside the kernel) –thread context switching and scheduling is done by the program (in the library) –Can either use cooperative or pre-emptive threads cooperative threads are implemented by CreateThread(), DestroyThread(), Yield(), Suspend(), etc. (library calls) pre-emptive threads are implemented with the help of a timer (signal), where the timer handler decides which thread to run next

User-Level Threads Context switching in user space? –Essentially the same as the implementation in the kernel, save registers in the tcb (in user memory), bring the new context from the corresponding tcb, and “jump” to the program counter location of the new thread The scheduler can implement any scheduling algorithm as before Caveat-Emptor: The kernel knows absolutely NOTHING about user- level threads –the user-level threads actually multiplex themselves on the kernel- level threads –the kernel only sees the kernel-level threads that it gave to a process

Multiplexing User-Level Threads The user-level thread package sees a “virtual” processor(s) –it schedules user-level threads on these virtual processors –each “virtual” processor is actually implemented by a kernel thread The big picture: –Create as many kernel threads as there are processors –Create as many user-level threads as the application needs –Multiplex user-level threads on top of the kernel-level threads Why would you want to do that? Why not just create as many kernel- level threads as the application needs? –Context switching –Resources

User-Level vs. Kernel Threads User-Level Managed by application Kernel is not aware of thread Context switching done by application (cheap) Can create as many as the application needs Must be used with care Kernel-Level Managed by kernel Consumes kernel resources Context switching done by kernel (expensive) Number limited by kernel resources Simpler to use Key issue: kernel threads provide virtual processors to user-level threads, but if all of kthreads block, then all user-level threads will block even if the program logic allows them to proceed

Retrospect on Scheduling Scheduling threads is very similar to scheduling processes: –it could be pre-emptive or non pre-emptive –it could use any scheduling algorithm (FCFS, SJF, RR, …) –threads (not processes) now get to be on the ready queue, can be blocked, or can be running, etc. But: –a good scheduler takes into account the relation between threads and processes –Therefore, we have several variations

Thread Scheduling Since all threads share code & data segments Option 1: Ignore this fact Option 2: Gang scheduling-- run all threads belonging to a process together (multiprocessor only) –if a thread needs to synchronize with another thread, the other one is available and active Option 3: Two-level scheduling-- schedule processes, and within each process, schedule threads –reduce context switching overhead and improve cache hit ratio Option 4: Space-based affinity-- assign threads to processors (multiprocessor only) –improve cache hit ratio, but can bite under low-load condition