OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan...

30
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu [email protected] University of the Western Cape Computer Science Department

Transcript of OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan...

Page 1: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

OPERATING SYSTEMSDESIGN AND IMPLEMENTATION

Third EditionANDREW S. TANENBAUMALBERT S. WOODHULL

Yan hao (Wilson) [email protected]

University of the Western Cape

Computer Science Department

Page 2: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Producer-Consumer Problem

Producer Consumer

1N-1

20

Buffer

In Out

Description: Producer and consumer share a buffer with N slots

Producer: produce one data and put into a empty slot at one time. Block itself if no empty slot available

l

Consumer: consume one data a time. Block itself if no data available

Out = Out % NIn = In % N

Page 3: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Producer-Consumer Problem (1)

Figure 2-13. The producer-consumer problem with a fatal race condition.

Page 4: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Producer-Consumer Problem (2)

Figure 2-13. The producer-consumer problem with a fatal race condition

Page 5: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Producer-Consumer Problem

Mutual exclusionMutex: only one process or thread can operate a slot at a time

Synchronizationempty : indicate how many slots is empty nowfull : indicate how many slots is full now

relationship: when “empty = empty -1”, “full = full +1” (P V)

Page 6: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Monitors (1)

Figure 2-15. A monitor.

Page 7: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Monitors (2)

Figure 2-16. An outline of the producer-consumer problem with monitors. Only one monitor procedure at a time is active. The buffer has N slots

Page 8: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Producer-Consumer Problem (3)

Figure 2-14. The producer-consumer problem using semaphores.Note:when buffer is full, producer will block at the second down with mutex =0 -> Deadlock: Producer block by empty semaphore, Consumer block by mutex semaphore

Page 9: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Producer-Consumer Problem (4)

Figure 2-14. The producer-consumer problem using semaphores.

Page 10: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Message Passing (1)

Figure 2-17. The producer-consumer problem with N messages.

Page 11: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Message Passing (2)

Page 12: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Dining Philosophers Problem (1)

Figure 2-18. Lunch time in the Philosophy Department.

There are 5 philosophers sitting at a round table. Between each adjacent pair of philosophers is a chopstick. In other words, there are five chopsticks. Each philosopher does two things: think and eat. The philosopher thinks for a while, and then stops thinking and becomes hungry. When the philosopher becomes hungry, he/she cannot eat until he/she owns the chopsticks to his/her left and right. When the philosopher is done eating he/she puts down the chopsticks and begins thinking again.

Page 13: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Dining Philosophers Problem (2)

Figure 2-19. A nonsolution to the dining philosophers problem.

Deadlock->Starvation:

All the programs continue to run indefinitely but fail to make any progress is called starvation

Page 14: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Dining Philosophers Problem (3)

Page 15: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Dining Philosophers Problem (4)

Page 16: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Dining Philosophers Problem (5)

Figure 2-20. A solution to the dining philosophers problem.

Page 17: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Readers and Writers Problem (1)

Page 18: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

The Readers and Writers Problem (2)

Page 19: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Process Behavior (1)

Figure 2-22. Bursts of CPU usage alternate with periods of waiting for I/O. (a) A CPU-bound process. (b) An I/O-bound process.

Page 20: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

When to ScheduleWhen scheduling is absolutely required:

1. When a process exits.

2. When a process blocks on I/O, or a semaphore.

When scheduling usually done (though not absolutely required)

1. When a new process is created.

2. When an I/O interrupt occurs.

3. When a clock interrupt occurs.

Page 21: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Scheduling Algorithms (1)

Page 22: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Scheduling Algorithms (2)

Figure 2-24. An example of shortest job first scheduling.

(a) Running four jobs in the original order.

(b) Running them in shortest job first order.

Page 23: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Shortest Job First84 12 16 20 240

P1

P2

P3

P4

Turnaround time: P1 P2 P3 P4

average time:

Page 24: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Three Level Scheduling (1)

Figure 2-25. Three-level scheduling.

Page 25: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Three Level Scheduling (2)

Criteria for deciding which process to choose(memory scheduler):

How long has it been since the process was swapped in or out?

How much CPU time has the process had recently?

How big is the process?

How important is the process?

Page 26: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Round-Robin Scheduling

Figure 2-26. Round-robin scheduling.

(a) The list of runnable processes.

(b) The list of runnable processes after B uses up its quantum.

Page 27: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Priority Scheduling

Figure 2-27. A scheduling algorithm with four priority classes.

Page 28: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Real time scheduling

0 50 100

A:20ms 10msB:50ms 25ms

A

B

m

∑i=1

Ci

Pi

<=1Schedulable

Page 29: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Thread Scheduling (1)

Figure 2-28. (a) Possible scheduling of user-level threads with a 50-msec process quantum and threads that run 5 msec per CPU burst.

Page 30: OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu wwu@uwc.ac.za University of the Western.

Thread Scheduling (2)

Figure 2-28. (b) Possible scheduling of kernel-level threads with the same characteristics as (a).