Chapter 4: Threads March 17 th, 2009 Instructor: Hung Q. Ngo Kyung Hee University.

Slides:



Advertisements
Similar presentations
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Advertisements

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Multithreaded Programming
Chapter 4: Multithreaded Programming
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 12, 2005 Prof. John Kubiatowicz
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching January 30, 2006 Prof. Anthony D. Joseph
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 10, 2008 Prof. John Kubiatowicz
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Objectives To introduce a notion of a thread.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Process Concept An operating system executes a variety of programs
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 &
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 11, 2006 Prof. John Kubiatowicz
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 9, 2009 Prof. John Kubiatowicz
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 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.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Operating System Principles Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
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.
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.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:
CSC139 Operating Systems Lecture 4 Thread Dispatching Adapted from Prof. John Kubiatowicz's lecture notes for CS162
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 Lecture 4: Threads Advanced Operating System Fall 2010.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
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: 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.
Goals for Today How do we provide multiprogramming? What are Processes? How are they related to Threads and Address Spaces? Note: Some slides and/or pictures.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Introduction to threads
CSCI 511 Operating Systems Chapter 3,4 (Part A) Processes and Threads
CSCS 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 4: Threads.
Operating System Concepts
CSCI 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 13, 2010 Prof. John Kubiatowicz
CS162 Operating Systems and Systems Programming Lecture 3 Thread Dispatching January 30, 2008 Prof. Anthony D. Joseph
CSCI 511 Operating Systems Chapter 3,4 (Part A) Processes and Threads
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 10, 2007 Prof. John Kubiatowicz
January 28, 2010 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching January.
Chapter 4: Threads.
Presentation transcript:

Chapter 4: Threads March 17 th, 2009 Instructor: Hung Q. Ngo Kyung Hee University

4.2 Hung Q. Ngo Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

4.3 Hung Q. Ngo Operating System Concepts Objectives To introduce the notion of a thread – a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs of Pthreads, Win32, and Java thread library Note: Some slides and/or pictures in this class are adapted from slides ©2005 Silberschatz, Galvin, and Gagne, and from lecture notes of professor Kubiatowicz (UC Berkeley)

4.4 Hung Q. Ngo Operating System Concepts Recall: Process Address Protection Prog 1 Virtual Address Space 1 Prog 2 Virtual Address Space 2 Code Data Heap Stack Code Data Heap Stack Data 2 Stack 1 Heap 1 OS heap & Stacks Code 1 Stack 2 Data 1 Heap 2 Code 2 OS code OS data Translation Map 1Translation Map 2 Physical Address Space Providing Illusion of Separate Address Space? Load new Translation Map on Switch

4.5 Hung Q. Ngo Operating System Concepts Recall: Process Control Block (PCB) Need to save information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information Quiz: where to store PCB? How many PCB active at a time? “context”

4.6 Hung Q. Ngo Operating System Concepts Motivation Examples Web browser Retrieve data Display texts or images Word processor Responding to keystrokes from user Displaying graphics Performing spelling and grammar checking in the background

4.7 Hung Q. Ngo Operating System Concepts “Lightweight” Process with Threads Thread: a sequential execution stream within process (Sometimes called a “Lightweight process”) Process still contains a single Address Space No protection between threads Multithreading: a single program made up of a number of different concurrent activities Sometimes called multitasking Why separate the concept of a thread from that of a process? Discuss the “thread” part of a process (concurrency) Separate from the “address space” (Protection) Heavyweight Process  Process with one thread

4.8 Hung Q. Ngo Operating System Concepts Single and Multithreaded Processes Threads encapsulate concurrency: “Active” component Address spaces encapsulate protection: “Passive” part Keeps buggy program from trashing the system Why have multiple threads per address space?

4.9 Hung Q. Ngo Operating System Concepts Address Space with Threads

4.10 Hung Q. Ngo Operating System Concepts Examples of multithreaded programs Embedded systems Elevators, Planes, Medical systems, Wristwatches Single Program, concurrent operations Most modern OS kernels Internally concurrent because have to deal with concurrent requests by multiple users But no protection needed within kernel Database Servers Access to shared data by many concurrent users Also background utility processing must be done

4.11 Hung Q. Ngo Operating System Concepts Examples of multithreaded programs Network Servers Concurrent requests from network Again, single program, multiple concurrent operations File server, Web server, and airline reservation systems Parallel Programming (More than one physical CPU) Split program into multiple threads for parallelism This is called Multiprocessing

4.12 Hung Q. Ngo Operating System Concepts Benefits Responsiveness When part of the program is blocked or is performing a lengthy operation, e.g. web browser downloading Resource Sharing Same addr. space Economy In Solaris, creating a process is 30 times slower than creating a thread, and context switching is 5 times slower Utilization of MP Architectures Multithreading on a multi-CPU machine increases concurrency

