Semaphores are variables that allow processes to synchronize access to shared resources. There are two main types: binary semaphores can have values of 0 or 1, while counting semaphores can have integer values greater than or equal to 0. Semaphores use P and V operations - P decrements the semaphore value and allows a process to enter its critical section if the value is greater than 0, while V increments the value and allows another process to enter its critical section. This allows processes to mutually exclude each other from critical sections to avoid race conditions when accessing shared resources.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
247 views
Semaphores
Semaphores are variables that allow processes to synchronize access to shared resources. There are two main types: binary semaphores can have values of 0 or 1, while counting semaphores can have integer values greater than or equal to 0. Semaphores use P and V operations - P decrements the semaphore value and allows a process to enter its critical section if the value is greater than 0, while V increments the value and allows another process to enter its critical section. This allows processes to mutually exclude each other from critical sections to avoid race conditions when accessing shared resources.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10
Semaphores
• Semaphore is a simply a variable. This variable is used to solve critical
section problem and to achieve process synchronization in the multi processing environment. The two most common kinds of semaphores are counting semaphores and binary semaphores. • Counting semaphore can take non-negative integer values and Binary semaphore can take the value 0 & 1. only. First look at two operations which can be used to access and change the value of semaphore variable. Can only enter in critical section if value of S=1 Some point regarding P and V operation 1.P operation is also called wait, sleep or down operation and V operation is also called signal, wake-up or up operation. 2.Both operations are atomic and semaphore(s) is always initialized to one. 3.A critical section is surrounded by both operations to implement process synchronization. See below image .critical section of Process P is in between P and V operation. Let there be two processes P1 and P2 A semaphore s is initialized as 1. Now if suppose P1 enters in its critical section then the value of semaphores becomes 0. Now if P2 wants to enter its critical section then it will wait until s > 0, this can only happen when P1 finishes its critical section and calls V operation on semaphore s. This way mutual exclusion is achieved. Look at the below image for details. This description is for binary semaphore which can take only two values 0 and 1. There is one other type of semaphore called counting semaphore which can take values greater than one. • Now suppose there is a resource whose number of instance is 4. • Now we initialize S = 4 and rest is same as for binary semaphore. • Whenever process wants that resource it calls P or wait function and when it is done it calls V or signal function. • If value of S becomes zero than a process has to wait until S becomes positive. • For example, Suppose there are 4 process P1, P2, P3, P4 and they all call wait operation on S(initialized with 4). If another process P5 wants the resource then it should wait until one of the four process calls signal function and value of semaphore becomes positive. Problem in this implementation of semaphore Whenever any process waits then it continuously checks for semaphore value (look at this line while (s==0); in P operation) and waste CPU cycle. To avoid this another implementation is provided below. In this implementation whenever process waits it is added to a waiting queue of processes associated with that semaphore. This is done through system call block() on that process. When a process is completed it calls signal function and one process in the queue is resumed. It uses wakeup() system call.
Download Full (Ebook) Artificial Intelligence and Heuristics for Smart Energy Efficiency in Smart Cities: Case Study: Tipasa, Algeria by Mustapha Hatti ISBN 9783030920371, 3030920372 PDF All Chapters