edited by
21,388 views
14 votes
14 votes

Semaphores are used to solve the problem of

  1. Race Condition
  2. Process Synchronization
  3. Mutual Exclusion
  4. None of the above
  1. I and II
  2. II and III
  3. All of the above
  4. None of the above
edited by

11 Answers

Best answer
10 votes
10 votes

This is my view on the above question -

Definitions -

Race Condition - A section of code where if multiple threads/processes execute it the final outcome is undefined and depends upon the order in which the execute. Thus some sort of mutual exclusion is required to avoid this condition

Mutual exculusion - A mutual exclusion (mutex) is a program object that prevents simultaneous access to a shared resource.

Process Synchronization - Process Synchronization means sharing system resources by processes in a such a way that, Concurrent access to shared data is handled thereby minimizing the chance of inconsistent data

Thus Semaphores are used to solve the problem of Race condition, Mutual exclution, process synchronization.

Ref - https://www.winemantech.com/blog/avoiding-race-conditions-with-labview-programming/

There are few arguments that Semaphore does not gurantee the freedom from race condition. Yes its true as improper use of Semaphore will not gurantee freedom from race condition. However, this argument is invalid as the question never asked which of the following is defintely solved by Semaphore. It is asking Semaphore is used to solve?

Also if the above argument is used then in that case even improper use of Semaphore will not gurantee mutual exclusion.

For example ,

struct semaphore {
Queue L;
int value
} mutex;

up( mutex )
Critical section
up( mutex)

In the above code there is use of semaphore but fails to gurantee mutual exclusion.

The answer to the question could be option C) or B) depending upon what the question setter had in his mind.

If By All of above he meant 1, 2, 3 and not including None of the above the correct option is C) All of above

If really its a tricky question and All of above includes None of above then most appropriate option will be B)

selected by
3 votes
3 votes

Answer: C (All of the three)

1. Race Condition:

we enclose any increment or decrement operation with Semaphores, to prevent Race Condition. An example of this can we seen in Readers and Writers Solution.

Writer Reader
wait(wrt);
        . . .
        writing is performed
        . . .
signal(wrt);
wait(mutex);
        readcount := readcount + 1;
        if readcount = 1 then wait(wrt);
signal(mutex);
        . . .
        reading is performed
        . . .
wait(mutex);
        readcount := readcount - 1;
        if readcount = 0 then signal(wrt);
signal(mutex);

2. Process Synchronization:

See this question: https://gateoverflow.in/964/gate2003-80

In this question we synchronize process such that 00 always gets printed before 11.

3. Mutual Exclusion:

Obvious one, we can provide ME using semaphores. 

2 votes
2 votes

Semaphores are used to solve process synchronization, mutual exclusion and also race condition. 

https://en.wikipedia.org/wiki/Semaphore_(programming)

2 votes
2 votes
b)Process Synchronization. We can also use semaphores to solve various synchronization problems.(Galvin)
Answer:

Related questions

26 votes
26 votes
5 answers
1
Kathleen asked Sep 12, 2014
23,590 views
At a particular time of computation, the value of a counting semaphore is $7$. Then $20$ $P$ operations and $15$ $V$ operations were completed on this semaphore. The resu...
0 votes
0 votes
1 answer
3
Mrityudoot asked Jan 27
178 views
Can a counting semaphore acquire a negative value?S = 2;15 P operations done, should the semaphore be 0 or -13