Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab #9 Semaphores Operating System Lab.

Similar presentations


Presentation on theme: "Lab #9 Semaphores Operating System Lab."— Presentation transcript:

1 Lab #9 Semaphores Operating System Lab

2 NCHU System & Network Lab
Race Condition Race condition: A situation that several tasks access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access take place. Example: Suppose that the value of the variable counter = 5. Process 1 and process 2 execute the statements “counter++” and “counter--” concurrently. Following the execution of these two statements, the value of the variable counter may be 4, 5, or 6! NCHU System & Network Lab

3 NCHU System & Network Lab
Race Condition (cont.) “counter++” is implemented as register1 = counter register1 = register1 + 1 counter = register1 “counter--” is implemented as register2 = counter register2 = register2 - 1 counter = register2 The concurrent execution of “counter++” and “counter--” is equivalent to a sequential execution where the low-level statements are interleaved in some arbitrary order. NCHU System & Network Lab

4 NCHU System & Network Lab
Race Condition (cont.) Thread 1 register1 Thread 2 register2 counter register1 =counter 5 --- register1=register1 + 1 6 register2 =counter register2=register2 − 1 4 counter =register1 counter =register2 NCHU System & Network Lab

5 Process Synchronization
To guard against the race condition mentioned above, we need to ensure that only one process at a time can be manipulating the variable counter. To make such a guarantee, we require some form of synchronization for the processes-semaphore Semaphore S: An integer variable that, apart from initialization, is accessed only through two standard atomic operations: wait and signal. NCHU System & Network Lab

6 Process Synchronization (cont.)
A blocking implementation of semaphores. typedef struct { int value; struct process *L; }semaphore; void wait(semaphore S) { S.value--; if(S.value < 0) { add this process to S.L; block(); } void signal(semaphore S) { S.value++; if(S.value <= 0) { remove a process P from S.L; wakeup(P); } NCHU System & Network Lab

7 Process Synchronization (cont.)
The following pseudocode protects a critical section if the semaphore variable S is initially 1. wait(&S); /* entry section */ <critical section> signal(&S); /* exit section */ <remainder section> NCHU System & Network Lab

8 NCHU System & Network Lab
Normal Execution Critical Section Blocked on semaphore S.L S.value A B 1 wait(S) wait(S) Critical Section B -1 signal(S) Critical Section signal(S) 1 NCHU System & Network Lab

9 POSIX Semaphores Declare a semaphore variable called sem. Operations:
Use <semaphore.h>

10 POSIX Semaphores sem_init: Initialize the semaphore referenced by sem to value. The semaphores must be initialized before they used. value: cannot be negative. pshared: A flag indicating whether or not the semaphore should be shared with forked processes. Pshared == 0 only threads of process creating semaphore can use semaphore.

11 POSIX Semaphores (cont.)
sem_destroy: Destroy a previously initialized semaphore referenced by the sem parameter. sem_getvalue: Get the current value of sem and places it in the location pointed to by val NCHU System & Network Lab

12 Examples sem_init sem_destroy sem_getvalue

13 POSIX Semaphores (cont.)
sem_post: Implement classic semaphore signaling. It increments the value of the semaphore and wakes up a blocked process waiting on the semaphore, if any. NCHU System & Network Lab

14 POSIX Semaphores (cont.)
sem_wait: Implement the classic semaphore wait operation. NCHU System & Network Lab

15 Example #include <semaphore.h> #include <pthread.h>
#include <stdio.h> void *PrintHello(void *arg); int count; int main (int argc, char *argv[]) { sem_t semaphore; ... /* call sem_init before creating any threads. */ if (sem_init(&semaphore, 0, 1) == -1) { printf(“Failed to initialize semaphore.”); exit(-1); } rc = pthread_create(&thread, NULL, PrintHello, (void *)t); rc = pthread_join(thread, NULL); return 0;

16 NCHU System & Network Lab
Example (cont.) void *PrintHello(void *arg) { sem_wait(...); /* Entry section */ /* start of critical section */ ... count++; sem_post(...); /* Exit section */ pthread_exit(NULL); } NCHU System & Network Lab

17 Solution to the Race Condition
Thread 1 register1 Thread 2 register2 counter sem_wait(&semA); --- 5 register1 =counter /* blocked */ register1=register1 + 1 6 counter =register1 sem_post(&semA); register2 =counter register2=register2 − 1 counter =register2

18 Lab I – Binary Semaphore
Write programs using semaphores to solve the race condition occurred in your previous lab and observe the differences between these two outcomes. NCHU System & Network Lab

19 Lab II – Counting Semaphore
Counting semaphores can be used to control access to a given resource consist 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() on the semaphore. When a process releases a resource, it performs a signal(). When the count for the semaphore goes to 0, all resources are being used. NCHU System & Network Lab

20 Lab II – Counting Semaphore (cont.)
Create 15 threads that share a buffer with four slots. Each thread sleeps for a random period of time and will attempt to insert their thread id into the buffer. When an insertion gets successful, print out the value of the buffer. Then, after a random period of time, it releases its slot to other threads who are waiting for using it. Print the value of the buffer if any change and count the number of the slots that have been used. NCHU System & Network Lab

21 NCHU System & Network Lab
References Avi Silberschatz, Peter Baer Galvin and Greg Gagne, “Operating System Concepts,” John Wiley & Sons, 6th Edition, 2001 “Unix Systems Programming: Communication, Concurrency, and Threads” by Kay A. Robbins, Steven Robbins Neil Matthew and Richard Stones, “Beginning Linux Programming,” Wiley publishing, 3rd Edition, 2004 W. Richard Stevens, “Advanced Programming in the UNIX Environment,” Addison-Wesley, 1992 NCHU System & Network Lab

22 NCHU System & Network Lab
References (cont.) IBM Synchronizing Threads with POSIX Semaphores NCHU System & Network Lab


Download ppt "Lab #9 Semaphores Operating System Lab."

Similar presentations


Ads by Google