chapter 2 tanenbaum-pearce 6

47
MODERN OPERATING SYSTEMS (3 MODERN OPERATING SYSTEMS (3 rd rd Ed.) Ed.) ANDREW S. TANENBAUM ANDREW S. TANENBAUM Modified by S. Pearce Modified by S. Pearce Chapter 2 Chapter 2 Processes and Threads 6 Processes and Threads 6 Classical IPC Problems Classical IPC Problems Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0- 13-6006639

Upload: teyina

Post on 25-Oct-2014

235 views

Category:

Documents


8 download

DESCRIPTION

The widely anticipated revision of this worldwide best-seller incorporates the latest developments in operating systems technologies. The Third Edition includes up-to-date materials on relevant operating systems such as Linux, Windows, and embedded real-time and multimedia systems. Includes new and updated coverage of multimedia operating systems, multiprocessors, virtual machines, and antivirus software. Covers internal workings of Windows Vista (Ch. 11); unique even for current publications. Provides information on current research based Tanenbaum’s experiences as an operating systems researcher. A useful reference for programmers.

TRANSCRIPT

Page 1: Chapter 2 Tanenbaum-Pearce 6

MODERN OPERATING SYSTEMS (3MODERN OPERATING SYSTEMS (3rdrd Ed.) Ed.)ANDREW S. TANENBAUMANDREW S. TANENBAUM

Modified by S. PearceModified by S. Pearce

Chapter 2Chapter 2Processes and Threads 6Processes and Threads 6Classical IPC ProblemsClassical IPC Problems

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 2: Chapter 2 Tanenbaum-Pearce 6

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.

Page 3: Chapter 2 Tanenbaum-Pearce 6

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.• Summarize process behavior.

Page 4: Chapter 2 Tanenbaum-Pearce 6

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.• Summarize process behavior. • Describe the four principle classes of scheduling in

multiprogramming.

Page 5: Chapter 2 Tanenbaum-Pearce 6

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.• Summarize process behavior. • Describe the four principle classes of scheduling in

multiprogramming.• Summarize the goals and trade-offs.

Page 6: Chapter 2 Tanenbaum-Pearce 6

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.• Summarize process behavior. • Describe the four principle classes of scheduling in

multiprogramming.• Summarize the goals and trade-offs.• Scheduling in batch and real-time systems.

Page 7: Chapter 2 Tanenbaum-Pearce 6

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.• Summarize process behavior. • Describe the four principle classes of scheduling in

multiprogramming.• Summarize the goals and trade-offs.• Scheduling in batch and real-time systems.• Gnatt Charts

Page 8: Chapter 2 Tanenbaum-Pearce 6

Summary of “Processes and Threads 6”

• Summarize the classical IPC problems found throughout OS literature.

Page 9: Chapter 2 Tanenbaum-Pearce 6

Summary of “Processes and Threads 6”

• Summarize the classical IPC problems found throughout OS literature.

• Review the Dining Philosopher’s Problem.

Page 10: Chapter 2 Tanenbaum-Pearce 6

Summary of “Processes and Threads 6”

• Summarize the classical IPC problems found throughout OS literature.

• Review the Dining Philosopher’s Problem.• Review the Readers-Writer’s Problem.

Page 11: Chapter 2 Tanenbaum-Pearce 6

Summary of “Processes and Threads 6”

• Summarize the classical IPC problems found throughout OS literature.

