Process Synchronization in Operating System (OS)

Process Synchronization :

On this page we will discuss about process synchronization in operating system (OS) . It is a process in operating system by which memory and resources are shared by different process.

 

Process Synchronization

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.
Process synchronization types in OS

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 .

Process synchronization solutions

The main blocks of process are –

  1. Entry Section – To enter the critical section code, a process must request permission. Entry Section code implements this request.
  2. 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.
  3. Exit Section – After the critical section is executed , this is followed by exit section code which marks the end of critical section code.
  4. 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

Mutual Exclusion

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.

Progress

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.

Bounded Waiting

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 –

  1. Semaphores
  2. Critical Section
  3. Test and Set
  4. Peterson’s Solution
  5. Mutex

The following are not required to be known as of now, read them later, we will cover these in our future posts –

  1. Synchronization Hardware
  2. Mutex Locks

Also Geeks4Geeks confuses by introducing Peterson’s Solution, TestAndSet and semaphores in the process Synchronization post. Please ignore it.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription