

{"id":100398,"date":"2021-09-09T09:00:48","date_gmt":"2021-09-09T03:30:48","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=100398"},"modified":"2021-09-15T13:25:56","modified_gmt":"2021-09-15T07:55:56","slug":"process-synchronization-in-operating-system","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/","title":{"rendered":"Process Synchronization in Operating System"},"content":{"rendered":"<p>Process Synchronization means coordinating the execution of processes such that no two processes access the same shared resources and data. It is required in a multi-process system where multiple processes run together, and more than one process tries to gain access to the same shared resource or data at the same time.<\/p>\n<p>Changes made in one process aren\u2019t reflected when another process accesses the same shared data. It is necessary that processes are synchronized with each other as it helps avoid the inconsistency of shared data.<\/p>\n<p>For example: A process P1 tries changing data in a particular memory location. At the same time another process P2 tries reading data from the same memory location. Thus, there is a high probability that the data being read by the second process is incorrect.<\/p>\n<h3>Sections of a Program in OS<\/h3>\n<p>Following are the four essential sections of a program:<\/p>\n<p><strong>1. Entry Section:<\/strong> This decides the entry of any process.<\/p>\n<p><strong>2. Critical Section:<\/strong> This allows a process to enter and modify the shared variable.<\/p>\n<p><strong>3. Exit Section:<\/strong> This allows the process waiting in the Entry Section, to enter into the Critical Sections and makes sure that the process is removed through this section once it&#8217;s done executing.<\/p>\n<p><strong>4. Remainder Section:<\/strong> Parts of the Code, not present in the above three sections are collectively called Remainder Section.<\/p>\n<h3>Types of process in Operating System<\/h3>\n<p>On the basis of synchronization, the following are the two types of processes:<\/p>\n<p><strong>1. Independent Processes:<\/strong> The execution of one process doesn\u2019t affect the execution of another.<\/p>\n<p><strong>2. Cooperative Processes:<\/strong> Execution of one process affects the execution of the other. Thus, it is necessary that these processes are synchronized in order to guarantee the order of execution.<\/p>\n<h3>Critical Section Problem in OS<\/h3>\n<p>A segment of code that a signal process can access at a particular point of time is known as the critical section. It contains the shared data resources that can be accessed by other processes.<\/p>\n<p>Wait() function (represented as P()) handles the entry of a process to the critical section. Whereas signal() function (represented as V()) handles the exit of a process from the critical section. A single process executes in a critical section at a time. Other processes execute after the current process is done executing.<\/p>\n<h3>Race Condition in OS<\/h3>\n<p>When more than one processes execute the same code or access the same memory\/shared variable, it is possible that the output or value of the shared variable is wrong. In this condition, all processes race ahead in order to prove that their output is correct. This situation is known as race condition.<\/p>\n<p>When multiple processes access and manipulate the same data concurrently the outcome depends on the order in which these processes accessed the shared data. When the output of multiple thread execution differs according to the order in which the threads execute, a race condition occurs.<\/p>\n<p>We can avoid it if we treat the critical section as an atomic instruction and maintain proper thread synchronization using locks or atomic variables.<\/p>\n<h3>Rules for Critical Section<\/h3>\n<p>There are three rules that need to be enforced in the critical section. They are:<\/p>\n<p><strong>1. Mutual Exclusion:<\/strong> A special type of binary semaphore used to control access to shared resources. It has a priority inheritance mechanism that helps avoid extended priority inversion problems. Only one process can execute at a time in the critical section.<\/p>\n<p><strong>2. Progress:<\/strong> We use it when the critical section is empty, and a process wants to enter it. The processes that are not present in their reminder section decide who should go in, within a finite time.<\/p>\n<p><strong>3. Bound Waiting:<\/strong> Only a specific number of processes are allowed into their critical section. Thus, a process needs to make a request when it wants to enter the critical section and when the critical section reaches its limit, the system allows the process\u2019 request and allows it into its critical section.<\/p>\n<h3>Solutions To The Critical Section<\/h3>\n<p>Following are some common solutions to the critical section problem:<\/p>\n<p><strong>1. Peterson Solution:<\/strong> If a process executes in a critical state, the other process can only execute the rest of the code and vise-versa.This method developed by Peterson is widely used and helps make sure that only one process runs at a specific time in the critical section<\/p>\n<p><strong>Example:<\/strong> Let there be N processes (P1, P2, &#8230; PN) such that at some point of time every process needs to enter the Critical Section. There is a FLAG[] array of size N that is false by default. Therefore, the flag of a process needs to be set true, whenever it wants to enter the critical section.<\/p>\n<p>A variable TURN tells the process number waiting to enter the Critical Section. When a process enters the critical section it changes the TURN to another number from the list of ready processes when it is exiting.<\/p>\n<p>Program:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PROCESS Pi\r\nFLAG[i] = true\r\nwhile( (turn != i) AND (CS is !free) ){ wait;\r\n}\r\nCRITICAL SECTION FLAG[i] = false\r\nturn = j; \/\/choose another process\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization-normal-image.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100530\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization-normal-image.jpg\" alt=\"Process Synchronization in OS\" width=\"378\" height=\"452\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization-normal-image.jpg 378w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization-normal-image-320x383.jpg 320w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/a><\/p>\n<p><strong>2. Synchronization Hardware:<\/strong> Hardware can also help resolve the problems of critical sections sometimes. Some OS offer lock functionality. This gives a process a lock when it enters the critical section and releases the lock after it leaves a critical section. Due to this other processes can\u2019t enter a critical section when a process is already inside.<\/p>\n<p><strong>3. Mutex Locks:<\/strong> Mutex Locks is a strict software method in which a LOCK over critical resources is given to a process in the entry section of code. The process can use this LOCK inside the critical section and can get rid of it in the exit section.<\/p>\n<p><strong>4. Semaphore Solution:<\/strong> Semaphore is a non-negative variable that is shared between threads. It is a signaling mechanism that uses a thread waiting on a semaphore, to signal another thread. It makes use of wait and signal for process synchronization.<\/p>\n<p><strong><strong>Example:<\/strong><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">WAIT ( S );\r\nwhile ( S &lt;= 0 );\r\nS = S - 1;\r\nSIGNAL ( S ):\r\nS = S + 1;\r\n<\/pre>\n<h3>Summary<\/h3>\n<p>Process Synchronization means coordinating the execution of processes such that no two processes access the same shared resources and data. There are four sections of a program: Entry Section, Critical Section, Exit Section, and Remainder Section. A segment of code that a signal process can access at a particular point of time is known as the critical section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Process Synchronization means coordinating the execution of processes such that no two processes access the same shared resources and data. It is required in a multi-process system where multiple processes run together, and more&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":100529,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24565],"tags":[25044,25042,25043,25045],"class_list":["post-100398","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-operating-system-tutorials","tag-critical-section-problem-in-os","tag-process-synchronization","tag-process-synchronization-in-os","tag-race-condition-in-os"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Process Synchronization in Operating System - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is process synchronization in Operating System, critical section problem with solution and race condition in OS.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Process Synchronization in Operating System - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is process synchronization in Operating System, critical section problem with solution and race condition in OS.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-09T03:30:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-15T07:55:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Process Synchronization in Operating System - DataFlair","description":"Learn what is process synchronization in Operating System, critical section problem with solution and race condition in OS.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/","og_locale":"en_US","og_type":"article","og_title":"Process Synchronization in Operating System - DataFlair","og_description":"Learn what is process synchronization in Operating System, critical section problem with solution and race condition in OS.","og_url":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-09-09T03:30:48+00:00","article_modified_time":"2021-09-15T07:55:56+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Process Synchronization in Operating System","datePublished":"2021-09-09T03:30:48+00:00","dateModified":"2021-09-15T07:55:56+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/"},"wordCount":1011,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization.jpg","keywords":["Critical Section Problem in OS","Process Synchronization","Process Synchronization in OS","Race Condition in OS"],"articleSection":["Operating System Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/","url":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/","name":"Process Synchronization in Operating System - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization.jpg","datePublished":"2021-09-09T03:30:48+00:00","dateModified":"2021-09-15T07:55:56+00:00","description":"Learn what is process synchronization in Operating System, critical section problem with solution and race condition in OS.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/OS-Process-Synchronization.jpg","width":1200,"height":628,"caption":"OS Process Synchronization"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/process-synchronization-in-operating-system\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Operating System Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/operating-system-tutorials\/"},{"@type":"ListItem","position":3,"name":"Process Synchronization in Operating System"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=100398"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100398\/revisions"}],"predecessor-version":[{"id":100665,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100398\/revisions\/100665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/100529"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=100398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=100398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=100398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}