You are on page 1of 20

PRESENTED BY

ROHIT GOYAL

The Dutch scientist E.W. Dijkstra showed how to solve the Critical Section Problem in the mid-sixties, and introduced the concept of a semaphore to control synchronization.

Critical Section Problem


Problem: Design a protocol for processes to

cooperate, such that only one process is in its critical section How to make multiple instructions seem like one?
Process 1 Process 2 CS1 CS2

Time

There Are Some Protocols developed for solving the Critical Section Problem

1. Mutual Exclusion. If process (let Pi) is executing in its critical section, then no other processes can be executing in their critical sections. 2. Progress. If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely.
3. Bounded Waiting. A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.

The protocol developed for solving the Critical Section Problem involved three steps: Entry section: Before entering the critical section, a process must request permission to enter Critical section: After permission is granted, a process may execute the code in the critical section. Other processes respect the request, and keep out of their critical sections. Exit section: The process acknowledges it has left its critical section.

Semaphores are devices used to help with synchronization. If multiple processes share a common resource, they need a way to be able to use that resource without disrupting each other. A semaphore will either allow or disallow access to the resource, depending on how it is set up.

One example setup would be a semaphore which allowed any number of processes to read from the resource, but only one could ever be in the process of writing to that resource at a time.

Non-negative integer with atomic increment and decrement Integer S that can only be modified by:

P(S) or S.wait(): decrement or block if already 0 V(S) or S.signal(): increment and wake up process if any

Originally, Dijkstra called the two special operations P (for proberen, the dutch word for test) and V (for verhogen, the dutch word to increment).

Counting Semaphores: Any integer Used for synchronization Binary Semaphores Value is limited to 0 or 1 mutex locks Used for mutual exclusion (mutex)

Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section. Could now have busy waiting in critical section implementation Little busy waiting if critical section rarely occupied

var mutex : semaphore


initially mutex = 1 Process Pi repeat wait(mutex); critical section signal(mutex); remainder section

With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: value (of type integer) pointer to next record in the list Two operations: block place the process invoking the operation on the appropriate waiting queue. wakeup remove one of processes in the waiting queue and place it in the ready queue.

Implementation of wait:
wait (S){ value--; if (value < 0) { add this process to waiting queue block(); } }

Implementation of signal:
Signal (S){ value++; if (value <= 0) { remove a process P from the waiting queue wakeup(P); }}

Deadlock two or more processes are waiting

indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P0 P1
wait (S); wait (Q); . . . signal (S); signal (Q); wait (Q); wait (S); . . . signal (Q); signal (S);

Starvation indefinite blocking. A process may

never be removed from the semaphore queue in which it is suspended.

Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N.

The structure of the producer process


do { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (true);

The structure of the consumer process


do { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item

} while (true);

You might also like