CSEN 602

Processes

Dr. Mahmoud Khalil

1 Announcement

• The Second Quiz will be on Saturday July 21, 2018 starting at 12:00.

• The quiz covers the synchronization.

2 Inter-Process Communication (IPC): Semaphores

• A physical is a signaling system that uses visual communication. • A software semaphore serves the same purpose, albeit using non-visual means. • It is used to: – ensure – send signals from one process to another.

3 Semaphores

• A semaphore is an integer variable with the following three operations. 1. Initialize: You can initialize the semaphore to any nonnegative value. 2. Decrement: A process can decrement the semaphore, if its value is positive. If value is 0, the process blocks. It is said to be sleeping on the semaphore. 3. Increment: If value is 0 and some processes are sleeping on the semaphore, one is unblocked. Otherwise, value is incremented. • All semaphore operations are implemented as atomic actions.

4 Semaphores

The producer-consumer problem using semaphores 5 Dining Philosophers (1)

• Philosophers eat/think • Eating needs 2 forks • Pick one fork at a time • How to prevent

6 Dining Philosophers (2)

A nonsolution to the dining philosophers problem

7 Dining Philosophers (3)

Solution to dining philosophers problem (part 1) 8 Dining Philosophers (4)

Solution to dining philosophers problem (part 2) 9 The Readers and Writers Problem

• It models access to a database. • Many competing processes wishing to read and write database. • It is acceptable to have multiple processes reading the database at the same time, but if one process is updating (writing) the database, no other processes may have access to the database, not even readers.

10 The Readers and Writers Problem

A solution to the readers and writers problem 11 The (1)

12 The Sleeping Barber Problem (2) • A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. • If there are no customers to be served, the barber goes to sleep. • If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. • If the barber is busy but chairs are available, then the customer sits in one of the free chairs. • If the barber is asleep, the customer wakes up the barber.

13 The Sleeping Barber Problem (3)

Solution to sleeping barber problem. 14 Monitors

• Need a more structured way to synchronize. • A monitor is a programming language construct, similar to a class. • A process that calls a monitor method is said to be in the monitor. • The makes sure that, at any time, only one process may be in the monitor. • Access to critical regions is done through monitor procedures. • If a process sleeps in a monitor, other processes are allowed to enter it.

15 Condition Variables

• There are many cases where a wishes to check whether a condition is true before continuing its execution. •Example: – A parent thread wishes to check whether a spawned child thread is complete. – How should the parent wait?

16 Condition Variables

• Operated on by two functions • cwait( c ) – Suspend execution of calling process on condition c • csignal( c ) – Resume execution of blocked process waiting on condition c

17 Monitors

Example of a monitor 18 Monitors

• Outline of producer-consumer problem with monitors – only one monitor procedure active at one time – buffer has N slots 19