Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Process Synchronization – Part II

Similar presentations


Presentation on theme: "Chapter 5: Process Synchronization – Part II"— Presentation transcript:

1 Chapter 5: Process Synchronization – Part II
Modified by Dr. Neerja Mhaskar for CS 3SH3

2 Semaphore wait() and signal() wait() operation: wait(S) {
Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer variable (usually initialized with a +ve integer) Can only be accessed via two indivisible (atomic) operations wait() and signal() Originally called P() and V() wait() operation: wait(S) { while (S <= 0) ; // busy wait S--; } signal() operation: signal(S) { S++;

3 Semaphore Usage Counting semaphore – integer value can range over an unrestricted domain Binary semaphore – integer value can range only between 0 and 1 Same as a mutex lock Semaphores can solve various synchronization problems Used as locks (binary semaphores) To solve synchronization problems (may or may not access shared data) Used to control access to a given resource consisting of a finite number of instances (counting semaphore)

4 Semphores – synchronization problems
Consider two concurrently running processes P1 and P2 that require S1 to happen before S2 (here P1 and P2 may or may not access/modify shared variables.) Solution: Create a semaphore “synch” initialized to 0 P1: S1; signal(synch); P2: wait(synch); S2;

5 Semaphore - Control access to resources
Counting semaphores can be used to control access to a given resource consisting of a finite number of instances. The semaphore is initialized to the number of resources available. Each process that wishes to use a resource performs a wait() operation on the semaphore (thereby decrementing the count). When a process releases a resource, it performs a signal() operation (incrementing the count). When the count for the semaphore goes to 0, all resources are being used. After that, processes that wish to use a resource will block until the count becomes greater than 0.

6 Semaphores – No busy waiting
Semaphores described so far suffer from busy waiting. Alternate solution: The process waiting on a semaphore is blocked, and removed from execution on the CPU Later awakened when the semaphore is available. As a result, each semaphore needs to maintain a list of processes that are blocked waiting for it, so that one of the processes can be woken up and swapped back in when the semaphore becomes available.

7 Semaphore Implementation with no Busy waiting
With each semaphore there is an associated waiting queue Each semaphore has two data items: value (of type integer) pointer to the list of processes waiting wait() and signal() operations are atomic Two additional operations: block() and wakeup() Semaphore Structure typedef struct{ int value; struct process *list; } semaphore;

8 Implementation with no Busy waiting (Cont.)
Wait Operation wait(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); } Signal Operation signal(semaphore *S) { S->value++; if (S->value <= 0) { remove a process P from S->list; wakeup(P); 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

9 Deadlock and Starvation
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 P P1 wait(S); wait(Q); wait(Q); wait(S); signal(S); signal(Q); signal(Q); signal(S); Starvation – indefinite blocking A process may never be removed from the semaphore queue in which it is suspended Priority Inversion – Scheduling problem when lower-priority process holds a lock needed by higher-priority process Solved via priority-inheritance protocol

10 Classical Problems of Synchronization
Bounded-Buffer Problem (Producer - Consumer Problem) Readers and Writers Problem Dining-Philosophers Problem

11 Bounded-Buffer 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

12 Bounded Buffer Problem (Cont.)
The structure of the producer process do { /* produce an item in next_produced */ ... wait(empty); wait(mutex); /* add next produced to the buffer */ signal(mutex); signal(full); } while (true);

13 Bounded Buffer Problem (Cont.)
The structure of the consumer process Do { wait(full); wait(mutex); /* remove an item from buffer to next_consumed */ ... signal(mutex); signal(empty); /* consume the item in next consumed */ } while (true);

14 Bounded Buffer, One Producer, One Consumer
Pi,j denotes the jth instruction in Producer i Ci,j denotes the jth instruction in Consumer i In the table below, each column represents a process and its line of execution. The rows represent semaphores (values and list) after each execution Buffer size =n=5. Initially Empty.v = 5 and Full.v = 0 The last row shows which process is in the critical section Mutex.v=1 indicates mutex is available, and Mutex.v=0 indicates it is unavailable. P0,1 P0,2 C0,1 P0,3 P0,4 C0,2 C0,3 C0,4 Mutex.value 1 Mutex list Empty.value 4 5 Empty.list Full.value -1 Full.list C0 CS P0

15 Dining-Philosophers Problem
Philosophers spend their lives alternating thinking and eating Don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl Need both chopsticks to eat, then release both when done In the case of 5 philosophers Shared data Bowl of rice (data set) – assumed infinite Semaphore chopstick [5] initialized to 1 (indicates its available)

16 Dining-Philosophers Problem Algorithm
The structure of Philosopher i: do { wait (chopstick[i] ); wait (chopstick[ (i + 1) % 5] ); // eat signal (chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE); What is the problem with this solutions?

17 Dining-Philosophers Problem Algorithm
Consider the following scenario in which all the Philosophers try to eat at the same time. Each philosopher executes its first instruction, and therefore takes the chopstick in front of him. Before each philosopher gets to execute its next statement (get the next chopstick) the philosopher adjacent to it executes it first statement. This results in a Deadlock. P0,1 P1,1 P2,1 P3,1 P4,1 Chopstick[0] Chopstick[1] 1 Chopstick[2] Chopstick[3] Chopstick[4]

18 Dining-Philosophers Problem Algorithm (Cont.)
Deadlock can be handled by Allow at most 4 philosophers to be sitting simultaneously at the table. Allow a philosopher to pick up the forks only if both are available (picking must be done in a critical section. Use an asymmetric solution -- an odd-numbered philosopher picks up first the left chopstick and then the right chopstick. Even-numbered philosopher picks up first the right chopstick and then the left chopstick.

19 Pthread and Semaphores
Semaphores are not part of Pthread standards and instead they belong to the POSIX SEM extension. To use semaphores in C on Linux to include ‘semaphore.h’ header file: #include <semaphore.h> sem_t data type for semaphores. semaphores created with sem_init() function. Sem_init() - Takes 3 arguments: Pointer to the semaphore Flag indicating the level of sharing The semaphore’s initial value sem_wait(); sem_post();

20 Pthread Semaphore Example
sem_init arguments 1. pointer to the semaphore 2. flag indicating the level of sharing 3. The semaphore’s initial value /*Declaring Semaphore*/ Sem_t sem; /*Initialize Semaphore*/ if (sem_init(&sem, 0, n) !=0){ printf("Error in initializing empty semaphore \n"); } /* acquire the semaphore */ sem_wait(&sem); /* critical section */ /* release the semaphore */ sem_post(&sem); Returns 0 when semaphore created with no errors, otherwise returns non zero value.

21 Problems with Semaphores
Incorrect use of semaphore operations: signal (mutex) …. wait (mutex) wait (mutex) … wait (mutex) Omitting of wait (mutex) or signal (mutex) (or both) Deadlock and/or starvation is possible.

22 End of Chapter 5


Download ppt "Chapter 5: Process Synchronization – Part II"

Similar presentations


Ads by Google