0% found this document useful (0 votes)
76 views4 pages

ANP 5. IPC Semaphore

A semaphore is a primitive that allows for synchronization between processes and threads. There are two main types - binary semaphores that can have values of 1 or 0, and counting semaphores that can have unrestricted integer values. Semaphores utilize operations like wait, post, trywait, which allow processes and threads to synchronize access to shared resources by waiting for or incrementing the semaphore value. They provide an alternative to mutexes for synchronization between processes and threads through shared memory or named semaphores in POSIX systems.

Uploaded by

Suman
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)
76 views4 pages

ANP 5. IPC Semaphore

A semaphore is a primitive that allows for synchronization between processes and threads. There are two main types - binary semaphores that can have values of 1 or 0, and counting semaphores that can have unrestricted integer values. Semaphores utilize operations like wait, post, trywait, which allow processes and threads to synchronize access to shared resources by waiting for or incrementing the semaphore value. They provide an alternative to mutexes for synchronization between processes and threads through shared memory or named semaphores in POSIX systems.

Uploaded by

Suman
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/ 4

7/21/2017

Semaphore
• A semaphore is a primitive for synchronization
between various
Inter-process Communication – Processes or
(IPC): POSIX Semaphore – Threads
• Location of semaphore
Harshad B. Prajapati – Posix named semaphore: Identified by Posix IPC names.
Associate Professor Used for synchronization of processes and threads.
Information Technology Department, – Memory based semaphores: in shared memory. Used for
Dharmsinh Desai University, Nadiad synchronization of processes and threads.

Semaphore Semaphore
• Semaphore types • Operations on semaphore
– Binary : can assume value 1 or 0
– Create a semaphore
• Used for mutual exclusive access
• The caller specifies initial value. (Generally, 1, but can
– Counting: can assume unrestricted integer values.
be 0)
• Used to keep track of the number of entities (e.g. resources,
resource consumers, resource producers) – Wait for semaphore
• Test the value of semaphore
– Wait if value is less than or equal to 0, Otherwise decrement
the value by one.
– Post to semaphore
• Increment the value of semaphore. If there are waiting
processes, one of those is awakened.

Producer-Consumer using
Semaphore
Semaphore
• Binary semaphore can be used to achieve • Use two semaphores
mutual exclusion. (What about using mutex) – put = 1, (Can assume 1 means free)
• Semaphore v/s mutex – get = 0, (Can assume 0 means locked)
– A mutex must always be unlocked by the thread • Producer Consumer
that locked the mutex. for(;;){
– A semaphore post need not be performed by the sem_wait(&put); sem_wait(&get);
thread that did the semaphore wait. put data into buffer process data in buffer
– A semaphore has state associated with it (Count). sem_post(&get); sem_post(&put);
In condition variable, if no thread is waiting for }
condition, a signal gets lost. • If the consumer starts first, it will block.

1
7/21/2017

Programming using Semaphore Programming using Semaphore


• semaphore functions prototypes and data • Create or open a named semaphore
types are defined in <semaphore.h> #include <semaphore.h>
• For semaphore object, use sem_t* sem_open(const char* name, int oflag,…
/*mode_t mode, unsigned int value*/);
– sem_t sem_name;
• oflag:
– 0 or O_CREAT, or O_CREAT|O_EXCL
– For O_CREAT, third and fourth arguments are required.
• mode: permission bits
• value: It is initial value of the semaphore. (Can’t
exceed SEM_VALUE_MAX, must be at least 32767)

Programming using Semaphore Programming using Semaphore


