hackerEarth questions Siemens
struct Semaphore
{
enum value(0, 1);
Queue q;
}
P(Semaphore s)
{
if (s.value == 1)
{
s.value = 0;
}
else
{
q.push(P);
sleep();
}
}
V(Semaphore s)
{
if (s.q is empty)
{
s.value = 1;
}
else
{
q.pop();
wakeup();
}
}