Quick actions

cmd+k|ctrl+k

Navigation

Languages

Counting Semaphore

Snippet info

Language

Cpp

Visibility

public

Author

soham57

Created

2022-10-06T02:50:37.992341Z

Updated

2022-10-06T02:50:37.992341Z

#include <iostream>
using namespace std;

struct Semaphore {

	int value;

	// q contains all Process Control Blocks(PCBs)
	// corresponding to processes got blocked
	// while performing down operation.
	Queue<process> q;

} P(Semaphore s)
{
	s.value = s.value - 1;
	if (s.value < 0) {

		// add process to queue
		// here p is a process which is currently executing
		q.push(p);
		block();
	}
	else
		return;
}

V(Semaphore s)
{
	s.value = s.value + 1;
	if (s.value <= 0) {

		// remove process p from queue
		Process p = q.pop();
		wakeup(p);
	}
	else
		return;
}
INFO