• Review the Dining Philosopher’s Problem.• Review the Readers-Writer’s Problem.• Mention the Sleeping Barber Problem (Homework

problem from slide #55 of Tanenbaum-Pearce 4 PPT)

Page 12: Chapter 2 Tanenbaum-Pearce 6

Summary of “Processes and Threads 6”

• Summarize the classical IPC problems found throughout OS literature.

• Review the Dining Philosopher’s Problem.• Review the Readers-Writer’s Problem.• Mention the Sleeping Barber Problem (Homework

problem from slide #55 of Tanenbaum-Pearce 4 PPT)

NOTENOTE: The Producer-Consumer Problem of “Processes and Threads 4” introduced the issues that are fundamental to IPC, and is often regarded as so-called classical.

Page 13: Chapter 2 Tanenbaum-Pearce 6

The Dining Philosophers Problem

• It was originally formulated in 1965 by Edsger Dijkstra as a student exam exercise, in terms of computers competing for access to tape drive peripherals.

Page 14: Chapter 2 Tanenbaum-Pearce 6

The Dining Philosophers Problem

• It was originally formulated in 1965 by Edsger Dijkstra as a student exam exercise, in terms of computers competing for access to tape drive peripherals.

• Soon after, Tony Hoare gave the problem its present formulation.

Wikipedia

Page 15: Chapter 2 Tanenbaum-Pearce 6

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

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 16: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

Page 17: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

RULES:

1. Each philosopher must alternately think and eat.

Page 18: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

RULES:

1. Each philosopher must alternately think and eat.2. The spaghetti is unlimited.

Page 19: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

RULES:

1. Each philosopher must alternately think and eat.2. The spaghetti is unlimited.3. A philosopher can eat only while possessing two adjacent forks

simultaneously; one from the RHS and one from the LHS.

Page 20: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

RULES:

1. Each philosopher must alternately think and eat.2. The spaghetti is unlimited.3. A philosopher can eat only while possessing two adjacent forks

simultaneously; one from the RHS and one from the LHS.4. A philosopher can only acquire or relinquish forks individually.

Page 21: Chapter 2 Tanenbaum-Pearce 6

The only situation where two forks makes sense.

Page 22: Chapter 2 Tanenbaum-Pearce 6

2 forks should be 2 chopsticks

Page 23: Chapter 2 Tanenbaum-Pearce 6
Page 24: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

RULES:

1. Each philosopher must alternately think and eat.2. The spaghetti is unlimited.3. A philosopher can eat only while possessing two adjacent forks

simultaneously; one from the RHS and one from the LHS.4. A philosopher can only acquire or relinquish forks individually.

QUESTION: Can each philosophers indefinitely perform the task of thinking without starving according to this simple set of rules?

Page 25: Chapter 2 Tanenbaum-Pearce 6

See Figure 2.45 of Tanenbaum.

Simple-Minded Solution

Page 26: Chapter 2 Tanenbaum-Pearce 6

See Figure 2.45 of Tanenbaum.

Simple-Minded Solution

• Think until the left fork is available; when it is, pick it up• Think until the right fork is available; when it is, pick it up• Eat• Put the left fork down• Put the right fork down• Repeat from the start

Page 27: Chapter 2 Tanenbaum-Pearce 6

The following possibility exists: Each philosopher holds one LH-chopstick, waiting for the RH-chopstick to be relinquished - DEADLOCK.

Simple-Minded Solution

• Think until the left fork is available; when it is, pick it up• Think until the right fork is available; when it is, pick it up• Eat• Put the left fork down• Put the right fork down• Repeat from the start

Page 28: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

RULES:

1. Each philosopher must alternately think and eat.2. The spaghetti is unlimited.3. A philosopher can eat only while possessing two adjacent forks

simultaneously; one from the RHS and one from the LHS.4. A philosopher can only acquire or relinquish forks individually.

QUESTION: Can each philosophers indefinitely perform the task of thinking without starving according to this simple set of rules?

TWO MORE RULES: 5. It is impossible for deadlock to occur.

Page 29: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

SITUATION: Five silent philosophers sitting around a table of spaghetti with one fork place between each pair of adjacent philosophers:

RULES:

1. Each philosopher must alternately think and eat.2. The spaghetti is unlimited.3. A philosopher can eat only while possessing two adjacent forks

simultaneously; one from the RHS and one from the LHS.4. A philosopher can only acquire or relinquish forks individually.

QUESTION: Can each philosophers indefinitely perform the task of thinking without starving according to this simple set of rules?

TWO MORE RULES: 5. It is impossible for deadlock to occur. 6. It is impossible for starvation to occur.

Page 30: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

It turns out that any implementation involving limitations on the number of philosophers, a no-hold on chopsticks rule if the other chopstick is not available, or a block/sleep wait for a preset time period for the second chopstick will not be effective.

Page 31: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Example 1: One semaphore per philosopher ensuring that only one philosopher can pick up a chopstick at any time. This fails if all philosophers put their LH chopstick down simultaneously (context switching occurs after down and so all of them will just sit there indefinitely with a LH chopstick in their hands).

Page 32: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Example 2: Use a single MUTEX semaphore enforcing the rule that only one philosopher can pick up a chopstick at any given time. The result removes starvation, but only one philosopher can eat at a time – wasteful.

Page 33: Chapter 2 Tanenbaum-Pearce 6

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

A good solutions was proposed by Dijkstra (1965) – introduce states as well as semaphores:

• THINKING• HUNGRY• EATING

Page 34: Chapter 2 Tanenbaum-Pearce 6

Figure 2-46. A deadlock-free solution for N dining philosophers, utilizing the array, state, to keep track of semaphores such that hungry philosophers can block if needed forks are busy.

Dining Philosophers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 35: Chapter 2 Tanenbaum-Pearce 6

Philosophers are now happy and prosperous.

Page 36: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

• The Dining Philosophers Problem pertains to modeling threads that compete for exclusive access to a common resource.

Page 37: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

• The Dining Philosophers Problem pertains to modeling threads that compete for exclusive access to a common resource.

• The classic model of access to databases is the Readers and Writers Problem.

Page 38: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Situation:

•A set of threads share a common memory.

Page 39: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Situation:

•A set of threads share a common memory. •The set of threads are partitioned into “readers” and “writers” – readers only read and writers only write.

Page 40: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Situation:

•A set of threads share a common memory. •The set of threads are partitioned into “readers” and “writers” – readers only read and writers only write.

• NOTE: We can, in fact, define a thread that both reads and writes but it must be considered a writer.

Page 41: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Situation:

•A set of threads share a common memory. •The set of threads are partitioned into “readers” and “writers” – readers only read and writers only write.

• NOTE: We can, in fact, define a thread that both reads and writes but it must be considered a writer.

•There is no problem if two or more readers read from memory simultaneously.

Page 42: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Situation:

•A set of threads share a common memory. •The set of threads are partitioned into “readers” and “writers” – readers only read and writers only write.

• NOTE: We can, in fact, define a thread that both reads and writes but it must be considered a writer.

•There is no problem if two or more readers read from memory simultaneously. •There are indeterminable problems, however, if a writer and a reader or two writers access memory simultaneously.

Page 43: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Situation:

•A set of threads share a common memory. •The set of threads are partitioned into “readers” and “writers” – readers only read and writers only write.

• NOTE: We can, in fact, define a thread that both reads and writes but it must be considered a writer.

•There is no problem if two or more readers read from memory simultaneously. •There are indeterminable problems, however, if a writer and a reader or two writers access memory simultaneously.

Conclusion: The writers must have exclusive access to memory.

Page 44: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

General Solution:

•There are actually several versions of this problem (eg. The First, Second, and Third Readers/Writers Problems).

Page 45: Chapter 2 Tanenbaum-Pearce 6

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

General Solution:

•There are actually several versions of this problem (eg. The First, Second, and Third Readers/Writers Problems).

•A data structure (readers-writer lock) solves all of the readers-writers problems.

Page 46: Chapter 2 Tanenbaum-Pearce 6

Figure 2-47. A solution to the readers and writers problem.

The Readers and Writers Problem

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639

Page 47: Chapter 2 Tanenbaum-Pearce 6

END

PRESENTATION