0% found this document useful (0 votes)
43 views2 pages

How To Use Semaphore

Semaphores are used to control access to shared resources between processes. This document discusses how to use POSIX semaphores in C code. It explains how to initialize a semaphore with sem_init, lock it with sem_wait, release it with sem_post, and destroy it with sem_destroy. Header file semaphore.h must be included to use these POSIX semaphore functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

How To Use Semaphore

Semaphores are used to control access to shared resources between processes. This document discusses how to use POSIX semaphores in C code. It explains how to initialize a semaphore with sem_init, lock it with sem_wait, release it with sem_post, and destroy it with sem_destroy. Header file semaphore.h must be included to use these POSIX semaphore functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Posix Semaphore Implementation

Semaphore: In computer science, a semaphore is a variable or abstract data type


used to control access to a common resource by multiple processes in a concurrent
system such as a multitasking operating system. A semaphore is simply a variable.

In this article, we will explore how we can use semaphore in C language.

We have the POSIX semaphore library in Linux systems. We will use this in our
case
 Include semaphore.h header file

To lock a semaphore, we can use the sem_wait function:

To release or signal a semaphore, we use the sem_post function:

A semaphore is initialised by using sem_init (for processes or threads)


or sem_open (for Interprocess communication):

sem: Specifies the semaphore to be initialized.


pshared: This argument specifies whether or not the newly initialized semaphore
is shared between processes or between threads. A non-zero value means the
semaphore is shared between processes and a value of zero means it is shared
between threads.
value: Specifies the value to assign to the newly initialized semaphore.
To destroy a semaphore, we can use sem_destroy function:

You might also like