Back to Blog
Mutex vs semaphor7/5/2023 It uses two functions to change the value of , that is initialized with the number of resources present in the system and is used for process synchronization. In this way, the process synchronization can be achieved with the help of a mutex object. So, in the critical section, no other processes are allowed to use the shared resource. After that, other processes can create the mutex object in the same manner and use it.īy locking the object, that particular resource is allocated to that particular process and no other process can take that resource. After locking, the process uses the resource and finally releases the mutex object. So, whenever the process wants to use that resource, then the process occupies a lock on the object. Whenever a process requests for a resource from the system, then the system will create a mutex object with a unique name or ID. Mutex uses the lock-based technique to handle the critical section problem. The mutex object allows all the processes to use the same resource but at a time, only one process is allowed to use the resource. Mutex or Mutual Exclusion Object is used to give access to a resource to only one process at a time. So, to apply process synchronization, two methods are used. The shared resources can be used by all the processes but the processes should make sure that at a particular time, only one process should be using that shared resource. So, there must be some synchronization between the processes when they are using shared resources. And here both the processes are dealing on the same resource i.e. But in the second case, the process P1 was stopped after executing one instruction and after that the process P2 starts executing. In the first approach, the process P1 executes first and then the process P2 starts executing. The processes are using the same resource here i.e. in 1st case it is 20 and in 2nd case, it is 15. In the above two cases, after the execution of the two processes P1 and P2, the final value of "a" is different i.e. It will then increment the value of "a" by 5 and write the final value of "a" in the memory i.e. So, the process P1 is having the value of "a" as 10(because P1 has already executed the instruction 1). After the execution of the process P2, the process P1 will be resumed and the context of the P1 will be read. It will then increment the value of "a" by 5 and make it to 15 and then save it to the memory. As the process P1 didn't change the value of "a", so, P2 will also read the value of "a" to be 10. Now, P2 will be in the running state and P1 will be in the waiting state and the context of the P1 process will be saved. Now, at this time, context switching happens between process P1 and P2(learn more about context switching from So, it reads the value of "a" from the memory and that value is 10(initial value of "a" is taken to be 10). In this case, let's assume that the process P1 starts executing. Here, in this case, the final value of "a" is 20. 15, increment with 5(15+5 = 20) and finally write it to the memory i.e. Now, the process P2 will read the value i.e. Lastly, this value will be updated in the memory. So, the process P1 will first read the value of "a" to be 10 and then increment the value by 5 and make it to 15. all the three instructions) and after that, the process P2 will be executed. In this case, process P1 will be executed fully (i.e. Let's take the following two cases and also assume that the value of "a" is 10 initially Now, we have two processes P1 and P2 that needs to be executed. In the above, you can see that a process after doing some operations will have to read the value of "a", then increment the value of "a" by 5 and at last write the value of "a" in the memory. They are reading the variable and then updating the value to the variable and finally writing the data in the memory. Now, think of a situation where we have two processes and these processes are using the same variable "a". In an Operating System, we have a number of processes and these processes require a number of resources. Process Synchronization and Critical Section We will also look at the solutions to the critical section problem i.e. So, in this blog, we will learn about process synchronization and critical section. No two processes should be allowed to use the printer at the same instant of time. For example, if the system is having a printer, then this printer is shared with all the processes but at a time, only one process can use the printer. But one thing that should be kept in mind is that the resources are shared but it should not be used simultaneously by all the processes. So, for this, we have shared resources in our system that can be shared between these processes. These processes require various resources for their execution. In an Operating System, there are a number of processes that are ready to be executed at a particular instant of time.
0 Comments
Read More
Leave a Reply. |