{"id":416,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:19","modified_gmt":"2020-08-22T20:26:19","slug":"this-is-the-page-title-toplevel-249","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=416","title":{"rendered":"Semaphores"},"content":{"rendered":"\n<title> Semaphores<\/title>\n<p>\nSemaphores are used to protect critical regions of code or data structures.\nRemember that each access of a critical piece of data such as a VFS inode describing\na directory is made by kernel code running on behalf of a process.\nIt would be very dangerous to allow one process to alter a critical data structure\nthat is being used by another process.\nOne way to achieve this would be  to use a buzz lock around the critical piece\nof data that is being accessed, but this is a simplistic approach that would degrade\nsystem performance.\n<p>\nInstead Linux uses semaphores to allow just one process at a time to access\ncritical regions of code and data; all other processes wishing to access this\nresource will be made to wait until it becomes free.\nThe waiting processes are suspended, other processes in the system can continue\nto run as normal.\n<p>\nA Linux <tt>semaphore<\/tt> data structure contains the following information:\n<dl compact>\n<p>\n\t<dt><b>count<\/b><\/dt><dd>\t This field keeps track of the count of processes wishing\n\t\tto use this resource.  A positive value means that the resource is\n\t\tavailable.  A negative or zero value means that processes are waiting\n\t\tfor it.  An initial value of 1 means that one and only one process at\n\t\ta time can use this resource.  When processes want this resource they\n\t\tdecrement the count and when they have finished with this resource they\n\t\tincrement the count,\n\t<dt><b>waking<\/b><\/dt><dd> This is the count of processes waiting for this resource which is\n\t\talso the number of process waiting to be awakened when this resource\n\t\tbecomes free,\n\t<dt><b>wait queue<\/b><\/dt><dd> When processes are waiting for this resource they are put\n\t\tonto this wait queue,\n\t<dt><b>lock<\/b><\/dt><dd> A buzz lock used when accessing the <tt>waking<\/tt> field.\n<\/dl>\n<p>\nSuppose the initial count for a semaphore is 1, the first process to come along\nwill see that the count is positive and decrement it by 1, making it 0.\nThe process now &#8220;owns&#8221; the critical piece of code or resource that is being protected\nby the semaphore.\nWhen the process leaves the critical region it increments the semphore&#8217;s count.\nThe most optimal case is where there are no other processes contending for ownership of\nthe critical region.\nLinux has implemented semaphores to work efficiently for this, the most common case.\n<p>\nIf another process wishes to enter the critical region whilst it is owned by a\nprocess it too will decrement the count.\nAs the count is now negative (-1) the process cannot enter the critical region.\nInstead it must wait until the owning process exits it.\nLinux makes the waiting process sleep until the owning process wakes it on exiting\nthe critical region.\nThe waiting process adds itself to the semaphore&#8217;s wait queue and sits in a loop\nchecking the value of the <tt>waking<\/tt> field and calling the scheduler until\n<tt>waking<\/tt> is non-zero.\n<p>\nThe owner of the critical region increments the semaphore&#8217;s count and if it is less\nthan or equal to zero then there are processes sleeping, waiting for this resource.\nIn the optimal case the semaphore&#8217;s count would have been returned to its initial\nvalue of 1 and no further work would be neccessary.\nThe owning process increments the waking counter and wakes up the process sleeping on\nthe semaphore&#8217;s wait queue.\nWhen the waiting process wakes up, the waking counter is now 1 and it knows that\nit may now enter the critical region.\nIt decrements the waking counter, returning it to a value of zero, and continues.\nAll access to the waking field of semaphore are protected by a buzz lock using the semaphore&#8217;s\nlock.\n","protected":false},"excerpt":{"rendered":"<p>Semaphores Semaphores are used to protect critical regions of code or data structures. Remember that each access of a critical piece of data such as a VFS inode describing a directory is made by kernel code running on behalf of &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=416\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-416","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=416"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/416\/revisions"}],"predecessor-version":[{"id":695,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/416\/revisions\/695"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}