• support@answerspoint.com

A semaphore is a blocking synchronisation primitive. Describe how they work with the aid of pseudo-code.


Semaphores work by blocking processes with P, calling thread_block(), waiting for a resource if it is not available, then being put into a queue of processes that want to access this resource. This is more efficient than other solutions because we avoid busy waiting – other processes can do other things while blocked ones are in the queue! When a resource is released, V is run, which calls thread_wakeup() to signal the next thread to use it.

Example :-

typedef struct _semaphore {

int count;

struct process *queue;

} semaphore;

semaphore sem;

void P(sem) {


        if (sem.count < 0)  {

        /* add process to sem.queue */




void V(sem) {


if (sem.count <= 0) {

/* remove a process from sem.queue */





Your Answer

    Facebook Share        
  • asked 7 years ago
  • viewed 3557 times
  • active 7 years ago

Best Rated Questions