Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Process Synchronization in Operating System (OS)
Process Synchronization in Operating System
There are two ways any process can execute –
- In Concurrent Execution – the CPU scheduler switches rapidly between processes. A process is stopped at any points and the processor is assigned to another instruction execution. Here, only one instruction is executed at a time.
- Parallel execution – 2 or more instructions of different process execute simultaneously on different processing cores.
Why is Process Synchronisation Important
When several threads (or processes) share data, running in parallel on different cores , then changes made by one process may override changes made by another process running parallel. Resulting in inconsistent data. So, this requires processes to be synchronized, handling system resources and processes to avoid such situation is known as Process Synchronization.
Classic Banking Example –
- Consider your bank account has 5000$.
- You try to withdraw 4000$ using net banking and simultaneously try to withdraw via ATM too.
- For Net Banking at time t = 0ms bank checks you have 5000$ as balance and you’re trying to withdraw 4000$ which is lesser than your available balance. So, it lets you proceed further and at time t = 1ms it connects you to server to transfer the amount
- Imagine, for ATM at time t = 0.5ms bank checks your available balance which currently is 5000$ and thus let’s you enter ATM password and withdraw amount.
- At time t = 1.5 ms ATM dispenses the cash of 4000$ and at time t = 2 net banking transfer is complete of 4000$.
Effect on the system
Now, due to concurrent access and processing time that computer takes in both ways you were able to withdraw 3000$ more than your balance. In total 8000$ were taken out and balance was just 5000$.
How to solve this Situation
To avoid such situations process synchronisation is used, so another concurrent process P2 is notified of existing concurrent process P1 and not allowed to go through as there is P1 process which is running and P2 execution is only allowed once P1 completes.
Process Synchronization also prevents race around condition. It’s the condition in which several processes access and manipulate the same data. In this condition, the outcome of the execution depends upon the particular order in which access takes place.
Types of Process Synchronization
On the basis of synchronization, processes are categorized as one of the following two types:
- Independent Process : Execution of one process does not affects the execution of other processes.
- Cooperative Process : Execution of one process affects the execution of other processes.
To understand more about process synchronization, a through understanding of process structure is required. So, let’s have a look at the elements of process .
The main blocks of process are –
- Entry Section – To enter the critical section code, a process must request permission. Entry Section code implements this request.
- Critical Section – This is the segment of code where process changes common variables, updates a table, writes to a file and so on. When 1 process is executing in its critical section, no other process is allowed to execute in its critical section.
- Exit Section – After the critical section is executed , this is followed by exit section code which marks the end of critical section code.
- Remainder Section – The remaining code of the process is known as remaining section.
Important – Visit the Critical Section page here, to read in detail about the Critical Section Problem
Solution for Process Synchronization
Processes have to abide to this rule under this, only one process at a time is allowed to operate inside critical section. All cooperating processes should follow this.
Many hacks on banking system try to inject code inside this to break this rule. So this is one of the most important part of concurrent process codes.
As the name suggests, the system must promote progress and maximum utilisation of the system. If there is no thread in the critical section then, and there are threads waiting for critical section utilisation. Then they must be allowed immediately to access critical section.
If a process P1 has asked for entry in its critical section. There is a limit on the number of other processes that can enter critical section. Since if a large number of processes are allowed to enter critical section. Then the process P1 may starve forever to gets its entry.
To further solve such situations many more solutions are available which will be discussed in detail in further posts –
- Critical Section
- Test and Set
- Peterson’s Solution
The following are not required to be known as of now, read them later, we will cover these in our future posts –
- Synchronization Hardware
- Mutex Locks
Also Geeks4Geeks confuses by introducing Peterson’s Solution, TestAndSet and semaphores in the process Synchronization post. Please ignore it.
- Process Synchronization
- Critical Section
- Inter-Process Communication
- UEFI(Unified Extensible Firmware Interface) and how is it different from BIOS
- Mutex vs. Semaphore
- Atomic Operations in OS
- Peterson’s Algorithm for Mutual Exclusion (Only important for Cisco and Arista Networs)
- Peterson’s Algorithm for Critical Section Problem (Only important for Cisco and Arista Networs)
- Readers-Writers Problem