Semaphores and Counting Semaphores | Operating System - M03 P05

Semaphores and Counting Semaphores | Operating System - M03 P05

ยท

5 min read

This is a multipart blog article series, and in this series I am going to explain you the concepts of operating system. This article series is divided into multiple modules and this is the third module which consists of 10 articles.

In this article we will discuss about semaphores in operating system, and see that how counting semaphores a type of semaphore works.

Semaphores

  • It is a method/ tool which are used to prevent race condition.
  • When we try to run multiple cooperative processes at single time then race condition can occur.
  • Race condition can cause loss of data, deadlock etc.
  • A semaphore is an integer variable which is used in mutual exclusive manner by various concurrent cooperative processes in order to achieve synchronization.

Untitled Diagram.png PCB : Process control block, store all the information related to a process like processed, list of open files, priority etc.

Entry section code

Down(Semaphore S) {
    Svalue = Svalue - 1
    if(Svalue < 0) {
        put process (PCB)m
        suspend list, sleep(),
    }
    else 
        return ;
}

Exit section code

Up(Semaphore S) {
    Svalue = Svalue + 1
    if(Svalue <= 0>) {
        select a process from suspend list and wake up();
    }
}
  • S: S is an integer and it is semaphore value which varies from (-infinity to + infinity), as this is counting semaphore.
  • If the Svalue is 0 then no other process can enter critical section, all process that want to enter critical section at that time will be blocked/ sleep.
  • If the Svalue is in negative then it (critical section) cannot take any other process.
  • So, when process exit critical section, they execute exit section code, which as you can see increase the value of Svalue.
  • In the code of exit section wake up() means that the process can again try to go in critical section, now it is not in sleep/ block state or critical section.
  • If the Svalue is 0 then there is no process in block/ sleep state, means no process has been suspended.
  • If the Svalue is 10, then 10 processes can enter critical section successfully.
  • If a process enters the critical section then it is stated as successful operation and if a process fails to enter the critical section then it is termed as unsuccessful operation.

Question: Let S = 10 we perform 6 P operation and 4 V operations. What is the current value of S?

Answer:

Given,

S = 10, P =6, V = 4

After performing 6 P operations

S = 10 - 6 = 4

After performing 4 V operations

S = 4 + 4 = 8

Therefore the current value of S = 4

Question: Let S = 17, we perform 5 P operation, 3 V operation, 1 P operation in sequence. What is the current value of S?

Answer:

Given,

S = 17, P1 = 5, V = 3, P2 = 1

After executing P1 we get

S = 17 โ€“ 5 = 12

After executing V we get

S = 12 + 3 = 15

After executing P2 we get

S = 15 โ€“ 1 = 14

Therefore the current value of S = 14

So this was all about semaphores and counting semaphores. Hope you liked it and learned something new from it.

If you have any doubt, question, quires related to this topic or just want to share something with me, then please feel free to contact me.

๐Ÿ“ฑ Contact Me

Twitter, LinkedIn, Telegram, Instagram,

๐Ÿ“ง Write a mail

rahulmishra102000@gmail.com

GitHub, HackerRank