Coordinating the execution of processes so that no two processes access the same shared resources and data is known as process synchronization. It is necessary for a multi-process system where several processes coexist and concurrently attempt to access the same shared resource or piece of data.
When another process accesses the same shared data, changes made in one process are not replicated. Process synchronisation is required in order to prevent shared data from becoming inconsistent. There are various processes in different states throughout the Operating System. We must divide our available resources among many processes because we only have a finite number of them. However, you should take care to prevent more than one process from using the same resource at once as this could result in inconsistent data.
In order to ensure process synchronization, the operating system needs to support it. Cooperative processes are those that share resources among themselves, and independent processes are those whose execution has no bearing on other processes' operations.
We will study what is process synchronization in OS, what is synchronization in OS, and what is the need for synchronization and we will explain in detail process synchronization in this blog. And if you want in-depth knowledge about operating systems to pursue a career, you can go for the Web Development Course to understand the basics of operating systems and learn how to use advanced tools to design and create dynamic websites.
What is Process Synchronization in OS?
An operating system is a piece of software that controls all of the installed programmes on a computer or other device, essentially making it run more efficiently. Because of this, the operating system frequently has to handle multiple tasks at once. Unless these processes running simultaneously share a resource, this usually doesn't pose a synchronization problem in OS.
Take a bank as an example, which maintains a single database for each customer's account balance. Let's say you had x rupees in your account at the beginning. Now you withdraw some money from your bank account, and someone tries to look at the balance of your account at the same time.
The total balance remaining in your account after the transaction will be less than x because you are taking money out of it. However, since the transaction takes some time, the person interprets x as your account balance, resulting in inconsistent data. We could guarantee consistent data if we could somehow make sure that only one process was running at once.
Source – Scalar.com
In the above image, if Process1 and Process2 occur simultaneously, User 2 will receive the incorrect account balance as Y as a result of Process1 being executed when the balance is X. When multiple processes share a single resource in a system, inconsistent data can result, which is why process synchronization is required in the operating system.
Process Synchronization Example in Operating System
Following is the synchronization example in OS:
1. Bounded Buffer Problem
A producer attempts to insert data into a buffer slot that is empty. Data is attempted to be removed from a filled slot in the buffer by a consumer. If those two processes are run simultaneously, the output won't be what was anticipated. There must be a way to enable the independent operation of the producer and consumer.
Solution:
The use of semaphores is one solution for this issue.
- m, a binary semaphore that is used to acquire and release the lock, is the semaphore that will be used in this situation.
- Given that all slots are initially empty, empty is a counting semaphore whose initial value is the number of slots in the buffer.
- A counting semaphore with a starting value of 0 is called "full."
- The number of empty slots in the buffer is represented by the current value of empty, and the number of occupied slots in the buffer is represented by the current value of full.
The structure of the producer process
while (true) {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
}
The structure of the consumer process
while (true) {
wait(full);
wait(mutex);
...
/* remove an item from buffer to
next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
}
2. Readers-Writers problem
The first reader-writer conflict gives readers precedence. If a reader requests access to the data in this issue and a writer is not already using it, access is given to the reader. A solution to this issue might starve the writers because there will always be more readers who want to access the information. As long as there is another reader accessing the data, a steady stream of readers will jump ahead of waiting writers (because the writer is forced to wait until the data is idle, which may never happen if there are enough readers).
The second reader-writers conflict gives writers priority. When a writer wants access to the data in this problem, they jump to the front of the line, blocking all other readers from accessing the data until the writer has it. The readers in this solution might go hungry due to the constant flow of writers.
The structure of a writer process
while (true) {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
}
The structure of a reader process
while (true){
wait(mutex);
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
}
How Does Process Synchronization work?
Let's examine the precise reasons why Process synchronization in OS is necessary. For example, in this case, there is a good chance that the data read by process1 will be incorrect if process2 attempts to change the data present at the same memory location while process1 is attempting to read it.
Source – Scalar.com
Essential Sections of a Program
Let's examine various components/sections of a program:
- Entry Section: This section determines when a process begins.
- Critical Section: The critical section permits only one process to modify the shared data and ensures that this is the case.
- Exit Section: After one process has finished running, the Exit section handles the entry of other processes into the shared data.
- Remainder Section: The remaining portion of the code that is not divided into the categories listed above is found in the Remainder section.
If the idea of creating apps and websites with user-friendly interfaces intrigues you, you can go for a Full Stack Developer Course to learn how to build, implement, secure, and manage programs and build proficiency across the business logic, user interface, and database stacks.
Looking to enhance your programming skills? Get certified in Python programming online and unlock endless possibilities. Join now!
Types of Process Synchronization in Operating System
The following are the types of synchronization:
1. Independent Processes
When a process is executed without affecting or having an impact on another process, that process is referred to as an independent process. For example the process without any shared databases, files, variables, etc.
2. Cooperative Processes
In a computer system, different processes can operate as independent or collaborative processes within the operating system. When a process won't be impacted by other processes running on the system, it is said to be independent. Data sharing between processes is not done by independent processes. On the other hand, any other process running on the system may impact a collaborating process. Data is shared between processes that are cooperating.
Why Synchronization is Important in Operating System
- Mutual exclusion: No other process should be permitted to run in the critical section while the current process is there.
- Progress: Any thread must be allowed to enter the critical section if no processes are still running inside it and other processes are waiting outside it to execute. Only those processes that aren't running in the other section will decide which process will enter the critical section.
- No starvation: A process that is starved waits interminably to access the critical section but is never given the opportunity. Bounded Waiting is another name for no starvation.
- A process shouldn't take an eternity to enter the crucial area.
- There should be a cap or bound that specifies how many other processes are permitted to access the critical section before a process can request access to its critical section.
- This process should be permitted access to the critical section once this bound is reached.
Conclusion
Coordinating the execution of processes so that no two processes access the same shared resources and data is known as process synchronization. A programme is divided into four sections: the entry section, the critical section, the exit section, and the remaining section. The critical section is a section of code that a signal processing unit can access at a specific time.
A critical section is a section of code that a signal process can access at a particular time. The following three regulations must be followed by critical sections: 1) mutual exclusion 2) Process solution 3 ) Bound waiting
A unique variety of binary semaphore called mutual exclusion is used to regulate access to shared resources. When no one is present in the crucial area and someone wants to enter, a process solution is used. There is a cap on the number of other processes that can enter a process' critical section after it requests access in the bound waiting solution.
We hope you found this blog helpful in understanding process synchronization in OS, what synchronization is in operating system and other related aspects. If you are interested in learning more about operating systems and how they work, you can go for KnowledgeHut’s Web Development course. There, you will find everything you need to know about developing apps for different platforms and acquiring top tech skills like React, Node.js, Full-Stack, JavaScript, etc. There, you will find everything you need to know about developing apps for different platforms and acquiring top tech skills like React, Node.js, Full-Stack, JavaScript, etc.