Quick actions

cmd+k|ctrl+k

Navigation

Languages

Binary Semaphore

Snippet info

Language

Cpp

Visibility

public

Author

soham57

Created

2022-10-06T02:51:18.206286Z

Updated

2022-10-06T02:51:18.206286Z

struct semaphore {
	enum value(0, 1);

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

} P(semaphore s)
{
	if (s.value == 1) {
		s.value = 0;
	}
	else {
		// add the process to the waiting queue
		q.push(P) sleep();
	}
}
V(Semaphore s)
{
	if (s.q is empty) {
		s.value = 1;
	}
	else {

		// select a process from waiting queue
		Process p = q.front();
		// remove the process from wating as it has been
		// sent for CS
		q.pop();
		wakeup(p);
	}
}
INFO