COUNTING SEMAPHORE




It uses a count that helps task to be acquired or released numerous times.

Analysis of Counting Semaphore approach:


1. In this mechanism, the entry and exit in the critical section are performed on the basis of the value of counting semaphore. The value of counting semaphore at any point of time indicates the maximum number of processes that can enter in the critical section at the same time.

2. A process which wants to enter in the critical section first decrease the semaphore value by 1 and then check whether it gets negative or not. If it gets negative then the process is pushed in the list of blocked processes (i.e. q) otherwise it gets enter in the critical section.

3. When a process exits from the critical section, it increases the counting semaphore by 1 and then checks whether it is negative or zero. If it is negative then that means that at least one process is waiting in the blocked state hence, to ensure bounded waiting, the first process among the list of blocked processes will wake up and gets enter in the critical section.

4. The processes in the blocked list will get waked in the order in which they slept. If the value of counting semaphore is negative then it states the number of processes in the blocked state while if it is positive then it states the number of slots available in the critical section.

5. To implement mutual exclusion, the value of counting semaphore is initialized with 1.

6. It ensures that only one process can be present in the critical section at any given time.

Demo

Steps

Lights

Input

Enter the integer value that is the number of process required and the number of semaphore, according to that the initial state will be filled.

Than for process the process click on the repective process and than it will go to the appropriate state which is availabe according to algorithm.

Output

According to the user input on clicking the process the output will be shown by placing the process button on appropriate places according to the algorithm of counting semaphore.

Also the current state of the critical state is shown above the added state according to the number of process in the critical state.

Lights