0% found this document useful (0 votes)
23 views9 pages

Semaphore

The document provides an overview of semaphores in operating systems, detailing the types of semaphores (unnamed and named) and their functionalities. It explains key functions such as sem_init(), sem_destroy(), sem_wait(), and sem_post(), including their parameters and expected behaviors. The content is aimed at understanding semaphore operations and their usage in process synchronization.

Uploaded by

f20220457
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)
23 views9 pages

Semaphore

The document provides an overview of semaphores in operating systems, detailing the types of semaphores (unnamed and named) and their functionalities. It explains key functions such as sem_init(), sem_destroy(), sem_wait(), and sem_post(), including their parameters and expected behaviors. The content is aimed at understanding semaphore operations and their usage in process synchronization.

Uploaded by

f20220457
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/ 9

OPERATING SYSTEMS (CS F372)

Semaphore
Barsha Mitra
BITS Pilani CSIS Dept., BITS Pilani, Hyderabad Campus
Hyderabad Campus
Types of Semaphore
• Unnamed semaphores are allocated in process memory and
initialized
• Unnamed semaphores might be usable by more than one process,
depending on how the semaphore is allocated and initialized
• Unnamed semaphores are either private, inherited through fork(),
or are protected by access protections of the regular file in which
they are allocated and mapped.

BITS Pilani, Hyderabad Campus


Types of Semaphore
• Named semaphores are like process-shared semaphores
• Named semaphores are sharable by several processes
• Named semaphores have an owner user-id, group-id, and a
protection mode
• functions sem_open(), sem_getvalue(), sem_close(), and
sem_unlink() are available to open, retrieve, close, and remove
named semaphores
• By using sem_open, you can create a named semaphore that has
a name defined in the file system name space
https://man7.org/linux/man-pages/man7/sem_overview.7.html

BITS Pilani, Hyderabad Campus


Functions

❖ Functions related to semaphore are defined in the header file


<semaphore.h>

❖ Some functions used for unnamed semaphores are:


❖ sem_init()
❖ sem_wait()
❖ sem_post()
❖ sem_destroy()

BITS Pilani, Hyderabad Campus


sem_init()
❖ Used to initialize a new unnamed semaphore.
❖ int sem_init(sem_t *sem, int pshared, unsigned int value);
❖ 3 parameters:
❖ sem – address at which the semaphore will be initialized.
❖ pshared – indicates how the semaphore is shared, 0 implies the
semaphore is shared between threads of a process, and non-zero
implies that the semaphore is shared between processes.
❖ value – initial value of the semaphore.
❖ Initializing a semaphore that has already been initialized results in
undefined behaviour.
❖ sem_init() returns 0 on success; on error, -1 is returned.
BITS Pilani, Hyderabad Campus
sem_destroy()
❖ Used to destroy an unnamed semaphore
❖ int sem_destroy(sem_t *sem);
❖ 1 parameter:
❖ sem – address of semaphore which is to be destroyed.
❖ Only a semaphore initialized using sem_init() should be destroyed using
sem_destroy(). Using a semaphore that has been destroyed gives undefined
behaviour, until the semaphore has been reinitialized using sem_init().
❖ Destroying a semaphore that other threads or processes are currently
blocked on produces undefined behaviour.
❖ sem_destroy() returns 0 on success; on error, -1 is returned.

BITS Pilani, Hyderabad Campus


sem_wait()
❖ Used to decrement the value of a semaphore.
❖ int sem_wait(sem_t *sem);
❖ 1 parameter:
❖ sem – address of semaphore whose value is to be decremented.
❖ If the value of the semaphore is non-zero, then the value of the
semaphore is decremented and the function returns.
❖ If the value of the semaphore is 0, then the execution of the
process/thread is blocked until the value increases to a non-zero number.
❖ sem_wait() returns 0 on success; on error, the value of the semaphore is
left unchanged and -1 is returned.
BITS Pilani, Hyderabad Campus
sem_post()
❖ Used to increment the value of a semaphore.
❖ int sem_post(sem_t *sem);
❖ 1 parameter:
❖ sem – address of semaphore whose value is to be incremented.
❖ If the value of the semaphore is non-zero and not the maximum value, then
the value of the semaphore is incremented and the function returns.
❖ If the value of the semaphore is 0 and another process is blocked in a
sem_wait() call, then that process will be woken up and proceed to lock the
semaphore.
❖ sem_post() returns 0 on success; on error, the value of the semaphore is left
unchanged and -1 is returned.
BITS Pilani, Hyderabad Campus
Thank You

BITS Pilani, Hyderabad Campus

You might also like