{"id":3339,"date":"2025-06-07T04:14:56","date_gmt":"2025-06-07T04:14:56","guid":{"rendered":"https:\/\/diznr.com\/?p=3339"},"modified":"2025-06-07T04:14:56","modified_gmt":"2025-06-07T04:14:56","slug":"subject-operating-system-topic-process-synchronization-example-based-on-2-algo","status":"publish","type":"post","link":"https:\/\/www.reilsolar.com\/pdf\/subject-operating-system-topic-process-synchronization-example-based-on-2-algo\/","title":{"rendered":"Subject &#8211; Operating System Topic &#8211; Process Synchronization (example based on Algo-2)."},"content":{"rendered":"<p>Subject &#8211; Operating System Topic &#8211; Process Synchronization (example based on Algo-2).<\/p>\n<p>[fvplayer id=&#8221;360&#8243;]<\/p>\n<p class=\"\" data-start=\"0\" data-end=\"39\">Here is a GATE-focused explanation for:<\/p>\n<hr class=\"\" data-start=\"41\" data-end=\"44\" \/>\n<h2 class=\"\" data-start=\"46\" data-end=\"83\">\ud83c\udf93 <strong data-start=\"52\" data-end=\"64\">Subject:<\/strong> Operating System<\/h2>\n<h3 class=\"\" data-start=\"84\" data-end=\"127\">\ud83d\udccc <strong data-start=\"91\" data-end=\"101\">Topic:<\/strong> Process Synchronization<\/h3>\n<h3 class=\"\" data-start=\"128\" data-end=\"183\">\ud83e\uddea <strong data-start=\"135\" data-end=\"183\">Example Based on Algo-2 \u2013 Dekker\u2019s Algorithm<\/strong><\/h3>\n<hr class=\"\" data-start=\"185\" data-end=\"188\" \/>\n<h3 class=\"\" data-start=\"190\" data-end=\"233\">\ud83d\udd0d <strong data-start=\"197\" data-end=\"233\">What is Process Synchronization?<\/strong><\/h3>\n<p class=\"\" data-start=\"235\" data-end=\"427\">In <strong data-start=\"238\" data-end=\"265\">multiprocessing systems<\/strong>, multiple processes may try to access <strong data-start=\"304\" data-end=\"324\">shared resources<\/strong> (like memory or files) simultaneously. This leads to <strong data-start=\"378\" data-end=\"397\">race conditions<\/strong> if not properly synchronized.<\/p>\n<p class=\"\" data-start=\"429\" data-end=\"544\">To prevent this, <strong data-start=\"446\" data-end=\"484\">process synchronization algorithms<\/strong> are used to manage <strong data-start=\"504\" data-end=\"543\">access to the critical section (CS)<\/strong>.<\/p>\n<hr class=\"\" data-start=\"546\" data-end=\"549\" \/>\n<h2 class=\"\" data-start=\"551\" data-end=\"617\">\ud83e\udde0 <strong data-start=\"557\" data-end=\"587\">Algo-2: Dekker\u2019s Algorithm<\/strong> (Two-Process Synchronization)<\/h2>\n<h3 class=\"\" data-start=\"619\" data-end=\"728\">\ud83d\udccc <strong data-start=\"626\" data-end=\"638\">Purpose:<\/strong> Software-based solution to achieve <strong data-start=\"674\" data-end=\"694\">mutual exclusion<\/strong> for <strong data-start=\"699\" data-end=\"726\">2 processes (P0 and P1)<\/strong><\/h3>\n<p class=\"\" data-start=\"729\" data-end=\"811\">\ud83d\udccc <strong data-start=\"732\" data-end=\"760\">Historical Significance:<\/strong> One of the <strong data-start=\"772\" data-end=\"795\">earliest algorithms<\/strong> that satisfies:<\/p>\n<ul data-start=\"812\" data-end=\"865\">\n<li class=\"\" data-start=\"812\" data-end=\"832\">\n<p class=\"\" data-start=\"814\" data-end=\"832\">Mutual Exclusion<\/p>\n<\/li>\n<li class=\"\" data-start=\"833\" data-end=\"852\">\n<p class=\"\" data-start=\"835\" data-end=\"852\">Bounded Waiting<\/p>\n<\/li>\n<li class=\"\" data-start=\"853\" data-end=\"865\">\n<p class=\"\" data-start=\"855\" data-end=\"865\">Progress<\/p>\n<\/li>\n<\/ul>\n<hr class=\"\" data-start=\"867\" data-end=\"870\" \/>\n<h3 class=\"\" data-start=\"872\" data-end=\"905\">\u2699\ufe0f <strong data-start=\"879\" data-end=\"905\">Shared Variables Used:<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]\">c<\/div>\n<div class=\"sticky top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center rounded-sm px-2 font-sans text-xs\"><button class=\"flex gap-1 items-center select-none px-4 py-1\" aria-label=\"Copy\">Copy<\/button><span class=\"\" data-state=\"closed\"><button class=\"flex items-center gap-1 px-4 py-1 select-none\">Edit<\/button><\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-c\">boolean flag[<span class=\"hljs-number\">2<\/span>] = {<span class=\"hljs-literal\">false<\/span>, <span class=\"hljs-literal\">false<\/span>};  <span class=\"hljs-comment\">\/\/ flags to show intention to enter CS<\/span><br \/>\n<span class=\"hljs-type\">int<\/span> turn = <span class=\"hljs-number\">0<\/span>;                      <span class=\"hljs-comment\">\/\/ indicates whose turn it is<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<hr class=\"\" data-start=\"1056\" data-end=\"1059\" \/>\n<h3 class=\"\" data-start=\"1061\" data-end=\"1106\">\ud83d\udc68\u200d\ud83d\udcbb <strong data-start=\"1071\" data-end=\"1106\">Process Pi&#8217;s Code (i = 0 or 1):<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]\">c<\/div>\n<div class=\"sticky top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center rounded-sm px-2 font-sans text-xs\"><button class=\"flex gap-1 items-center select-none px-4 py-1\" aria-label=\"Copy\">Copy<\/button><span class=\"\" data-state=\"closed\"><button class=\"flex items-center gap-1 px-4 py-1 select-none\">Edit<\/button><\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-c\"><span class=\"hljs-comment\">\/\/ Entry Section<\/span><br \/>\nflag[i] = <span class=\"hljs-literal\">true<\/span>;<br \/>\n<span class=\"hljs-keyword\">while<\/span> (flag[<span class=\"hljs-number\">1<\/span> - i]) {<br \/>\n    <span class=\"hljs-keyword\">if<\/span> (turn != i) {<br \/>\n        flag[i] = <span class=\"hljs-literal\">false<\/span>;<br \/>\n        <span class=\"hljs-keyword\">while<\/span> (turn != i);   <span class=\"hljs-comment\">\/\/ busy wait<\/span><br \/>\n        flag[i] = <span class=\"hljs-literal\">true<\/span>;<br \/>\n    }<br \/>\n}<\/p>\n<p><span class=\"hljs-comment\">\/\/ ---- Critical Section ----<\/span><\/p>\n<p><span class=\"hljs-comment\">\/\/ Exit Section<\/span><br \/>\nturn = <span class=\"hljs-number\">1<\/span> - i;<br \/>\nflag[i] = <span class=\"hljs-literal\">false<\/span>;<br \/>\n<\/code><\/div>\n<\/div>\n<hr class=\"\" data-start=\"1372\" data-end=\"1375\" \/>\n<h3 class=\"\" data-start=\"1377\" data-end=\"1412\">\u2705 <strong data-start=\"1383\" data-end=\"1412\">Explanation of Key Parts:<\/strong><\/h3>\n<ul data-start=\"1414\" data-end=\"1694\">\n<li class=\"\" data-start=\"1414\" data-end=\"1465\">\n<p class=\"\" data-start=\"1416\" data-end=\"1465\"><strong data-start=\"1416\" data-end=\"1435\">flag[i] = true:<\/strong> Process Pi wants to enter CS.<\/p>\n<\/li>\n<li class=\"\" data-start=\"1466\" data-end=\"1546\">\n<p class=\"\" data-start=\"1468\" data-end=\"1546\"><strong data-start=\"1468\" data-end=\"1490\">while (flag[1-i]):<\/strong> If the other process also wants to enter, check <code data-start=\"1539\" data-end=\"1545\">turn<\/code>.<\/p>\n<\/li>\n<li class=\"\" data-start=\"1547\" data-end=\"1616\">\n<p class=\"\" data-start=\"1549\" data-end=\"1616\"><strong data-start=\"1549\" data-end=\"1566\">If turn != i:<\/strong> It waits and gives a chance to the other process.<\/p>\n<\/li>\n<li class=\"\" data-start=\"1617\" data-end=\"1694\">\n<p class=\"\" data-start=\"1619\" data-end=\"1694\"><strong data-start=\"1619\" data-end=\"1640\">After exiting CS:<\/strong> Sets <code data-start=\"1646\" data-end=\"1652\">turn<\/code> to the other process and resets its flag.<\/p>\n<\/li>\n<\/ul>\n<hr class=\"\" data-start=\"1696\" data-end=\"1699\" \/>\n<h3 class=\"\" data-start=\"1701\" data-end=\"1729\">\ud83e\uddfe <strong data-start=\"1708\" data-end=\"1729\">Example Scenario:<\/strong><\/h3>\n<ol data-start=\"1731\" data-end=\"2021\">\n<li class=\"\" data-start=\"1731\" data-end=\"1770\">\n<p class=\"\" data-start=\"1734\" data-end=\"1770\"><strong data-start=\"1734\" data-end=\"1747\">P0 and P1<\/strong> both want to enter CS.<\/p>\n<\/li>\n<li class=\"\" data-start=\"1771\" data-end=\"1821\">\n<p class=\"\" data-start=\"1774\" data-end=\"1821\">Both set <code data-start=\"1783\" data-end=\"1799\">flag[0] = true<\/code> and <code data-start=\"1804\" data-end=\"1820\">flag[1] = true<\/code>.<\/p>\n<\/li>\n<li class=\"\" data-start=\"1822\" data-end=\"1951\">\n<p class=\"\" data-start=\"1825\" data-end=\"1859\">Now, both check each other\u2019s flag:<\/p>\n<ul data-start=\"1863\" data-end=\"1951\">\n<li class=\"\" data-start=\"1863\" data-end=\"1905\">\n<p class=\"\" data-start=\"1865\" data-end=\"1905\">If <code data-start=\"1868\" data-end=\"1878\">turn = 1<\/code>, P0 waits and P1 proceeds.<\/p>\n<\/li>\n<li class=\"\" data-start=\"1909\" data-end=\"1951\">\n<p class=\"\" data-start=\"1911\" data-end=\"1951\">If <code data-start=\"1914\" data-end=\"1924\">turn = 0<\/code>, P1 waits and P0 proceeds.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li class=\"\" data-start=\"1952\" data-end=\"2021\">\n<p class=\"\" data-start=\"1955\" data-end=\"2021\">After exiting CS, the current process gives <strong data-start=\"1999\" data-end=\"2020\">turn to the other<\/strong>.<\/p>\n<\/li>\n<\/ol>\n<hr class=\"\" data-start=\"2023\" data-end=\"2026\" \/>\n<h3 class=\"\" data-start=\"2028\" data-end=\"2084\">\ud83d\udcca <strong data-start=\"2035\" data-end=\"2084\">Satisfaction of Synchronization Requirements:<\/strong><\/h3>\n<div class=\"_tableContainer_16hzy_1\">\n<div class=\"_tableWrapper_16hzy_14 group flex w-fit flex-col-reverse\">\n<table class=\"w-fit min-w-(--thread-content-width)\" data-start=\"2086\" data-end=\"2488\">\n<thead data-start=\"2086\" data-end=\"2165\">\n<tr data-start=\"2086\" data-end=\"2165\">\n<th data-start=\"2086\" data-end=\"2107\" data-col-size=\"sm\">Requirement<\/th>\n<th data-start=\"2107\" data-end=\"2120\" data-col-size=\"sm\">Satisfied?<\/th>\n<th data-start=\"2120\" data-end=\"2165\" data-col-size=\"sm\">How?<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"2246\" data-end=\"2488\">\n<tr data-start=\"2246\" data-end=\"2326\">\n<td data-start=\"2246\" data-end=\"2269\" data-col-size=\"sm\"><strong data-start=\"2248\" data-end=\"2268\">Mutual Exclusion<\/strong><\/td>\n<td data-start=\"2269\" data-end=\"2281\" data-col-size=\"sm\">\u2705<\/td>\n<td data-col-size=\"sm\" data-start=\"2281\" data-end=\"2326\">Only one process enters CS at a time<\/td>\n<\/tr>\n<tr data-start=\"2327\" data-end=\"2407\">\n<td data-start=\"2327\" data-end=\"2350\" data-col-size=\"sm\"><strong data-start=\"2329\" data-end=\"2341\">Progress<\/strong><\/td>\n<td data-col-size=\"sm\" data-start=\"2350\" data-end=\"2362\">\u2705<\/td>\n<td data-col-size=\"sm\" data-start=\"2362\" data-end=\"2407\">No process is blocked unnecessarily<\/td>\n<\/tr>\n<tr data-start=\"2408\" data-end=\"2488\">\n<td data-start=\"2408\" data-end=\"2431\" data-col-size=\"sm\"><strong data-start=\"2410\" data-end=\"2429\">Bounded Waiting<\/strong><\/td>\n<td data-start=\"2431\" data-end=\"2443\" data-col-size=\"sm\">\u2705<\/td>\n<td data-col-size=\"sm\" data-start=\"2443\" data-end=\"2488\">Each process eventually gets a turn<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"sticky end-(--thread-content-margin) h-0 self-end select-none\">\n<div class=\"absolute end-0 flex items-end\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr class=\"\" data-start=\"2490\" data-end=\"2493\" \/>\n<h3 class=\"\" data-start=\"2495\" data-end=\"2524\">\u2753<strong data-start=\"2500\" data-end=\"2524\">GATE-Style Question:<\/strong><\/h3>\n<p class=\"\" data-start=\"2526\" data-end=\"2589\"><strong data-start=\"2526\" data-end=\"2532\">Q:<\/strong> In Dekker\u2019s algorithm, what ensures <strong data-start=\"2569\" data-end=\"2588\">bounded waiting<\/strong>?<\/p>\n<p class=\"\" data-start=\"2591\" data-end=\"2682\"><strong data-start=\"2591\" data-end=\"2597\">A.<\/strong> <code data-start=\"2598\" data-end=\"2606\">flag[]<\/code><br data-start=\"2606\" data-end=\"2609\" \/><strong data-start=\"2609\" data-end=\"2615\">B.<\/strong> <code data-start=\"2616\" data-end=\"2622\">turn<\/code> variable<br data-start=\"2631\" data-end=\"2634\" \/><strong data-start=\"2634\" data-end=\"2640\">C.<\/strong> Both A and B<br data-start=\"2653\" data-end=\"2656\" \/><strong data-start=\"2656\" data-end=\"2662\">D.<\/strong> None of the above<\/p>\n<p class=\"\" data-start=\"2684\" data-end=\"2753\">\u2705 <strong data-start=\"2686\" data-end=\"2697\">Answer:<\/strong> <strong data-start=\"2698\" data-end=\"2703\">C<\/strong> \u2014 Both are required to implement bounded waiting.<\/p>\n<hr class=\"\" data-start=\"2755\" data-end=\"2758\" \/>\n<p class=\"\" data-start=\"2760\" data-end=\"2775\">Would you like:<\/p>\n<ul data-start=\"2776\" data-end=\"2929\">\n<li class=\"\" data-start=\"2776\" data-end=\"2824\">\n<p class=\"\" data-start=\"2778\" data-end=\"2824\">A <strong data-start=\"2780\" data-end=\"2801\">diagram\/flowchart<\/strong> of Dekker\u2019s Algorithm?<\/p>\n<\/li>\n<li class=\"\" data-start=\"2825\" data-end=\"2886\">\n<p class=\"\" data-start=\"2827\" data-end=\"2886\">A comparison between <strong data-start=\"2848\" data-end=\"2885\">Dekker\u2019s and Peterson\u2019s Algorithm<\/strong>?<\/p>\n<\/li>\n<li class=\"\" data-start=\"2887\" data-end=\"2929\">\n<p class=\"\" data-start=\"2889\" data-end=\"2929\"><strong data-start=\"2889\" data-end=\"2919\">MCQs and practice problems<\/strong> for GATE?<\/p>\n<\/li>\n<\/ul>\n<p class=\"\" data-start=\"2931\" data-end=\"2943\">Let me know!<\/p>\n<h3 data-start=\"2931\" data-end=\"2943\"><a href=\"https:\/\/sriindu.ac.in\/wp-content\/uploads\/2023\/10\/R20CSE2202-OPERATING-SYSTEMS.pdf\" target=\"_blank\" rel=\"noopener\">Subject &#8211; Operating System Topic &#8211; Process Synchronization (example based on Algo-2).<\/a><\/h3>\n<h3 class=\"LC20lb MBeuO DKV0Md\"><a href=\"https:\/\/web.cs.wpi.edu\/~cs3013\/c07\/lectures\/Section06-Sync.pdf\" target=\"_blank\" rel=\"noopener\">OPERATING SYSTEMS PROCESS SYNCHRONIZATION<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Subject &#8211; Operating System Topic &#8211; Process Synchronization (example based on Algo-2). [fvplayer id=&#8221;360&#8243;] Here is a GATE-focused explanation for: \ud83c\udf93 Subject: Operating System \ud83d\udccc Topic: Process Synchronization \ud83e\uddea Example Based on Algo-2 \u2013 Dekker\u2019s Algorithm \ud83d\udd0d What is Process Synchronization? In multiprocessing systems, multiple processes may try to access shared resources (like memory or [&hellip;]<\/p>\n","protected":false},"author":66,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-3339","post","type-post","status-publish","format-standard","hentry","category-operating-system"],"_links":{"self":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts\/3339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/comments?post=3339"}],"version-history":[{"count":0,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts\/3339\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/media?parent=3339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/categories?post=3339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/tags?post=3339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}