4.13 Hung Q. Ngo Operating System Concepts Thread State State shared by all threads in process/addr space Contents of memory (global variables, heap) I/O state (file system, network connections, etc) State “private” to each thread Kept in TCB  Thread Control Block CPU registers (including, program counter) Execution stack – what is this? Execution Stack Parameters, Temporary variables return PCs are kept while called procedures are executing

4.14 Hung Q. Ngo Operating System Concepts Execution Stack Example Stack holds temporary results Permits recursive execution Crucial to modern languages A(int tmp) { if (tmp<2) B(); printf(tmp); } B() { C(); } C() { A(2); } A(1); A: tmp=2 ret=C+1 Stack Pointer Stack Growth A: tmp=1 ret=exit B: ret=A+2 C: ret=b+1

4.15 Hung Q. Ngo Operating System Concepts Classification Real operating systems have either One or many address spaces One or many threads per address space Did Windows 95/98/ME have real memory protection? No: Users could overwrite process tables/System DLLs Mach, OS/2, Linux Windows 9x??? Win NT to XP, Solaris, HP-UX, OS X Embedded systems (Geoworks, VxWorks, JavaOS,etc) JavaOS, Pilot(PC) Traditional UNIX MS/DOS, early Macintosh Many One # threads Per AS: ManyOne # of addr spaces:

4.16 Hung Q. Ngo Operating System Concepts Single-Threaded Example Imagine the following C program: main() { ComputePI(“pi.txt”); PrintClassList(“clist.text”); } What is the behavior here? Program would never print out class list Why? ComputePI would never finish

4.17 Hung Q. Ngo Operating System Concepts Use of Threads Version of program with Threads: main() { CreateThread(ComputePI(“pi.txt”)); CreateThread(PrintClassList(“clist.text”)); } What does “CreateThread” do? Start independent thread running given procedure What is the behavior here? Now, you would actually see the class list This should behave as if there are two separate CPUs CPU1CPU2CPU1CPU2 Time CPU1CPU2

4.18 Hung Q. Ngo Operating System Concepts Memory Footprint of Previous Example If we stopped this program and examined it with a debugger, we would see Two sets of CPU registers Two sets of Stacks Questions: How do we position stacks relative to each other? What maximum size should we choose for the stacks? What happens if threads violate this? How might you catch violations? Code Global Data Heap Stack 1 Stack 2 Address Space

4.19 Hung Q. Ngo Operating System Concepts Per Thread State Each Thread has a Thread Control Block (TCB) Execution State: CPU registers, program counter, pointer to stack Scheduling info: State (more later), priority, CPU time Accounting Info Various Pointers (for implementing scheduling queues) Pointer to enclosing process? (PCB)? Etc (add stuff as you find a need) OS Keeps track of TCBs in protected memory In Array, or Linked List, or …

4.20 Hung Q. Ngo Operating System Concepts Lifecycle of a Thread (or Process) As a thread executes, it changes state: new: The thread is being created ready: The thread is waiting to run running: Instructions are being executed waiting: Thread waiting for some event to occur terminated: The thread has finished execution “Active” threads are represented by their TCBs TCBs organized into queues based on their state

4.21 Hung Q. Ngo Operating System Concepts Ready Queue And Various I/O Device Queues Thread not running  TCB is in some scheduler queue Separate queue for each device/signal/condition Each queue can have a different scheduler policy Other State TCB 9 Link Registers Other State TCB 6 Link Registers Other State TCB 16 Link Registers Other State TCB 8 Link Registers Other State TCB 2 Link Registers Other State TCB 3 Link Registers Head Tail Head Tail Head Tail Head Tail Head Tail Ready Queue Tape Unit 0 Disk Unit 0 Disk Unit 2 Ether Netwk 0

4.22 Hung Q. Ngo Operating System Concepts Dispatch Loop Conceptually, the dispatching loop of the operating system looks as follows: Loop { RunThread(); ChooseNextThread(); SaveStateOfCPU(curTCB); LoadStateOfCPU(newTCB); } This is an infinite loop One could argue that this is all that the OS does Should we ever exit this loop??? When would that be?

4.23 Hung Q. Ngo Operating System Concepts Running a thread Consider first portion: RunThread() How do I run a thread? Load its state (registers, PC, stack pointer) into CPU Load environment (virtual memory space, etc) Jump to the PC How does the dispatcher get control back? Internal events: thread returns control voluntarily External events: thread gets preempted

4.24 Hung Q. Ngo Operating System Concepts Internal Events Blocking on I/O The act of requesting I/O implicitly yields the CPU Waiting on a “signal” from other thread Thread asks to wait and thus yields the CPU Thread executes a yield() Thread volunteers to give up CPU computePI() { while(TRUE) { ComputeNextDigit(); yield(); } }

4.25 Hung Q. Ngo Operating System Concepts Stack for Yielding Thread How do we run a new thread? run_new_thread() { newThread = PickNewThread(); switch(curThread, newThread); ThreadHouseKeeping(); /* next Lecture */ } How does dispatcher switch to a new thread? Save anything next thread may trash: PC, regs, stack Maintain isolation for each thread yield ComputePI Stack growth run_new_thread kernel_yield Trap to OS switch

