Download presentation
Presentation is loading. Please wait.
Published byJeffrey Mason Modified over 8 years ago
1
操作系统原理 OPERATING SYSTEM Chapter 2 Processes and Threads 进程与线程
2
Figure 2-1. (a) Multiprogramming of four programs. (b) Conceptual model of four independent, sequential processes. (c) Only one program is active at once. The Process Model Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
3
Events which cause process creation: System initialization. Execution of a process creation system call by a running process. A user request to create a new process. Initiation of a batch job. Process Creation Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
4
Events which cause process termination: Normal exit (voluntary). Error exit (voluntary). Fatal error (involuntary). Killed by another process (involuntary). Process Termination Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
5
Figure 2-2. A process can be in running, blocked, or ready state. Transitions between these states are as shown. Process States Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
6
Figure 2-3. The lowest layer of a process-structured operating system handles interrupts and scheduling. Above that layer are sequential processes. Implementation of Processes (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
7
Figure 2-4. Some of the fields of a typical process table entry. Implementation of Processes (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
8
Figure 2-5. Skeleton of what the lowest level of the operating system does when an interrupt occurs. Implementation of Processes (3) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
9
Figure 2-6. CPU utilization as a function of the number of processes in memory. Modeling Multiprogramming Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
10
Figure 2-7. A word processor with three threads. Thread Usage (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
11
Figure 2-8. A multithreaded Web server. Thread Usage (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
12
Figure 2-9. A rough outline of the code for Fig. 2-8. (a) Dispatcher thread. (b) Worker thread. Thread Usage (3) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
13
Figure 2-10. Three ways to construct a server. Thread Usage (4) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
14
Figure 2-11. (a) Three processes each with one thread. (b) One process with three threads. The Classical Thread Model (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
15
Figure 2-12. The first column lists some items shared by all threads in a process. The second one lists some items private to each thread. The Classical Thread Model (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
16
Figure 2-13. Each thread has its own stack. The Classical Thread Model (3) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
17
Figure 2-14. Some of the Pthreads function calls. POSIX Threads (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
18
Figure 2-15. An example program using threads. POSIX Threads (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639...
19
Figure 2-16. (a) A user-level threads package. (b) A threads package managed by the kernel. Implementing Threads in User Space Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
20
Implementing Threads in User Space Advantages –Thread switching in user space is faster than trapping to the kernel No trapping/ no context switching/ no memory cache flushing –Customized scheduling algorithm –Scale better Disadvantages –Difficult to use blocking system call Nonblocking call requires the changes to the OS and user programs Jacket or wrapper –Page fault Blocking entire process until the disk I/O is complete –Running for ever unless giving up CPU voluntarily Runtime request clock signal (interrupt) per second –Crude and messy to program/ Overhead/ interfere with real clock interrupt –Blocking in the system call, kernel or select ?
21
Implementing Threads in the Kernel Advantages –Schedule the threads across the different processes –Does not need new, nonblocking system calls Disadvantages –Scheduling cost and system call cost like creation or termination in the kernel is greater than those in run-time Recycle threads Existing problems –Fork –Signal
22
Figure 2-17. Multiplexing user-level threads onto kernel-level threads. Hybrid Implementations Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
23
Scheduler Activations Goal –Mimic the functionality of kernel threads –Maintain the better performance and greater flexibility of threads packages in user space Implementation –Avoid unnecessary transitions between user and kernel space –Upcall –Kernel => runtime Problem –Violate the principle of layered system structure
24
Figure 2-18. Creation of a new thread when a message arrives. (a) Before the message arrives. (b) After the message arrives. Pop-Up Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
25
Figure 2-19. Conflicts between threads over the use of a global variable. Making Single-Threaded Code Multithreaded (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
26
Figure 2-20. Threads can have private global variables. Making Single-Threaded Code Multithreaded (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
27
Making Single-Threaded Code Multithreaded (3) New scoping level –variables visible to all the procedures of a thread New library procedures –Create_global( ‘ bufptr ’ ); –Set_global( ‘ bufper ’, &buf); –Bufptr = read_global( ‘ bufptr ’ ); Reentrant –Communication buffer –Memory allocation Signal –Kernel hardly directs the signal to the right thread implemented entirely in user space –One thread is only able to process a signal at a time while multiple threads alarm independently –Keyboard interrupt Stack
28
Three basic issues: How to pass information among processes? How to avoid two or more processes getting in each other’s way? How to do proper sequencing when dependencies are present? InterProcess Communication Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
29
Figure 2-21. Two processes want to access shared memory at the same time. Race Conditions Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
30
Example Print spooler Race conditions definition Two or more processes are reading or writing some shared data and the final result depends on who runs precisely when. Avoid race condition Mutual exclusion: if one process is using a shard variable or file, the other processes will be excluded from doing the same thing. Race conditions Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
31
Definition of the critical regions: The part of the program where the shared memory is accessed. Conditions required to avoid race condition: No two processes may be simultaneously inside their critical regions. No assumptions may be made about speeds or the number of CPUs. No process running outside its critical region may block other processes. No process should have to wait forever to enter its critical region. Critical Regions (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
32
Figure 2-22. Mutual exclusion using critical regions. Critical Regions (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
33
Proposals for achieving mutual exclusion: Disabling interrupts Lock variables Strict alternation Peterson's solution The TSL instruction Mutual Exclusion with Busy Waiting Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
34
Figure 2-23. A proposed solution to the critical region problem. (a) Process 0. (b) Process 1. Strict Alternation Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 Busy waiting Violate condition 3
35
Figure 2-24. Peterson’s solution for achieving mutual exclusion. Peterson's Solution Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
36
Figure 2-25. Entering and leaving a critical region using the TSL instruction. The TSL Instruction (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 hardware instructions indivisible
37
Figure 2-26. Entering and leaving a critical region using the XCHG instruction. The TSL Instruction (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
38
Disadvantages of busy waiting approaches: Waste CPU time Priority inversion problem More solutions: Sleep and wakeup Semaphore Mutex Monitor Message passing Barrier More solutions Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
39
Figure 2-27. The producer-consumer problem with a fatal race condition. Example: The Producer-Consumer Problem ( bounded-buffer problem ) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
40
Figure 2-28. The producer-consumer problem using semaphores. Semaphores Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 semaphore: a variable used to count the number of wakeups saved for future use. “down/up” = “P/V”: atomic action / indivisible
41
Figure 2-29. Implementation of mutex lock and mutex unlock. Mutexes Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 A mutex is a variable that can be one of two states: unlocked or locked A mutex is used to allow or block access to a critical region
42
Figure 2-30. Some of the Pthreads calls relating to mutexes. Mutexes in Pthreads (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
43
Figure 2-31. Some of the Pthreads calls relating to condition variables. Mutexes in Pthreads (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 Condition variables allow threads to block due to some condition not being met.
44
Figure 2-32. Using threads to solve the producer-consumer problem. Mutexes in Pthreads (3) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
45
Figure 2-33. A monitor. Monitors (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
46
Figure 2-34. An outline of the producer-consumer problem with monitors. Monitors (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
47
Figure 2-35. A solution to the producer-consumer problem in Java. Monitors (3) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
48
Figure 2-35. A solution to the producer-consumer problem in Java. Monitors (4) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
49
Figure 2-35. A solution to the producer-consumer problem in Java. Monitors (5) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
50
Figure 2-36. The producer-consumer problem with N messages. Producer-Consumer Problem with Message Passing (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
51
Figure 2-36. The producer-consumer problem with N messages. Producer-Consumer Problem with Message Passing (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
52
Figure 2-37. Use of a barrier. (a)Processes approaching a barrier. (b)All processes but one blocked at the barrier. (c)When the last process arrives at the barrier, all of them are let through. Barriers Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 Barrier is a synchronization mechanism for groups of processes. Barrier blocks the processes until all processes arrive.
53
Figure 2-38. Bursts of CPU usage alternate with periods of waiting for I/O. (a) A CPU-bound process. (b) An I/O-bound process. Scheduling – Process Behavior Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
54
When a new process is created When a process exits When a process blocks on I/O, on a semaphore, or for some other reason When an I/O interrupt occurs When the periodic clock interrupts occur Nonpreemptive scheduling Preemptive scheduling When to Schedule Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
55
Batch Interactive Real time Categories of Scheduling Algorithms Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
56
Figure 2-39. Some goals of the scheduling algorithm under different circumstances. Scheduling Algorithm Goals Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
57
First-come first-served nonpreemptive Shortest job first nonpreemptive Shortest remaining Time next preemptive Scheduling in Batch Systems Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
58
Figure 2-40. An example of shortest job first scheduling. (a) Running four jobs in the original order. Average turnaround=14 (b) Running them in shortest job first order. Average turnaround=11 Shortest Job First Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 Optimal Solution: (4a+3b+2c+d)/4
59
Round-robin scheduling Priority scheduling Multiple queues Shortest process next T0, T0/2+T1/2,T0/4+T1/4+T2/2… Guaranteed scheduling Ratio of actual CPU time consumed to CPU time entitled Lottery scheduling A process holding a fraction f of the tickets will get about a fraction f of the resource Fair-share scheduling Take into account the owner of the processes Scheduling in Interactive Systems Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
60
Figure 2-41. Round-robin scheduling. (a) The list of runnable processes. (b) The list of runnable processes after B uses up its quantum. Round-Robin Scheduling Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 quantum
61
Figure 2-42. A scheduling algorithm with four priority classes. Priority Scheduling Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 Adjust priorities: Decrease the priority of currently running process at each clock tick Assign a quantum to each process, switch processes when current quantum is used up
62
Hard real time / soft real time Periodic/ aperiodic Scheduable ∑C i /P i <= 1, (i=1..m) Example: 100, 200 and 500 msec of periods; 50, 30 and 100 msec of CPU time; 0.5+0.15+0.2 < 1; 1sec / 150 msec; Static scheduling algorithm RMS (Rate Monotonic Scheduling) Dynamic scheduling algorithm EDF (Earliest Deadline First) Scheduling in Real-Time System Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
63
(a) Possible scheduling of user-level threads with a 50-msec process quantum and threads that run 5 msec per CPU burst. (b) Possible scheduling of kernel-level threads with the same characteristics as (a). Thread Scheduling Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639 Figure 2-43 (a) Figure 2-43 (b)
64
Classical IPC Problems
65
Figure 2-44. Lunch time in the Philosophy Department. Dining Philosophers Problem (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
66
Figure 2-45. A nonsolution to the dining philosophers problem. Dining Philosophers Problem (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
67
Figure 2-46. A solution to the dining philosophers problem. Dining Philosophers Problem (3) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639...
68
Figure 2-46. A solution to the dining philosophers problem. Dining Philosophers Problem (4) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639...
69
Figure 2-46. A solution to the dining philosophers problem. Dining Philosophers Problem (5) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639...
70
Figure 2-47. A solution to the readers and writers problem. The Readers and Writers Problem (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639...
71
Figure 2-47. A solution to the readers and writers problem. The Readers and Writers Problem (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.