0% found this document useful (0 votes)
35 views5 pages

5CS021 Numerical Methods and Concurrency: Thread Synchronization Using Semaphores

This document discusses using semaphores to synchronize threads. Semaphores allow multiple threads to access shared resources simultaneously without interfering with each other, unlike mutexes which only allow one thread at a time. It describes the sem_init, sem_destroy, sem_wait, and sem_post semaphore functions, and provides an example of using semaphores in a producer-consumer problem where multiple producer and consumer threads need synchronized access to a shared resource pool.

Uploaded by

Subiran Stha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

5CS021 Numerical Methods and Concurrency: Thread Synchronization Using Semaphores

This document discusses using semaphores to synchronize threads. Semaphores allow multiple threads to access shared resources simultaneously without interfering with each other, unlike mutexes which only allow one thread at a time. It describes the sem_init, sem_destroy, sem_wait, and sem_post semaphore functions, and provides an example of using semaphores in a producer-consumer problem where multiple producer and consumer threads need synchronized access to a shared resource pool.

Uploaded by

Subiran Stha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

5CS021 Numerical Methods

and Concurrency
Thread Synchronization Using Semaphores
Semaphores

• Mutexes allow only one thread to enter a


critical section at a time.
• This can have a significant performance
impact when there are lots of threads
• Semaphores are an alternative method of
synchronizing multiple threads
• This can allow multiple threads to access a
"pool" of resources at the same time without
interfering with each other.
Semaphore functions

• int sem_init (sem_t *sem, int pshared, unsigned int value)


– sem_init initializes the semaphore. The pshared indicates
whether the semaphore is local to the current process (0) or is
to be shared between several processes (not zero)., The value
is the initial count value of the semaphore.
• int sem_destroy (sem_t * sem)
– sem_destroy frees the resources hled by the semaphore..
• int sem_wait (sem_t * sem)
– sem_wait suspends the calling thread until the semaphore
pointed to by sem has non-zero count. It then decreases the
semaphore count.
• int sem_post (sem_t * sem)
– sem_post increases the count of the semaphore pointed to by
sem. This function never blocks.
Producer Consumer Example

Consumer
Producer
Consumer
Questions?

You might also like