The Wayback Machine - https://web.archive.org/web/20240923012904/https://www.geeksforgeeks.org/introduction-of-process-synchronization/
Open In App

Introduction of Process Synchronization

Last Updated : 18 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Process Synchronization is the coordination of execution of multiple processes in a multi-process system to ensure that they access shared resources in a controlled and predictable manner. It aims to resolve the problem of race conditions and other synchronization issues in a concurrent system.

The main objective of process synchronization is to ensure that multiple processes access shared resources without interfering with each other and to prevent the possibility of inconsistent data due to concurrent access. To achieve this, various synchronization techniques such as semaphores, monitors, and critical sections are used.

In a multi-process system, synchronization is necessary to ensure data consistency and integrity, and to avoid the risk of deadlocks and other synchronization problems. Process synchronization is an important aspect of modern operating systems, and it plays a crucial role in ensuring the correct and efficient functioning of multi-process systems.

What is Process?

A process is a program that is currently running or a program under execution is called a process. It includes the program’s code and all the activity it needs to perform its tasks, such as using the CPU, memory, and other resources. Think of a process as a task that the computer is working on, like opening a web browser or playing a video.

Types of Process

On the basis of synchronization, processes are categorized as one of the following two types:

  • Independent Process: The execution of one process does not affect the execution of other processes.
  • Cooperative Process: A process that can affect or be affected by other processes executing in the system.

Process synchronization problem arises in the case of Cooperative processes also because resources are shared in Cooperative processes.   

What is Race Condition?

When more than one process is executing the same code or accessing the same memory or any shared variable in that condition there is a possibility that the output or the value of the shared variable is wrong so for that all the processes doing the race to say that my output is correct this condition known as a race condition. Several processes access and process the manipulations over the same data concurrently, and then the outcome depends on the particular order in which the access takes place. A race condition is a situation that may occur inside a critical section. This happens when the result of multiple thread execution in the critical section differs according to the order in which the threads execute. Race conditions in critical sections can be avoided if the critical section is treated as an atomic instruction. Also, proper thread synchronization using locks or atomic variables can prevent race conditions.   

Example

Let’s say there are two processes P1 and P2 which share a common variable (shared=10), both processes are present in – queue and waiting for their turn to be executed. Suppose, Process P1 first come under execution, and the CPU store a common variable between them (shared=10) in the local variable (X=10) and increment it by 1(X=11), after then when the CPU read line sleep(1),it switches from current process P1 to process P2 present in ready-queue. The process P1 goes in a waiting state for 1 second.

Now CPU execute the Process P2 line by line and store common variable (Shared=10) in its local variable (Y=10) and decrement Y by 1(Y=9), after then when CPU read sleep(1), the current process P2 goes in waiting for state and CPU remains idle for some time as there is no process in ready-queue, after completion of 1 second of process P1 when it comes in ready-queue, CPU takes the process P1 under execution and execute the remaining line of code (store the local variable (X=11) in common variable (shared=11) ), CPU remain idle for sometime waiting for any process in ready-queue,after completion of 1 second of Process P2, when process P2 comes in ready-queue, CPU start executing the further remaining line of Process P2(store the local variable (Y=9) in common variable (shared=9) ).

Initially Shared = 10

Process 1

Process 2

int X = shared

int Y = shared

X++

Y–

sleep(1)

sleep(1)

shared = X

shared = Y

Note: We are assuming the final value of a common variable(shared) after execution of Process P1 and Process P2 is 10 (as Process P1 increment variable (shared=10) by 1 and Process P2 decrement variable (shared=11) by 1 and finally it becomes shared=10). But we are getting undesired value due to a lack of proper synchronization.

Actual meaning of Race-Condition

  • If the order of execution of the process(first P1 -> then P2) then we will get the value of common variable (shared) =9.
  • If the order of execution of the process(first P2 -> then P1) then we will get the final value of common variable (shared) =11.
  • Here the (value1 = 9) and (value2=10) are racing, If we execute these two processes in our computer system then sometime we will get 9 and sometime we will get 10 as the final value of a common variable(shared). This phenomenon is called race condition.

Critical Section Problem

A critical section is a code segment that can be accessed by only one process at a time. The critical section contains shared variables that need to be synchronized to maintain the consistency of data variables. So the critical section problem means designing a way for cooperative processes to access shared resources without creating data inconsistencies. 

critical section problem

