Lab 9 Semaphores Operating System Lab NCHU System

Lab #9 Semaphores Operating System Lab 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

Race Condition (cont. ) “counter++” is implemented as “counter--” is implemented as register 1 = counter register 1 = register 1 + 1 counter = register 1 register 2 = counter register 2 = register 2 - 1 counter = register 2 • 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

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

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

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

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

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

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

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.

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

Examples • sem_init • sem_destroy • sem_getvalue

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

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

Example #include <semaphore. h> #include <pthread. h> #include <stdio. h> void *Print. Hello(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, Print. Hello, (void *)t); . . . rc = pthread_join(thread, NULL); return 0; }

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

Solution to the Race Condition Thread 1 register 1 Thread 2 register 2 counter sem_wait(&sem. A); --- 5 --- sem_wait(&sem. A); 5 register 1 =counter 5 /* blocked */ --- 5 register 1=register 1 + 1 6 /* blocked */ --- 5 counter =register 1 6 /* blocked */ 6 sem_post(&sem. A); ------- register 2 =counter 6 6 register 2=register 2 − 1 5 6 counter =register 2 5 5 sem_post(&sem. A);

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

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

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

References • Avi Silberschatz, Peter Baer Galvin and Greg Gagne, “Operating System Concepts, ” John Wiley & Sons, 6 th 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, 3 rd Edition, 2004 • W. Richard Stevens, “Advanced Programming in the UNIX Environment, ” Addison-Wesley, 1992 NCHU System & Network Lab

References (cont. ) • IBM http: //publib. boulder. ibm. com/infocenter/iseries/v 5 r 3/index. jsp? t opic=/rzahwsemco. htm • Synchronizing Threads with POSIX Semaphores http: //www. csc. villanova. edu/~mdamian/threads/posixsem. html NCHU System & Network Lab
- Slides: 22