Download presentation
Presentation is loading. Please wait.
Published byMark Davidson Modified over 9 years ago
1
Chapter 4: Threads March 17 th, 2009 Instructor: Hung Q. Ngo Kyung Hee University
2
4.2 Hung Q. Ngo 09.03.17 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads
3
4.3 Hung Q. Ngo 09.03.17 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.4 Hung Q. Ngo 09.03.17 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
5
4.5 Hung Q. Ngo 09.03.17 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”
6
4.6 Hung Q. Ngo 09.03.17 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
7
4.7 Hung Q. Ngo 09.03.17 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
8
4.8 Hung Q. Ngo 09.03.17 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?
9
4.9 Hung Q. Ngo 09.03.17 Operating System Concepts Address Space with Threads
10
4.10 Hung Q. Ngo 09.03.17 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
11
4.11 Hung Q. Ngo 09.03.17 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
12
4.12 Hung Q. Ngo 09.03.17 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
13
4.13 Hung Q. Ngo 09.03.17 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
14
4.14 Hung Q. Ngo 09.03.17 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
15
4.15 Hung Q. Ngo 09.03.17 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:
16
4.16 Hung Q. Ngo 09.03.17 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
17
4.17 Hung Q. Ngo 09.03.17 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
18
4.18 Hung Q. Ngo 09.03.17 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
19
4.19 Hung Q. Ngo 09.03.17 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 …
20
4.20 Hung Q. Ngo 09.03.17 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
21
4.21 Hung Q. Ngo 09.03.17 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
22
4.22 Hung Q. Ngo 09.03.17 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?
23
4.23 Hung Q. Ngo 09.03.17 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
24
4.24 Hung Q. Ngo 09.03.17 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(); } }
25
4.25 Hung Q. Ngo 09.03.17 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
26
4.26 Hung Q. Ngo 09.03.17 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 */ }
27
4.27 Hung Q. Ngo 09.03.17 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
28
4.28 Hung Q. Ngo 09.03.17 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
29
4.29 Hung Q. Ngo 09.03.17 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
30
4.30 Hung Q. Ngo 09.03.17 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
31
4.31 Hung Q. Ngo 09.03.17 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
32
4.32 Hung Q. Ngo 09.03.17 Operating System Concepts Multithreading Models
33
4.33 Hung Q. Ngo 09.03.17 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)
34
4.34 Hung Q. Ngo 09.03.17 Operating System Concepts
35
4.35 Hung Q. Ngo 09.03.17 Operating System Concepts
36
4.36 Hung Q. Ngo 09.03.17 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
37
4.37 Hung Q. Ngo 09.03.17 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
38
4.38 Hung Q. Ngo 09.03.17 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
39
4.39 Hung Q. Ngo 09.03.17 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
40
4.40 Hung Q. Ngo 09.03.17 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
41
End of Chapter 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.