How Do Counting Semaphores Synchronize Processes?

1.7M views
•
February 28, 2018
by
Gate Smashers
YouTube video player
How Do Counting Semaphores Synchronize Processes?

TL;DR

A counting semaphore synchronizes concurrent cooperative processes by controlling entry to a critical section with atomic Wait and Signal operations. Wait decreases the semaphore and blocks a process when the result is negative, while Signal increases it and wakes a suspended process when the resulting value is less than or equal to zero.

Transcript

Hello everyone, welcome to Gate Smashers. Today we are going to discuss topic Semaphore In GATE and other competitive exams many times questions are asked on semaphore. What actually is semaphore? It is a method or it is a tool which is used to prevent race condition Means when we synchronize processes or run multiple processes at a time, and speci... Read More

Key Insights

  • A semaphore is an integer variable used by concurrent cooperative processes in a mutually exclusive manner to achieve synchronization and help prevent race conditions around shared code or resources.
  • Counting and binary semaphores are the two presented semaphore types. A counting semaphore can take integer values from negative infinity to positive infinity, while a binary semaphore has only zero and one as possible values.
  • A critical section contains code that is common among multiple processes. Every process seeking access must first execute entry-section code, perform its work inside the critical section if admitted, and then execute exit-section code.
  • The Wait operation is also called P or Down. It belongs to the entry section and first decreases the semaphore value by one before checking whether the new value is less than zero.
  • A negative semaphore value during Wait causes the requesting process to be blocked. Its process control block, which stores information such as process ID, open files, and priority, is placed in the suspended list.
  • A semaphore value of zero means no further process can enter without being blocked. The next Wait operation decreases the value to negative one, satisfies the negative-value condition, and sends that process to the suspended list.
  • A negative counting-semaphore value indicates the number of suspended processes in the presented model. For example, a value of negative four means four processes are in the blocked state, while negative two corresponds to two blocked processes.
  • The Signal operation is also called V, Up, Post, or Release. It belongs to the exit section, increases the semaphore by one, and wakes a selected suspended process when the resulting value is less than or equal to zero.

Install to Summarize YouTube Videos and Get Transcripts

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: What is a semaphore in an operating system?

A semaphore is an integer variable used in a mutually exclusive manner by concurrent cooperative processes to achieve synchronization. It is presented as one method for preventing race conditions when several processes run at the same time and share something in common. Preventing those race conditions matters because they can cause data loss, deadlock, and other problems.

Q: What is the difference between counting and binary semaphores?

A counting semaphore is an integer variable whose value can vary from negative infinity to positive infinity in the model presented. A binary semaphore has only two possible values, zero and one. The counting form can therefore represent available critical-section capacity and, through negative values, the number of processes currently placed in the suspended or blocked list.

Q: How does the Wait operation work in a counting semaphore?

The Wait operation, also called P or Down, is executed in the entry section before a process enters the critical section. It first changes the semaphore using S equals S minus one. If the resulting value is less than zero, the process control block is placed in the suspended list, and the process enters the blocked or sleeping state.

Q: How does the Signal operation work in a counting semaphore?

The Signal operation, also called V, Up, Post, or Release, is executed when a process exits the critical section. It changes the semaphore using S equals S plus one. If the resulting value is less than or equal to zero, a process is selected from the suspended list and awakened, connecting completion by one process with progress by a blocked process.

Q: What does a negative semaphore value mean?

A negative semaphore value represents the number of processes in the suspended list in the presented counting-semaphore model. For example, a value of negative four means four processes are blocked, while a value of negative two means two processes are blocked. Each additional unsuccessful Wait decreases the value again and places another process control block into that list.

Q: What does a semaphore value of zero mean?

A semaphore value of zero means the currently available entry capacity has been exhausted. No additional process can enter the critical section successfully under the presented Wait procedure. The next requesting process decreases the value from zero to negative one, finds that the result is less than zero, and has its process control block placed in the suspended list.

Q: How does a semaphore initialized to three handle five processes?

With the semaphore initialized to three, the first process decreases it to two and enters, the second decreases it to one and enters, and the third decreases it to zero and enters. The fourth decreases it to negative one and is blocked. The fifth decreases it to negative two and is also placed in the suspended list.

Q: Why are entry and exit sections required with semaphores?

The entry section controls whether a process may proceed into the critical section, which contains code common to multiple processes. It performs Wait, P, or Down before admission. The exit section runs after critical work is complete and performs Signal, V, Up, Post, or Release, increasing the semaphore and potentially waking a process from the suspended list.

Summary & Key Takeaways

  • A semaphore is an integer variable used in a mutually exclusive manner by concurrent cooperative processes to achieve synchronization. It helps prevent race conditions when multiple processes share something in common. The two presented semaphore categories are counting semaphores, which have a broad integer range, and binary semaphores, which use zero and one.

  • Processes must execute entry-section code before entering a critical section and exit-section code after completing their critical work. The entry operation may be called P, Down, or Wait. It decreases the semaphore value, tests whether the result is negative, and places the process control block in a suspended list when entry is unavailable.

  • The exit operation may be called V, Up, Signal, Post, or Release. It increases the semaphore value and checks whether the result is less than or equal to zero. If that condition holds, a process is selected from the suspended list and awakened, allowing blocked processes to progress as critical-section capacity becomes available.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from Gate Smashers 📚