MODERN OPERATING SYSTEMS Third Edition ANDREW S

Download Report

Transcript MODERN OPERATING SYSTEMS Third Edition ANDREW S

操作系统原理
OPERATING SYSTEM
Chapter 2
Processes and Threads
进程与线程
The Process Model
Figure 2-1. (a) Multiprogramming of four programs. (b) Conceptual
model of four independent, sequential processes. (c) Only
one program is active at once.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process Creation
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.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process Termination
Events which cause process termination:
•
•
•
•
Normal exit (voluntary).
Error exit (voluntary).
Fatal error (involuntary).
Killed by another process (involuntary).
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process States
Figure 2-2. A process can be in running, blocked, or ready state.
Transitions between these states are as shown.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Implementation of Processes (1)
Figure 2-3. The lowest layer of a process-structured operating
system handles interrupts and scheduling. Above that layer
are sequential processes.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Implementation of Processes (2)
Figure 2-4. Some of the fields of a typical process table entry.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Implementation of Processes (3)
Figure 2-5. Skeleton of what the lowest level of the operating
system does when an interrupt occurs.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Modeling Multiprogramming
Figure 2-6. CPU utilization as a function of the number of
processes in memory.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Usage (1)
Figure 2-7. A word processor with three threads.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Usage (2)
Figure 2-8. A multithreaded Web server.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Usage (3)
Figure 2-9. A rough outline of the code for Fig. 2-8. (a) Dispatcher
thread. (b) Worker thread.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Usage (4)
Figure 2-10. Three ways to construct a server.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Classical Thread Model (1)
Figure 2-11. (a) Three processes each with one thread. (b) One
process with three threads.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Classical Thread Model (2)
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.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Classical Thread Model (3)
Figure 2-13. Each thread has its own stack.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
POSIX Threads (1)
Figure 2-14. Some of the Pthreads function calls.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
POSIX Threads (2)
...
Figure 2-15. An example program using threads.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Implementing Threads in User Space
Figure 2-16. (a) A user-level threads package. (b) A threads
package managed by the kernel.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
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 ?
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
Hybrid Implementations
Figure 2-17. Multiplexing user-level threads
onto kernel-level threads.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
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
Pop-Up Threads
Figure 2-18. Creation of a new thread when a message arrives.
(a) Before the message arrives.
(b) After the message arrives.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Making Single-Threaded Code
Multithreaded (1)
Figure 2-19. Conflicts between threads
over the use of a global variable.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Making Single-Threaded Code
Multithreaded (2)
Figure 2-20. Threads can have private global variables.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
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
InterProcess Communication
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?
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Race Conditions
Figure 2-21. Two processes want to access
shared memory at the same time.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Race conditions
•
•
•
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.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Critical Regions (1)
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.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Critical Regions (2)
Figure 2-22. Mutual exclusion using critical regions.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutual Exclusion with Busy Waiting
Proposals for achieving mutual exclusion:
•
•
•
•
•
Disabling interrupts
Lock variables
Strict alternation
Peterson's solution
The TSL instruction
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Strict Alternation
•Busy waiting
•Violate condition 3
Figure 2-23. A proposed solution to the critical region problem.
(a) Process 0. (b) Process 1.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Peterson's Solution
Figure 2-24. Peterson’s solution for achieving mutual exclusion.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The TSL Instruction (1)
• hardware instructions
• indivisible
Figure 2-25. Entering and leaving a critical region
using the TSL instruction.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The TSL Instruction (2)
Figure 2-26. Entering and leaving a critical region
using the XCHG instruction.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
More solutions
Disadvantages of busy waiting approaches:
•
•
Waste CPU time
Priority inversion problem
More solutions:
•
•
•
•
•
•
Sleep and wakeup
Semaphore
Mutex
Monitor
Message passing
Barrier
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Example: The Producer-Consumer Problem
( bounded-buffer problem )
Figure 2-27. The producer-consumer problem
with a fatal race condition.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Semaphores
• semaphore:
a variable used to count
the number of wakeups
saved for future use.
• “down/up” = “P/V”:
atomic action / indivisible
Figure 2-28. The producer-consumer problem using semaphores.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutexes
• 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
Figure 2-29. Implementation of mutex lock and mutex unlock.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutexes in Pthreads (1)
Figure 2-30. Some of the Pthreads calls relating to mutexes.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutexes in Pthreads (2)
• Condition variables allow threads to block due to some condition not being met.
Figure 2-31. Some of the Pthreads calls relating
to condition variables.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutexes in Pthreads (3)
Figure 2-32. Using threads to solve the producer-consumer problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Monitors (1)
Figure 2-33. A monitor.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Monitors (2)
Figure 2-34. An outline of the producer-consumer problem with monitors.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Monitors (3)
Figure 2-35. A solution to the producer-consumer
problem in Java.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Monitors (4)
Figure 2-35. A solution to the producer-consumer
problem in Java.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Monitors (5)
Figure 2-35. A solution to the producer-consumer
problem in Java.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Producer-Consumer Problem with Message Passing (1)
Figure 2-36. The producer-consumer problem with N messages.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Producer-Consumer Problem with Message Passing (2)
Figure 2-36. The producer-consumer problem with N messages.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Barriers
• Barrier is a synchronization mechanism for groups of processes.
• Barrier blocks the processes until all processes arrive.
(c)
Figure 2-37. Use of a barrier.
(a) Processes approaching a barrier.
(b) All processes but one blocked at the barrier.
When the last process arrives at the barrier, all of them are let through.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling – Process Behavior
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.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
When to Schedule
•
•
•
•
•
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
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Categories of Scheduling Algorithms
•
•
•
Batch
Interactive
Real time
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling Algorithm Goals
Figure 2-39. Some goals of the scheduling algorithm under
different circumstances.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Batch Systems
•
•
First-come first-served
•nonpreemptive
Shortest job first
•nonpreemptive
•
Shortest remaining Time next
•preemptive
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Shortest Job First
Optimal Solution: (4a+3b+2c+d)/4
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
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Interactive Systems
•
•
•
•
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
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Round-Robin Scheduling
• quantum
Figure 2-41. Round-robin scheduling.
(a) The list of runnable processes. (b) The list of runnable
processes after B uses up its quantum.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Priority Scheduling
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
Figure 2-42. A scheduling algorithm with four priority classes.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Real-Time System
•
•
•
Hard real time / soft real time
Periodic/ aperiodic
Scheduable
• ∑Ci/Pi <= 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)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Scheduling
Figure 2-43 (a)
Figure 2-43 (b)
(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).
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Classical IPC Problems
Dining Philosophers Problem (1)
Figure 2-44. Lunch time in the Philosophy Department.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (2)
Figure 2-45. A nonsolution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (3)
...
Figure 2-46. A solution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (4)
...
...
Figure 2-46. A solution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem (5)
...
Figure 2-46. A solution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Readers and Writers Problem (1)
...
Figure 2-47. A solution to the readers and writers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Readers and Writers Problem (2)
...
Figure 2-47. A solution to the readers and writers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639