Semaphore in UNIX – An Overview
Insight about Semaphore:
First let us begin our topic by giving an insight to what is actually a semaphore. A semaphore is nothing but a term used in UNIX for a variable which acts as a counter. So the next question that comes in mind is what for we need this variable. It’s so simple. The reason is explained below. For instance there may be times when two processes try to access the same file simultaneously. In this event we must control the access of the file when the other process is accessing. This is done by assigning value to semaphore.
The value of the semaphore is initialized by the first process when the file is in access by it. When the second process try to access the file it checks the value of the semaphore and if it finds the value as initialized it does not access the file. After the first process is completed it reinitializes the semaphore value and now the second process uses it. The above example is for two processes but a semaphore can be used even when numbers of processes try to access the same file. Thus semaphores are used to coordinate access to a resource by different processes.
Where a Semaphore gets stored
We have seen that semaphore can be used when numbers of processes try to access the same file. In this case we must make the semaphore available accessible to all processes so that they can read and check the value and also initialize and reinitialize the value of semaphore appropriately. For this reason only the semaphore is stored in kernel so that it can be accessed by all processes.
Details of Semaphore
The command
$ ipcs –s
will give the list of existing semaphores.
Function to create a simple semaphore
The function that can be used to create semaphores is semget( ). This function takes 3 arguments as input namely:
Name of the semaphore with which we are going to create the semaphore. This is technically called as key of the semaphore.
The number of sub semaphores to be created. The minimum required number is 1.
The semaphore mode is specified. There are 2 modes in semaphore namely read and rewrite.
The return integer value from this function indicates the semaphore id with which the semaphore is associated and if the return integer is -1 it indicates that an error has occurred in the creation of semaphore.
How Kernel maintains internally the semaphores:
For every semaphore created a structure is created inside by the kernel byname semid_ds and this structure is found in header file sys/ipc.h.
How to set user defined values for semaphore:
In the create semaphore function we saw that the semaphore created returns a semaphore id which is system generated. By we could realize in real usage scenarios it would prove helpful only when the user would be able to define the value of the semaphore. This is because if the user would be able to define the value of the semaphore then they could design easily stating the numbers they have given for semaphores .For instance number one may be used to mark that semaphore is free and say number two to mark the semaphore is in use by another process and so on. This can be done by using the following functions and values namely:
semctl( )
GETPID
SETVAL
along with semget( ) which we used before for creating semaphores.
Let us see in brief how to do this:
- The first step is to create semaphore as we discussed before by using semget( ) function which returns the semaphore id.
- Before seeing how to do this first let us see what the semctl( ) function takes as arguments. It takes 4 parameters as arguments namely:
- The semaphore id created by semget( ) function
- Sub semaphore id. For the first time it would be zero since it gets created that time.
- GETPID. This returns the Process id of the process.
- value to be placed for the above process id. This value only will be returned by the function semctl( ).
- After this by using SETVAL in semctl( ) function with all the arguments the same except instead of GETPID we place SETVAL and give the value to be placed in this which is returned by the semctl( ) function.
This is how we assign user defined values to semaphores which give ease of maintenance and use of semaphores by users.
Aspects of Semaphore:
- Semaphores are identified by semaphore id which is unique for each semaphore.
- The semaphores can be deleted by suing the function semdelete(semaphore)
- The semaphore value can be incremented or decremented by using functions wait (sem) and signal (sem) respectively. wait (sem) decrements the semaphore value and if in the process of decrementing the value of semaphore reaches negative value then the process is suspended and placed in queue for waiting. signal (sem) increments the value of the semaphore and it is opposite in action to wait (sem).In other words it causes the first process in queue to get executed.
The value of semaphore represents thus the number of threads which are nothing but processes. In other words we found that if the value is positive then we have threads to decrement and proceed for execution without suspending. If the value of semaphore is negative then it represents that the number of threads or process is blocked and kept in suspended state. If the value of semaphore is zero then it means that there are no threads or processes in waiting state.
So from the above the important fact in semaphores is when we create semaphores we found that semaphores can be assigned with any value. Also we found from above that after creation of semaphores we can modify that is either increment or decrement the value of semaphores. But at any situation we cannot read the current value of semaphore.