Chapter 2 Tanenbaum-Pearce 6

Preview:

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.

Citation preview

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

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.

Overview of “Processes and Threads 5”

• Introduce scheduling algorithms.• Summarize process behavior.

Overview of “Processes and Threads 5”

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

multiprogramming.

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.

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.

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

Summary of “Processes and Threads 6”

• Summarize the classical IPC problems found throughout OS literature.

Summary of “Processes and Threads 6”

• Summarize the classical IPC problems found throughout OS literature.

• Review the Dining Philosopher’s Problem.

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.

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)

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.

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.

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

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

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:

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.

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.

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.

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.

The only situation where two forks makes sense.

2 forks should be 2 chopsticks

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?

See Figure 2.45 of Tanenbaum.

Simple-Minded Solution

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

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

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.

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.

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.

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).

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.

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

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

Philosophers are now happy and prosperous.

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 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.

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 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.

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.

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.

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.

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.

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).

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.

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

END

PRESENTATION

Recommended