![]() ![]() Since Semaphores are integer variables, their value acts as a signal, which allows or does not allow a process to access the critical section of code or certain other resources. So to formally define Semaphore we can say that it is an integer variable that is used in a mutually exclusive manner by concurrent processes, to achieve synchronization. ![]() Hence we need proper synchronization between processes, and to prevent these, we use a signaling integer variable, called - Semaphore. This is called a race condition, and due to this condition, problems such as deadlock may occur. And if another process Y releases the same resource it had taken earlier, a similar situation might occur and the resultant would be 4, which instead should have been 5 itself. One would think that if we increment and decrement a number, its value should be unchanged and that is exactly what was happening in the two processes, however in this case the value of the "shared" variable is 4, and this is undesired.įor example: If we have 5 resources and one process uses it, decrementing its value by 1, just like in our example - process X, had done. Once that process is complete, in process 2 the value of the shared variable is changed to 4. We move back to process 1 and the value of the shared variable becomes 6. Then we decrement the value of y and process 2 goes into a sleep state. The integer y has the value of the shared variable initially which is unchanged, and is 5. Since the current processing is concurrent, the CPU does not wait and starts the processing of process 2. The value of x is then incremented, and it becomes 6, and post that the process goes into a sleep state. We start with the execution of process 1, in which we declare a variable x which has initially the value of the shared variable which is 5. The resource here is the variable "shared" with the value 5. What is our goal here? We want to achieve mutual exclusion, meaning that we want to prevent simultaneous access to a shared resource. We have 2 processes, that are concurrent and since we are talking about Process Synchronization, let's say they share a variable "shared" which has a value of 5. Now that you are familiar with these terms, we can move on to understanding the need for Semaphores with an example. This section is called the Critical Section. For any number of processes that are executing simultaneously, let's say all of them need to access a section of the code. Now, this Process Synchronization is required for concurrent processes. Process Synchronization can be defined as the coordination between two processes that has access to common materials such as a common section of code, resources or data, etc.įor example: There may be some resource that is shared by 3 different processes, and none of the processes at a certain time can change the resource, since that might ruin the results of the other processes sharing the same resource. If not, let's quickly get comfortable with these terms above.Ĭoncurrent Processes are those processes that are executed simultaneously or parallel and might or might not be dependent on other processes. If you have read about Process Synchronization, you are aware of the critical section problem that arises for concurrent processes. We also cover the implementation of Semaphore with help of the Producer-Consumer problem.In this article, we’ll discuss Semaphore, and types of Semaphore in detail with the help of examples.It is a concept in operating systems for the synchronization of concurrent processes. Semaphore is essentially a non-negative integer that is used to solve the critical section problem by acting as a signal. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |