lab #9 semaphores
DESCRIPTION
Lab #9 Semaphores. Operating System 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: - PowerPoint PPT PresentationTRANSCRIPT
NCHU System & Network LabNCHU System & Network Lab
Lab #9Lab #9SemaphoresSemaphores
Operating System Lab
NCHU System & Network LabNCHU System & Network Lab
Race ConditionRace 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 LabNCHU System & Network Lab
Race Condition (cont.)Race Condition (cont.)
• The concurrent execution of “counter++” and “counter--” is equivalent to a sequential execution where the low-level statements are interleaved in some arbitrary order.
“counter++” is implemented as
register1 = counterregister1 = register1 + 1counter = register1
“counter--” is implemented as
register2 = counterregister2 = register2 - 1counter = register2
NCHU System & Network LabNCHU System & Network Lab
Race Condition (cont.)Race Condition (cont.)
Thread 1 register1 Thread 2 register2 counter
register1 =counter 5 --- --- 5
register1=register1 + 1 6 --- --- 5
--- --- register2 =counter 5 5
------ register2=register2 − 1 4 5
counter =register1 6 --- --- 6
--- --- counter =register2 4 4
NCHU System & Network LabNCHU System & Network Lab
Process SynchronizationProcess 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 LabNCHU System & Network Lab
Process Synchronization (cont.)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 LabNCHU System & Network Lab
Process Synchronization (cont.)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 LabNCHU System & Network Lab
S.L AS.value B
wait(S)
0
-1
0
1
1
wait(S)
signal(S)
B
signal(S)
Normal Execution
Critical Section
Blocked onsemaphore
Critical Section
Critical Section
POSIX SemaphoresPOSIX Semaphores
• Declare a semaphore variable called sem.
• Operations:
• Use <semaphore.h>
POSIX SemaphoresPOSIX 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.
NCHU System & Network LabNCHU System & Network Lab
POSIX Semaphores (cont.)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
ExamplesExamples
• sem_init
• sem_destroy
• sem_getvalue
NCHU System & Network LabNCHU System & Network Lab
POSIX Semaphores (cont.)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 LabNCHU System & Network Lab
POSIX Semaphores (cont.)POSIX Semaphores (cont.)
• sem_wait: Implement the classic semaphore wait operation.
ExampleExample#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;
}
NCHU System & Network LabNCHU System & Network Lab
Example (cont.)Example (cont.)
void *PrintHello(void *arg){
sem_wait(...); /* Entry section */
/* start of critical section */ ... count++;
sem_post(...); /* Exit section */ pthread_exit(NULL);}
Solution to the Race ConditionSolution to the Race Condition
Thread 1 register1 Thread 2 register2 counter
sem_wait(&semA); --- 5
--- sem_wait(&semA); 5
register1 =counter 5 /* blocked */ --- 5
register1=register1 + 1 6 /* blocked */ --- 5
counter =register1 6 /* blocked */ 6
sem_post(&semA); /* blocked */ 6
--- register2 =counter 6 6
--- register2=register2 − 1 5 6
--- counter =register2 5 5
sem_post(&semA);
NCHU System & Network LabNCHU System & Network Lab
Lab I – Binary SemaphoreLab 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 LabNCHU System & Network Lab
Lab II – Counting SemaphoreLab 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 LabNCHU System & Network Lab
Lab II – Counting Semaphore (cont.)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 LabNCHU System & Network Lab
ReferencesReferences
• 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 LabNCHU System & Network Lab
References (cont.)References (cont.)
• IBM
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzahw/rzahwsemco.htm
• Synchronizing Threads with POSIX Semaphores
http://www.csc.villanova.edu/~mdamian/threads/posixsem.html