Inter Process Communication: Semaphores

A semaphore allows you to control access to a resource. A semaphore is an equivalent of a key (or a set of keys) when a process tries to access a shared resource.

A semaphore is first associated with a resource that needs to be protected. Whenever a process wants to access this resource, it seeks a key from the semaphore. Depending on the availability of a key, a key is either allotted to the current process or not. If a key is alloted to a process, when this process is done with using the resource, it will give the key back to the semaphore and that key may be allotted to other processes who are waiting for a key.

Syntactically, semaphore is a built-in class that allows only certain pre-defined operations on the keys.

A semaphore declaration is shown below:




Built-in methods in Semaphore
There are 4 predefined methods inside a semaphore type class.


  1. new( ): Create a semaphore with specified number of keys.
  2. get( ): Obtain one or more keys from a sempahore and block until keys are available.
  3. try_get( ): Obtain one or more kets from a semaphore without blocking.
  4. put( ): Return one or more keys to a semaphore
new( )
Just as any other class, a semaphore needs a constructor.

The prototype declaration for new( ) is shown below:



  • The constructor new( ) has an integer argument key_count that can be used for creating a desired number of keys. 
  • The default value of key_count is 0. 
  • Upon success, the new( ) function returns the semaphore handle, otherwise, it returns null.

get( )
The get( ) task is used for obtaining one or multiple keys for a semaphore. 

The prototype declaration for get( ) is shown below:



  • The number of keys to procure is passed as the argument to get( ). 
  • The default value of this argument is 1.
  • If a process asks for certain number of kets and they are available, the call to get( ) returns and the execution continues.
  • If the required number of keys are not available, the call to get( ) blocks subsequent statements and waits for additional keys to be available. That is why get( ) is a task, not a function, and hence, can consume time.
  • All calls to get( ) are queued in a FIFO and keys are delivered in a first-come-first-serve basis.

try_get( )
If you do not want to block while trying to get keys for a semaphore, try_get( ) is your solution. Unlike get( ), try_get( ) is a function that checks for key availability and procures them if they are available (and returns 1). But, if they are not, try_get( ) does not block and returns 0.

The prototype for try_get( ) is shown below:



put( )
Now, suppose that a process is done with using a resource. According to the good code of conduct in the semaphore land, that process must return the keys (so that another process can use them). This is done by the put( ) task, where the number of returned kets are passes as an argument.

The prototype for put( ) is shown below:


No comments:

Post a Comment