In the entry section, the process requests for entry in the Critical Section.

Any solution to the critical section problem must satisfy three requirements:

  • Mutual Exclusion: If a process is executing in its critical section, then no other process is allowed to execute in the critical section.
  • Progress: If no process is executing in the critical section and other processes are waiting outside the critical section, then only those processes that are not executing in their remainder section can participate in deciding which will enter the critical section next, and the selection can not be postponed indefinitely.
  • Bounded Waiting: A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.

Peterson’s Solution

Peterson’s Solution is a classical software-based solution to the critical section problem. In Peterson’s solution, we have two shared variables:

  • boolean flag[i]: Initialized to FALSE, initially no one is interested in entering the critical section
  • int turn: The process whose turn is to enter the critical section.

peterson's Solution

 Peterson’s Solution preserves all three conditions

  • Mutual Exclusion is assured as only one process can access the critical section at any time.
  • Progress is also assured, as a process outside the critical section does not block other processes from entering the critical section.
  • Bounded Waiting is preserved as every process gets a fair chance.

Disadvantages of Peterson’s Solution

  • It involves busy waiting. (In the Peterson’s solution, the code statement- “while(flag[j] && turn == j);” is responsible for this. Busy waiting is not favored because it wastes CPU cycles that could be used to perform other tasks.)
  • It is limited to 2 processes.
  • Peterson’s solution cannot be used in modern CPU architectures.

Semaphores

A semaphore is a signaling mechanism and a thread that is waiting on a semaphore can be signaled by another thread. This is different than a mutex as the mutex can be signaled only by the thread that is called the wait function.

A semaphore uses two atomic operations, wait and signal for process synchronization.
A Semaphore is an integer variable, which can be accessed only through two operations wait() and signal().
There are two types of semaphores: Binary Semaphores and Counting Semaphores.

  • Binary Semaphores: They can only be either 0 or 1. They are also known as mutex locks, as the locks can provide mutual exclusion. All the processes can share the same mutex semaphore that is initialized to 1. Then, a process has to wait until the lock becomes 0. Then, the process can make the mutex semaphore 1 and start its critical section. When it completes its critical section, it can reset the value of the mutex semaphore to 0 and some other process can enter its critical section.
  • Counting Semaphores: They can have any value and are not restricted to a certain domain. They can be used to control access to a resource that has a limitation on the number of simultaneous accesses. The semaphore can be initialized to the number of instances of the resource. Whenever a process wants to use that resource, it checks if the number of remaining instances is more than zero, i.e., the process has an instance available. Then, the process can enter its critical section thereby decreasing the value of the counting semaphore by 1. After the process is over with the use of the instance of the resource, it can leave the critical section thereby adding 1 to the number of available instances of the resource.

Advantages of Process Synchronization

  • Ensures data consistency and integrity
  • Avoids race conditions
  • Prevents inconsistent data due to concurrent access
  • Supports efficient and effective use of shared resources

Disadvantages of Process Synchronization

  • Adds overhead to the system
  • This can lead to performance degradation
  • Increases the complexity of the system
  • Can cause deadlock if not implemented properly.

Conclusion

Concurrent computing requires process synchronization to coordinate numerous processes in a multi-process system to regulate and forecast resource access. It addresses race situations and data inconsistency, essential for data integrity. Semaphores and Peterson’s solution are used for synchronization. Synchronization is necessary for data consistency but adds complexity and performance overheads, making correct implementation and management vital for multi-process systems.

Frequently Asked Questions on Process Synchronization – FAQs

Why is process synchronization important?

Process synchronization is important It’s crucial to prevent issues like race conditions and deadlocks, which can lead to unpredictable behavior or system crashes in multitasking environments.

What is the main objective of process synchronization in a multi-process system?

A multi-process system’s process synchronization goal is to govern and predict shared resource access. It prevents race situations and other synchronization concerns to maintain data integrity and avoid deadlocks.

What are the key requirements that any solution to the critical section problem must satisfy?

The crucial section problem solution must meet three criteria:

  • Mutual Exclusion: Only one process can run in its critical section.
  • Progress: Only processes not in their remainder sections can select the next process to enter the critical section if no process is in it and others are waiting.
  • Bounded Waiting: The number of times a process can enter its crucial phase after making a request must be limited before it is granted.


Previous Article
Next Article

Similar Reads