4.26 Hung Q. Ngo Operating System Concepts Saving/Restoring state (often called “Context Switch) Switch(tCur,tNew) { /* Unload old thread */ TCB[tCur].regs.r7 = CPU.r7; … TCB[tCur].regs.r0 = CPU.r0; TCB[tCur].regs.sp = CPU.sp; TCB[tCur].regs.retpc = CPU.retpc; /*return addr*/ /* Load and execute new thread */ CPU.r7 = TCB[tNew].regs.r7; … CPU.r0 = TCB[tNew].regs.r0; CPU.sp = TCB[tNew].regs.sp; CPU.retpc = TCB[tNew].regs.retpc; return; /* Return to CPU.retpc */ }

4.27 Hung Q. Ngo Operating System Concepts Switch Details What if you make a mistake in implementing switch? Suppose you forget to save/restore register 4 Get intermittent failures depending on when context switch occurred and whether new thread uses register 4 System will give wrong result without warning Can you devise an exhaustive test to test switch code? No! Too many combinations and inter-leavings Cautionary tail: For speed, Topaz kernel saved one instruction in switch() Carefully documented!  Only works As long as kernel size < 1MB What happened?  Time passed, People forgot  Later, they added features to kernel (no one removes features!)  Very weird behavior started happening Moral of story: Design for simplicity

4.28 Hung Q. Ngo Operating System Concepts What happens when thread blocks on I/O? What happens when a thread requests a block of data from the file system? User code invokes a system call Read operation is initiated Run new thread/switch CopyFile read run_new_thread kernel_read Trap to OS switch Stack growth

4.29 Hung Q. Ngo Operating System Concepts External Events What happens if thread never does any I/O, never waits, and never yields control? Could the ComputePI program grab all resources and never release the processor?  What if it didn’t print to console? Must find way that dispatcher can regain control! Answer: Utilize External Events Interrupts: signals from hardware or software that stop the running code and jump to kernel Timer: like an alarm clock that goes off every some many milliseconds If we make sure that external events occur frequently enough, can ensure dispatcher runs

4.30 Hung Q. Ngo Operating System Concepts  add $r1,$r2,$r3 subi $r4,$r1,#4 slli $r4,$r4,#2 PC saved Disable All Ints Supervisor Mode Restore PC User Mode Raise priority Reenable All Ints Save registers Dispatch to Handler  Transfer Network Packet from hardware to Kernel Buffers  Restore registers Clear current Int Disable All Ints Restore priority RTI “Interrupt Handler” Example: Network Interrupt An interrupt is a hardware-invoked context switch No separate step to choose what to run next Always run the interrupt handler immediately lw$r2,0($r4) lw$r3,4($r4) add$r2,$r2,$r3 sw8($r4),$r2  External Interrupt Pipeline Flush

4.31 Hung Q. Ngo Operating System Concepts Use of Timer Interrupt to Return Control Solution to our dispatcher problem Use the timer interrupt to force scheduling decisions Timer Interrupt routine: TimerInterrupt() { DoPeriodicHouseKeeping(); run_new_thread(); } I/O interrupt: same as timer interrupt except that DoHousekeeping() replaced by ServiceIO(). Some Routine run_new_thread TimerInterrupt Interrupt switch Stack growth

4.32 Hung Q. Ngo Operating System Concepts Multithreading Models

4.33 Hung Q. Ngo Operating System Concepts Thread Libraries User Threads: Thread management done by user-level threads library Kernel Threads: supported & managed directly by OS Three primary thread libraries: POSIX Pthreads (both) Win32 threads (kernel level) Java threads (hybrid, API is implemented using thread lib available on host OS)

4.34 Hung Q. Ngo Operating System Concepts

4.35 Hung Q. Ngo Operating System Concepts

4.36 Hung Q. Ngo Operating System Concepts Many-to-One Model Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads + Efficient: thread management is done by the thread library in user space + Entire process will block if a thread makes a blocking system call + Unable to run in parallel on multiprocessors

4.37 Hung Q. Ngo Operating System Concepts One-to-One Model Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later +Better concurrency than Many-to-One +Creating a user thread requires creating the corresponding kernel thread

4.38 Hung Q. Ngo Operating System Concepts 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

4.39 Hung Q. Ngo Operating System Concepts 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

4.40 Hung Q. Ngo Operating System Concepts Summary Processes have two parts Threads (Concurrency) Address Spaces (Protection) Concurrency accomplished by multiplexing CPU Time: Unloading current thread (PC, registers) Loading new thread (PC, registers) Such context switching may be voluntary ( yield(), I/O operations) or involuntary (timer, other interrupts) Protection accomplished restricting access: Memory mapping isolates processes from each other Dual-mode for isolating I/O, other resources Book talks about processes When this concerns concurrency, really talking about thread portion of a process When this concerns protection, talking about address space portion of a process

End of Chapter 4