Counting And Binary Semaphore-Operating Systems-Lecture Notes, Study notes of Operating Systems

Operating Systems is necessary course in Computer Science. Its about threading, process scheduling, deadlocks, memory management etc. This lecture includes: Counting, Binary, Semaphore, Buffer, Dining, Classical, Operation, Implement, Signal, Initial

Typology: Study notes

2011/2012

Uploaded on 08/06/2012

sarang
sarang ๐Ÿ‡ฎ๐Ÿ‡ณ

4.5

(47)

86 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
114
Operating Systems [CS-604] Lecture No. 24
Operating Systems
Lecture No. 24
Reading Material
๎˜‚๎˜ƒChapter 7 of the textbook
๎˜‚๎˜ƒLecture 24 on Virtual TV
Summary
๎˜‚๎˜ƒCounting semaphores
๎˜‚๎˜ƒClassical synchronization problems
๎˜‚๎˜ƒBounded buffer problem
๎˜‚๎˜ƒReaders and writers problem
๎˜‚๎˜ƒDining philosophers problem
Semaphores
There are two kinds of semaphores:
๎˜‚๎˜ƒCounting semaphore whose integer value can range over an unrestricted integer
domain.
๎˜‚๎˜ƒBinary semaphore whose integer value cannot be > 1; can be simpler to
implement.
Let S be a counting semaphore. To implement it in terms of binary semaphores we
need the following data structures:
binary-semaphore S1, S2;
int C;
Initially S1=1, S2=0, and the value of integer C is set to the initial value of the counting
semaphore S. The wait operation on the counting semaphore S can be implemented as
follows:
wait(S1);
C--;
if(C < 0) {
signal(S1);
wait(S2);
}
signal(S1);
The signal operation on the counting semaphore S can be implemented as follows:
wait(S1);
C++;
if(C <= 0)
signal(S2);
else
signal(S1);
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Counting And Binary Semaphore-Operating Systems-Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems [CS-604] Lecture No. 24

Operating Systems

Lecture No. 24

Reading Material

Chapter 7 of the textbook Lecture 24 on Virtual TV

Summary

Counting semaphores Classical synchronization problems Bounded buffer problem Readers and writers problem Dining philosophers problem

Semaphores

There are two kinds of semaphores:  Counting semaphore whose integer value can range over an unrestricted integer domain.  Binary semaphore whose integer value cannot be > 1; can be simpler to implement. Let S be a counting semaphore. To implement it in terms of binary semaphores we need the following data structures: binary-semaphore S1, S2; int C;

Initially S1=1, S2=0, and the value of integer C is set to the initial value of the counting semaphore S. The wait operation on the counting semaphore S can be implemented as follows: wait(S1); C--; if(C < 0) { signal(S1); wait(S2); } signal(S1); The signal operation on the counting semaphore S can be implemented as follows: wait(S1); C++; if(C <= 0) signal(S2); else signal(S1);

Classic Problems of Synchronization

The three classic problems of synchronization are: Bounded-Buffer Problem Readers and Writers Problem Dining Philosophers Problem

Bounded Buffer Problem The bounded-buffer problem, which was introduced in a previous lecture, is commonly used to illustrate the power of synchronization primitives. The solution presented in this section assumes that the pool consists of n buffers, each capable of holding one item.

The mutex semaphore provides mutual exclusion for accesses to the buffer pool and is initialized to the value 1. The empty and full semaphores count the number of empty and full buffers, respectively. The semaphore empty is initialized to the value n; the semaphore full is initialized to the value 0. The code for the producer is as follows: do { ... produce an item in nextp ... wait(empty); wait(mutex); ... add nextp to buffer ... signal(mutex); signal(full); } while(1);

And that for the consumer is as follows:

Producer Consumer

Empty Pool

Full Pool

possible. In other words, if a writer is waiting to access the object, no new readers may start reading. A solution to either problem may result in starvation. In the first case, writers may starve; in the second case, readers may starve. For this reason, other variants of the problem have been proposed. In this section, we discuss a solution to the first readers- writers problem. In the solution to the first readers-writers problem, processes share the following data structures.

semaphore mutex, wrt; int readcount;

The semaphores mutex and wrt are initialized to 1; readcount is initialized to 0. The semaphore wrt is common to both the reader and writer processes. The mutex semaphore is used to ensure mutual exclusion when the reader processes update the readcount variable. The readcount variable keeps track of how many processes are currently reading the object. The wrt semaphore is used to ensure mutual exclusion for writers or a writer and readers. This semaphore is also used by the first and last readers to block entry of a writer into its critical section and to allow open access to the wrt semaphore, respectively. It is not used by readers who enter or exit, while at least one reader is in its critical sections. The codes for reader and writer processes are shown below:

wait(mutex); readcount++; if(readcount == 1) wait(wrt); signal(mutex); ... reading is performed ... wait(mutex); readcount--; if(readcount == 0) signal(wrt); signal(mutex);

wait(wrt); ... writing is performed ... signal(wrt);

Note that, if a writer is in the critical section and n readers are waiting, then one reader is queued on wrt, and n-1 readers are queued on mutex. Also observe that when a writer executes signal(wrt) we may resume the execution of either the waiting readers or a single waiting writer; the selection is made by the CPU scheduler.

Dining Philosophers Problem Consider five philosophers who spend their lives thinking and eating, as shown in the following diagram.

The philosophers share a common circular table surrounded by five chairs, each belonging to one philosopher. In the center of the table is a bowl of rice, and the table is laid with five single chopsticks.

When a philosopher thinks, she does not interact with her colleagues. From time to time, a philosopher gets hungry and tries to pick up the two chopsticks that are closest to her (the chopsticks that are between her and her left and right neighbors). A philosopher may pick up only one chopstick at a time. Obviously, she cannot pick up a chopstick that is already in the hand of her neighbor. When a hungry philosopher has both her chopsticks at the same time, she eats without releasing her chopsticks. When she is finished eating, she puts down both of her chopsticks and starts thinking again. The dining philosophers problem is considered to be a classic synchronization problem because it is an example of a large class of concurrency control problems. It is a simple representation of the need to allocate several resources among several processes in a deadlock and starvation free manner. One simple solution is to represent each chopstick by a semaphore. A philosopher tires to grab the chopstick by executing a wait operation on that semaphore; she releases her chopsticks by executing the signal operation on the appropriate semaphores. Thus the shared data are:

semaphore chopstick[5];

All the chopsticks are initialized to 1. The structure of philosopher i is as follows: