

{"id":100284,"date":"2021-08-20T09:00:48","date_gmt":"2021-08-20T03:30:48","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=100284"},"modified":"2021-08-18T17:58:04","modified_gmt":"2021-08-18T12:28:04","slug":"semaphores-in-operating-system","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/","title":{"rendered":"Semaphores in Operating System"},"content":{"rendered":"<p>Integer variables that help solve the critical section problem are known as semaphores. Wait and signal are the two atomic operations that help achieve process synchronization in a multiprocessing environment..<\/p>\n<p>If an argument, let&#8217;s say argument A is positive, then the wait (sleep or down) operation decrements its value. And if argument A is negative or zero, then no operation occurs. Whereas, the signal (wake-up or up) operation increments the value of argument A.<\/p>\n<p>P(A) is <strong>wait<\/strong> and V(A) is <strong>signal.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">P(A): if A &gt;= 1 then A := A - 1\r\n      else &lt;block and enqueue the process&gt;;\r\n\r\nV(A): if &lt;some process is blocked on the queue&gt;\r\n        then &lt;unblock a process&gt;\r\n      else A := A + 1;\r\n<\/pre>\n<h3>Properties of Semaphores<\/h3>\n<p>Following are the properties of semaphore:<\/p>\n<ul>\n<li>Simple and has a positive integer value.<\/li>\n<li>Can work with multiple processes.<\/li>\n<li>Different critical sections for different semaphores.<\/li>\n<li>An unique access semaphore for each critical section.<\/li>\n<li>Allows multiple processes into the critical section at a time, if required.<\/li>\n<\/ul>\n<h3>Types of Semaphore<\/h3>\n<p>There are two types of semaphores:<\/p>\n<p><strong>1. Counting Semaphore<\/strong><\/p>\n<p>Counting semaphores are integer value semaphores. They have an unrestricted value domain and are used to coordinate resource access. During this coordination, the count of semaphores is equal to the number of available resources and if a resource is added or removed, then the count of semaphores changes accordingly.<\/p>\n<p><strong>2. Binary Semaphore<\/strong><\/p>\n<p>Binary semaphores are similar to the above, just that their value is restricted to 0 and 1. The wait and signal operation only works when the semaphore is 1 and 0 respectively. Binary semaphores are easier to implement.<\/p>\n<h3>Counting Semaphore vs Binary Semaphore<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>COUNTING SEMAPHORE<\/b><\/td>\n<td><b>BINARY SEMAPHORE<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">No mutual exclusion<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Mutual exclusion is present<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Any integer value possible<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Two integer values, 0 and 1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">More than one slots<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Only one slot<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Offers a set of processes<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Contains mutual exclusion mechanism<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Semaphore vs Mutex<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>PARAMETER<\/b><\/td>\n<td><b>SEMAPHORE<\/b><\/td>\n<td><b>MUTEX<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Data Type<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Integer variable<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Mechanism<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Signaling mechanism<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Type of locking mechanism<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Types<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Binary and Counting\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">No Type<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Operation<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Wait and signal modifies the semaphore\u2019s value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Locked and Unlocked operation<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Thread<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Multiple program threads<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Multiple program threads that aren\u2019t simultaneous<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Advantages of OS Semaphores<\/h3>\n<p>Following are the advantages of semaphore:<\/p>\n<ul>\n<li>They allow one process into the critical section at a time and follow the principle of mutual exclusion. They are far more efficient than other methods of synchronization.<\/li>\n<li>Zero wastage of resources due to busy waiting in semaphore.<\/li>\n<li>The OS implements a semaphore in the machine-independent code of the microkernel. This makes them machine-independent.<\/li>\n<\/ul>\n<h3>Disadvantages of Semaphore in Operating System<\/h3>\n<p>Following are the disadvantages of semaphores:<\/p>\n<ul>\n<li>It is necessary to implement the wait and signal operations in the correct order in order to prevent deadlocks.<\/li>\n<li>They are impractical for last-scale use. This is because it leads to loss of modularity as wait and signal operations don\u2019t allow the creation of a structured layout for the system.<\/li>\n<li>Semaphores can cause a priority inversion where low priority processes may get the chance to access the critical section before the high priority processes.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>A semaphore is a variable that helps with critical section problems and process synchronisation. Wait and signal are the two operations that help modify the value of a semaphore. There are two types of semaphores, namely, counting and binary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Integer variables that help solve the critical section problem are known as semaphores. Wait and signal are the two atomic operations that help achieve process synchronization in a multiprocessing environment.. If an argument, let&#8217;s&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":100346,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24565],"tags":[24962,24964,24963,24965],"class_list":["post-100284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-operating-system-tutorials","tag-semaphore","tag-semaphore-in-operating-system","tag-semaphore-in-os","tag-semaphores"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Semaphores in Operating System - DataFlair<\/title>\n<meta name=\"description\" content=\"Semaphores are variables that help with critical section problems and process synchronisation. Learn its properties, types, advantages etc.\" \/>\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\/semaphores-in-operating-system\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Semaphores in Operating System - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Semaphores are variables that help with critical section problems and process synchronisation. Learn its properties, types, advantages etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/semaphores-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-08-20T03:30:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/08\/OS-Semaphores.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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Semaphores in Operating System - DataFlair","description":"Semaphores are variables that help with critical section problems and process synchronisation. Learn its properties, types, advantages etc.","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\/semaphores-in-operating-system\/","og_locale":"en_US","og_type":"article","og_title":"Semaphores in Operating System - DataFlair","og_description":"Semaphores are variables that help with critical section problems and process synchronisation. Learn its properties, types, advantages etc.","og_url":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-08-20T03:30:48+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/08\/OS-Semaphores.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Semaphores in Operating System","datePublished":"2021-08-20T03:30:48+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/"},"wordCount":510,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/08\/OS-Semaphores.jpg","keywords":["Semaphore","Semaphore in Operating System","Semaphore in OS","Semaphores"],"articleSection":["Operating System Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/","url":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/","name":"Semaphores in Operating System - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/08\/OS-Semaphores.jpg","datePublished":"2021-08-20T03:30:48+00:00","description":"Semaphores are variables that help with critical section problems and process synchronisation. Learn its properties, types, advantages etc.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/semaphores-in-operating-system\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/08\/OS-Semaphores.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/08\/OS-Semaphores.jpg","width":1200,"height":628,"caption":"OS Semaphores"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/semaphores-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":"Semaphores 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\/100284","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=100284"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100284\/revisions"}],"predecessor-version":[{"id":100347,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100284\/revisions\/100347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/100346"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=100284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=100284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=100284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}