1 / 11

POSIX SEMAPHORES

POSIX SEMAPHORES. Mutex Reminder. mutex is a lock that only one thread can "hold" at any given time If a thread tries to acquire the lock while another thread "holds" it, that thread will be blocked until the lock is released

ksand
Download Presentation

POSIX 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. POSIX SEMAPHORES

  2. Mutex Reminder • mutex is a lock that only one thread can "hold" at any given time • If a thread tries to acquire the lock while another thread "holds" it, that thread will be blocked until the lock is released • This mechanism is quite helpful when you want only one thread executing certain code at a given time

  3. Introducing Semaphores • A semaphore is a counter that can be used to keep track of resources in threaded applications. • It basically represents a non-negative value that can be signaled or waited on • The value of the semaphore can be thought of as the number of unused resources • When this number reaches zero, a thread that wishes to use a resource must wait until one is available.

  4. Simple Semaphore Implementation struct semaphore { int val; thread_list waiting; // List of threads waiting for semaphore } wait(semaphore Sem): // Wait until > 0 then decrement while (Sem.val <= 0) { add this thread to Sem.waiting; block(this thread); } Sem.val = Sem.val -1; return; post(semaphore Sem): // Increment value and wake up next thread Sem.val = Sem.val + 1; if (Sem.waiting is nonempty) { remove a thread T from Sem.waiting; wakeup(T); }

  5. Why to use semaphores? • Semaphores impose deliberate constraints that help programmers avoid errors. • Solutions using semaphores are often clean and organized, making it easy to demonstrate their correctness. • Semaphores can be implemented efficiently on many systems, so solutions that use semaphores are portable and usually efficient.

  6. Semaphore Implementation • How do we ensure that the semaphore implementation is atomic? • One approach: Make them system calls, and ensure only one P() or V() operation can be executed by any process at a time. • This effectively puts a lock around the P() and V() operations themselves! • Easy to do by disabling interrupts in the P() and V() calls. • Another approach: Use hardware support • Say your CPU had atomic P and V instructions • That would be sweet.

  7. The Producer - Consumer Problem • Producer pushes items into the buffer. • Consumer pulls items from the buffer. • Producer needs to wait when buffer is full. • Consumer needs to wait when the buffer is empty.

  8. Consumer() { int item; while (TRUE) { P(full); P(mutex); item = remove_item(); V(mutex); V(empty); eat(item); } } Semaphore mutex = 1;Semaphore empty = N;Semaphore full = 0; • Producer() { int item; while (TRUE) { item = bake(); P(empty); P(mutex); insert_item(item); V(mutex); V(full); } }

  9. Create a semaphore • int sem_init (sem_t* sem, int pshared, unsigned int value); • sem_t - the semaphore to be initialized • pshared - indicates whether this semaphore is to be shared between the threads of a process, or between processes • value - the initial value of the semaphore • All semaphore functions return zero on success and non-zero on failure.

  10. Semaphore Operations • int sem_post(sem_t * sem); • This will increase the value of the semaphore by one. • int sem_wait (sem_t* sem); • This will return immediately if the value of the semaphore is greater than zero and block the thread otherwise. It decreases the semaphore by one.

  11. Semaphore Operations • int sem_trywait(sem_t * sem); • Version of the sem_wait which not blocks. Decreases the semaphore by one if the semaphore does not equal to zero. If it is zero it does not block, returns zero with error code EAGAIN • int sem_destroy(sem_t * sem); • It releases the resources that semaphore has and destroys it. • int sem_getvalue(sem_t * sem, int * semdeg); • The current semaphore value is stored in semdeg variable.

More Related