FIFO Barbershop in Process synchronization
Overview :In process synchronization, there exists a Sleeping Barber problem, as discussed here. But in the above solution, there is no guarantee that the customers are served in the order they arrive in the barbershop, i.e. there is no guarantee that the customers enter a FIFO manner(First-in First-out). In the solution discussed above. Up to n cu
5 min read
Monitors in Process Synchronization
Monitors are a higher-level synchronization construct that simplifies process synchronization by providing a high-level abstraction for data access and synchronization. Monitors are implemented as programming language constructs, typically in object-oriented languages, and provide mutual exclusion, condition variables, and data encapsulation in a s
3 min read
Process Synchronization | Set 2
Prerequisite - Process Synchronization | Introduction , Critical Section , SemaphoresProcess Synchronization is a technique which is used to coordinate the process that use shared Data. There are two types of Processes in an Operating Systems:- Independent Process - The process that does not affect or is affected by the other process while its exec
5 min read
Peterson's Algorithm in Process Synchronization
Prerequisite - Synchronization, Critical Section  The producer-consumer problem (or bounded buffer problem) describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue. Producers produce an item and put it into the buffer. If the buffer is already full then the producer will have to wait for an em
15+ min read
Bakery Algorithm in Process Synchronization
Prerequisite - Critical Section, Process Synchronization, Inter Process Communication The Bakery algorithm is one of the simplest known solutions to the mutual exclusion problem for the general case of N process. Bakery Algorithm is a critical section solution for N processes. The algorithm preserves the first come first serve property. How does th
13 min read
Dekker's algorithm in Process Synchronization
Prerequisite - Process Synchronization, Inter Process Communication To obtain such a mutual exclusion, bounded waiting, and progress there have been several algorithms implemented, one of which is Dekker's Algorithm. To understand the algorithm let's understand the solution to the critical section problem first. A process is generally represented a
15+ min read
Semaphores in Process Synchronization
Semaphores are a tool used in computer science to help manage how different processes (or programs) share resources, like memory or data, without causing conflicts. A semaphore is a special kind of synchronization data that can be used only through specific synchronization primitives. Semaphores are used to implement critical sections, which are re
15+ min read
Difference Between Process, Parent Process, and Child Process
Running program is a process. From this process, another process can be created. There is a parent-child relationship between the two processes. This can be achieved using a library function called fork(). fork() function splits the running process into two processes, the existing one is known as parent and the new process is known as a child. Here
8 min read
Print numbers in sequence using thread synchronization
The problem is to synchronize n number of threads using pthread library. The idea is take thread count and print 1 in first thread, print 2 in second thread, print 3 in third thread, ..... print n in nth thread and again repeat from thread 1 infinitely. Prerequisite : Multithreading Examples : Input : Thread count Output : 1 2 3 ... thread count 1
2 min read
Priority Inheritance Protocol (PIP) in Synchronization
Prerequisite - Introduction of Process Synchronization Priority Inheritance Protocol (PIP) is a technique which is used for sharing critical resources among different tasks. This allows the sharing of critical resources among different tasks without the occurrence of unbounded priority inversions. Basic Concept of PIP : The basic concept of PIP is
3 min read
Centralized Clock Synchronization
Centralized Clock Synchronization is an internal clock synchronization approach where clocks of system are synchronized with one of clock of system. Clock synchronization is carried out to make all clocks in network agree on the same value. In centralized clock synchronization, one of clocks is appointed as master clock. Other clock of the system a
2 min read
Hardware Synchronization Algorithms : Unlock and Lock, Test and Set, Swap
Process Synchronization problems occur when two processes running concurrently share the same data or same variable. The value of that variable may not be updated correctly before its being used by a second process. Such a condition is known as Race Around Condition. There are a software as well as hardware solutions to this problem. In this articl
5 min read
Critical Section in Synchronization
In computer science, a critical section refers to a segment of code that is executed by multiple concurrent threads or processes, and which accesses shared resources. These resources may include shared memory, files, or other system resources that can only be accessed by one thread or process at a time to avoid data inconsistency or race conditions
9 min read
Lock Variable Synchronization Mechanism
Prerequisites - Process SynchronizationA lock variable provides the simplest synchronization mechanism for processes. Some noteworthy points regarding Lock Variables are- Its a software mechanism implemented in user mode, i.e. no support required from the Operating System.Its a busy waiting solution (keeps the CPU busy even when its technically wai
5 min read
Mutual Exclusion in Synchronization
During concurrent execution of processes, processes need to enter the critical section (or the section of the program shared across processes) at times for execution. It might happen that because of the execution of multiple processes at once, the values stored in the critical section become inconsistent. In other words, the values depend on the se
6 min read
Synchronization Examples
The Synchronization is an important concept in operating systems that ensures the smooth and coordinated execution of processes and threads. It is the task of coordinating the execution of processes in such a way that no two processes can access the same shared data and resource. It is a critical part of operating system design which ensures that p
6 min read
Synchronization in Distributed Systems
Synchronization in distributed systems is crucial for ensuring consistency, coordination, and cooperation among distributed components. It addresses the challenges of maintaining data consistency, managing concurrent processes, and achieving coherent system behavior across different nodes in a network. By implementing effective synchronization mech
11 min read
Classical Problems of Synchronization with Semaphore Solution
With the evolution of multi-processors, tasks suffer due to overheads and traffic. To manage the resources efficiently, synchronization is needed. Synchronization ensures perfect execution of processes and controlled access to shared resources. Synchronization in operating systems is often explained with the help of real-life examples. In this arti
4 min read
Process Scheduler : Job and Process Status
When the jobs moves through the system and makes progress, it changes it's states from HOLD to FINISH. When the job is being processed by the job manager and the process manager, it is always in one of these 5 states: HOLD: When a user submits a job and it accepts the job, the job is put on HOLD and placed in a queue. READY: A job is in READY state
2 min read
Difference between Process Image and Multi Thread Process image
1. Process Image : Process image is an executable file required during the execution of any process. It consists of several segments related to the execution of the process. Following are the contents of the process image - 1. Process Control Block 2. Stack 3. Data 4. Code 2. Multi Thread Process Image : Multi thread process image is an executable
2 min read
Process Table and Process Control Block (PCB)
While creating a process, the operating system performs several operations. To identify the processes, it assigns a process identification number (PID) to each process. As the operating system supports multi-programming, it needs to keep track of all the processes. For this task, the process control block (PCB) is used to track the process’s execut
7 min read
Process states and Transitions in a UNIX Process
Process is an instance of a program in execution. A set of processes combined together make a complete program. There are two categories of processes in Unix, namely User processes : They are operated in user mode. Kernel processes : They are operated in kernel mode. Understanding the distinction between user and kernel processes is essential for c
4 min read
Introduction of Process Management
A process is a program in execution. For example, when we write a program in C or C++ and compile it, the compiler creates binary code. The original code and binary code are both programs. When we actually run the binary code, it becomes a process. A process is an 'active' entity instead of a program, which is considered a 'passive' entity. A singl
10 min read
Operating Systems | Process Management | Question 6
Consider the following code fragment: if (fork() == 0) { a = a + 5; printf("%d,%d\n", a, &a); } else { a = a –5; printf("%d, %d\n", a, &a); } Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE? (A) u = x + 10 and v = y (B) u = x +
2 min read
Operating Systems | Process Management | Question 6
Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the P operation on semaphores c, d, and a before entering the respecti
2 min read
Operating Systems | Process Management | Question 6
A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process befo
2 min read
Process-Based and Thread-Based Multitasking
Multitasking operating system is an operating system that gives you the perception of two or more tasks/jobs/processes running simultaneously. It does this by dividing system resources amongst these tasks/jobs/processes and switching between the tasks/jobs/processes while they are executing over and over again. The CPU processes only one task at a
2 min read
Communication between two process using signals in C
Prerequisite : C signal handling In this post, the communication between child and parent processes is done using kill() and signal(), fork() system call. fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (if pid == 0) or the parent (pid = child process id).The parent can then send messages t
3 min read
Union process in DFA
Prerequisite - Designing finite automata To perform the union operation on two deterministic finite automata (DFAs), the following steps can be taken: Create a new DFA with a new set of states, consisting of all the states from both original DFAs.Define the initial state of the new DFA to be the tuple (q1, q2), where q1 and q2 are the initial state
5 min read
Concatenation process in DFA
Prerequisite - Designing finite automata Designing a DFA for the set of string over {a, b} such that string of the language start with "a" and end with "b". There two desired language will be formed: L1 = {a, aab, aabab, .......} L2 = {b, bbab, bbabab, .......} In L1, starting element is "a" and in L2, ending element is b. State Transition Diagram
2 min read