• Create or open a named semaphore • Close a named semaphore
#include <semaphore.h> int sem_close(sem_t* sem);
sem_t* sem_open(const char* name, int oflag,… /*mode_t
mode, unsigned int value*/);
• This happens whether the process terminates
voluntarily (exit) or involuntarily (killed by a signal)
• oflag:
– O_CREAT: the semaphore is initialized only if it does not already exist. • Closing a named semaphore does not remove the
– O_CREAT|O_EXCL: Get error if the semaphore already exists. semaphore from the system (Posix semaphores are
• The return value is a pointer to a sem_t datatype at least kernel persistent).
• (Note: the sem_open does not need pshared – They retain their value even if no process currently has the
semaphore open.
attribute, as in sem_init, as named semaphore is
– It is removed from the system by sem_unlink.
always sharable between different processes).

Programming using Semaphore Programming using Semaphore


• Remove a named semaphore from the system • To wait on a semaphore, use sem_wait:
int sem_unlink(sem_t* sem); int sem_wait(sem_t *sem);
• Example of use:
• Semaphores have reference count of how many
sem_wait(&sem_name);
times they are currently open (similar to reference
It returns value always 0 (cannot fail)
count of files)
• If the semaphore value is greater than 0, the value is
• Using this function, its name can be removed from decremented by 1, and the function returns immediately.
the system while reference count is greater than 0. • If the value is 0, when the function is called, the calling thread
But actual destruction of semaphore occurs when is put to sleep until another thread calls sem_post (increment
last sem_close returns. semaphore value, makes it 1 from 0), at this time (when the
value is 1) the function decrements the value of semaphore
(makes it 0) and returns.

2
7/21/2017

Programming using Semaphore Programming using Semaphore


• Trying to perform wait • To increment the value of a semaphore, use
int sem_trywait(sem_t * sem); int sem_post(sem_t *sem);
• This function does not put the calling thread to sleep if the • It increments the value by one and wakes up any of
current value of the semaphore is already 0.
threads that are waiting for the semaphore value to
• An error of EAGAIN is returned.
become positive.
• Example of use:
• sem_wait can return prematurely if it is interrupted by the
signal. (Return an error of EINTR) sem_post(&sem_name);
It returns value 0 if successful, -1 in case of an error

Programming using Semaphore Programming using Semaphore


• Get value of the semaphore • Memory based semaphores are initialized using sem_init.
int sem_getvalue(sem_t *sem, int * valp); int sem_init(sem_t *sem, int pshared, unsigned int value);
It gives the current value of the semaphore in the integer • sem: a semaphore object to be initialized. An application must
pointed to by valp. allocate memory for variable.
• Example of use: • pshared
sem_getvalue(&sem_name,&value); – indicates how semaphore should be shared.
– If pshared is 0, then semaphore is shared between threads of the
It returns value 0 if successful, -1 in case of an error
process
• If the semaphore is currently locked, then the value returned – Otherwise, the semaphore is shared between processes. (The
is 0 or a negative number. semaphore must be in some type of shared memory that is accessible
• Absolute value of the negative number indicates the number to all processes that are using the semaphore)
of threads that are waiting on the semaphore. • value: an initial value of the semaphore

Programming using Semaphore Programming using Semaphore


• sem_open v/s sem_init • To destroy a semaphore, use
– The return value from sem_open is a pointer to a sem_t variable that
the function has allocated and initialized. int sem_destroy(sem_t *sem);
– The first argument to sem_init is a pointer to a sem_t variable that the
caller must allocate and that the function then initializes. • destroys the semaphore; no threads should be
waiting on the semaphore if its destruction is
• Named semaphores are normally used when different, to succeed.
unrelated processes are using the semaphore.
• memory based semaphore • Example of use:
– If shared between the threads of a single process, then the semaphore sem_destroy(&sem_name);
has process persistence.
– If shared between different processes, then semaphore must be Returns 0 on success, -1 on error.
stored in shared memory and semaphore remains in existence as long
as the shared memory remains in existence.

3
7/21/2017

Semaphore limits
• SEM_NSEMS_MAX: the maximum number of
semaphores that a process can have open at
once (at least 256).
• SEM_VALUE_MAX: the maximum value of the
semaphore (at least 32767)

You might also like