Chapter 02
Chapter 02
Third Edition
ANDREW S. TANENBAUM
Chapter 2
Processes and Threads
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
outline
Process
Threads
Process scheduling
Race condition/synchronization
pseudoparallelism
Execution non-reproducible
Non-reproducible
Program B
repeat print(N);N:=0;
Program A
repeat N:=N+1;
N: N 1 print(N); N:=0;
10
print(N); N:=0; N: N 1
print(N); N: N 1 N:=0;
10
then, N is:n 1 n
then N is: n 0 1
then N is: n n
Another example:
Consider an Analogy
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:
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process States
Modeling Multiprogramming
summary
Pseudoparallelism
Difference between process and program
Lifecycle of process
Multiprogramming degree
Thread
Threads: Benefits
User responsiveness
Speed
Threads: Drawbacks
Synchronization
Lack of independence
Interprocess Communication
Race Conditions
Therac-25
http://doi.ieeecomputersociety.org/10.1109/MC.1993.274940
Concepts
Race condition
Concetps
Mutual exclusion
Critical region
3.
4.
30
Disable interrupt
Lock variable
A software solution
A single, shared variable (lock), before entering
critical region, programs test the variable, if 0, no
contest; if 1, the critical region is occupied
What is the problem?
An analogy: the notepad on the door
(b) Process 1.
Concepts
Busy waiting
Spin lock
Wakeup sleep
Producer-consumer problem
A simple solution
Producer-Consumer Problem
Signal missing
Semaphore
Down (P)
Up (v)
Semaphore
Semaphores
Mutexes
A problem
...
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
...
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
...
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
...
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
Reader-Writer Problem
scheduling
Note
Concepts
Preemptive algorithm
Nonpreemptive
Batch
Interactive
Real time
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
First-come first-served
Shortest job first
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
FCFS
Process
Arrive
time
Service
time
Start time
Finish
time
Turnar Weighted
ound turnaroun
d
100
101
100
101
102
100
100
100
102
202
199
1.99
FCFS
Disadvantage:
SJF
FCFS
Process
Arrive time
Service Time
Finish time
18
13
turnaround
16
weighted
2.67
3.1
1.5
2.25
2.1
finish
12
14
18
turnaround
10
11
14
weighted
5.5
3.5
2.8
Compare:
average
Round-robin scheduling
Priority scheduling
Multiple queues
Shortest process next
Guaranteed scheduling
Lottery scheduling
Fair-share scheduling
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Round-Robin Scheduling
Arrival time = 0
Time quantum =20
Process
Burst Time
P1
53
P2
17
P3
68
p4
24
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154162
Context switch
Priority scheduling
Example
E.g.:
Process
Burst Time
Priority
P1
10
P2
P3
P4
P5
Priority (nonpreemprive)
Multiple Queues
CTSS
Three queues:
Multiple queues
It is not good for process that firstly CPUbounded, but later interactive
T0 , T0 / 2 + T1 / 2, T0 / 4 + T1 / 4 + T2 / 2....
Aging:
Priority inversion
Priority inversion
E.g.:
Priority: P1<PRT3<PRT2
PRT3 preempt P1; PRT2 waits for P1;
PRT2 waits for PRT3
Priority Inversion in
Pathfinder
Priority Inversion in
Pathfinder
"information bus
Priority Inversion in
Pathfinder
Solution
Priority-inheritance (lending)
PRT2 lends its priory to P1, thus PRT3 could not preempt P1
E.g.:
Priority: P1<PRT3<PRT2
Priority Inversion
Ceiling Protocol
Exercises
Compare reading a file using a singlethreaded file server and a multithreaded one.
Exercises
Exercise
Round robin
Priority scheduling
FCFS
Shortest job first
Exercise
Exercise