1 / 22

Lab #9 Semaphores

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:

drake
Download Presentation

Lab #9 Semaphores

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab #9Semaphores Operating System Lab

  2. 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. 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 = counter register1 = register1 + 1 counter= register1 “counter--” is implemented as register2 = counter register2 = register2 - 1 counter= register2 NCHU System & Network Lab

  4. Race Condition (cont.) 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. Normal Execution Critical Section Blocked on semaphore B S.L S.value A 1 wait(S) 0 wait(S) Critical Section B -1 signal(S) Critical Section 0 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. 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

  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. 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. 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 NCHU System & Network Lab

More Related