niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Unix internals
what is the difference between critical section, mutex and semophore?
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
puzzlecracker
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 261

PostPosted: Fri Jun 17, 2005 5:47 pm    Post subject: what is the difference between critical section, mutex and semophore? Reply with quote

what is the difference between critical section, mutex and semophore?
Back to top
Lew Pitcher
*nix forums Guru


Joined: 21 Feb 2005
Posts: 675

PostPosted: Fri Jun 17, 2005 9:08 pm    Post subject: Re: what is the difference between critical section, mutex and semophore? Reply with quote

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

puzzlecracker wrote:
Quote:
what is the difference between critical section, mutex and semophore?

A "critical section" is a part of a code path which cannot tolerate sharing
it's resources with other, asynchronous, code paths.

A concrete (but somewhat silly) example of a critical section would be the
interval between the time you place food in your mouth, and the time you
swallow the food. During that time, you cannot tolerate interruptions that
require you to use your mouth for talking.


A "mutex" (or "mutual exclusion lock") is a signal that two or more
asynchronous processes can use to reserve a shared resource for exclusive use.
The first process that obtains ownership of the "mutex" also obtains ownership
of the shared resource. Other processes must wait for for the first process to
release it's ownership of the "mutex" before they may attempt to obtain it.

A concrete example of a "mutex" would be a bathroom door. Each person wishing
to use the toilet must first be able to open the bathroom door. If they cannot
open the door (because it is locked), they wait outside the bathroom until the
current occupant finishes with the toilet, and unlocks the door. Each
successive occupant locks the door before using the toilet (thus preventing
others from entering and attempting to use the toilet while they are using
it), and unlocks the door after they are done. The bathroom door is a "mutex"
mechanism for the toilet (shared resource) inside.


Finally, a "semaphore" is a sort of "mutex" that is used to signal the
availability of a plentiful resource. The source of the resource adds one to
the semaphore for each unused resource available. As resources are taken away,
the count in the semaphore is decremented (one for each removed resource)
until it reaches zero (meaning that no more resources are available). At this
point, any remaining consumers of the resource must wait until the "semaphore"
increments above zero, indicating that more resources are available.

A concrete example of a "semaphore" would be the "Number of cups" level on
your coffee urn. Your coffee urn starts off full, with a large "number of
cups". As each coffee drinker pours a cup, the level in the urn goes down and
the "number of cups" count decreases. When the last of the coffee is poured,
the "number of cups" reaches zero and someone refills the urn. Any further
coffee drinkers must wait until the "number of cups" in the coffee urn has
increased above zero. As it does, each drinker in turn can pour a cup (thus
decreasing the count again), until the count again reaches zero.

Does this help?

PS: Read the first two chapters of "Operating Systems - Design and
Implementation" by Andy Tanenbaum. It's all there (except for the colourful
examples Smile ).

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCs1f9agVFX4UWr64RAuEfAKDjJRkKFnuq+fv1P46b4/ZBJedtqgCgm0un
wUzQMmkk/0dw2aBcuYyvMck=
=U5q4
-----END PGP SIGNATURE-----
Back to top
puzzlecracker
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 261

PostPosted: Sun Jun 19, 2005 3:06 pm    Post subject: Re: what is the difference between critical section, mutex and semophore? Reply with quote

pretty clear now -really colorful... thanks..
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 6:34 am | All times are GMT
navigation Forum index » Programming » Unix internals
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts HPSBMA02133 SSRT061201 rev.1 - HP Oracle for OpenView (Of... Security Alert HP-UX 0 Thu Jul 20, 2006 6:43 pm
No new posts HPSBMA02133 SSRT061201 rev.1 - HP Oracle for OpenView (Of... Security Alert HP-UX 0 Thu Jul 20, 2006 5:53 pm
No new posts FAQ 1.12 What's the difference between "perl" and "Perl"? PerlFAQ Server Perl 0 Wed Jul 19, 2006 7:03 pm
No new posts Difference between function and procedure? Anastasios Hatzis PostgreSQL 3 Wed Jul 19, 2006 3:46 pm
No new posts 10.1 Critical Error KDM orrin1933-usenet@yahoo.co Suse 1 Tue Jul 18, 2006 11:08 pm

Mortgage | Bankruptcy | Car Finance | Personal Loans | Mortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1324s ][ Queries: 16 (0.0598s) ][ GZIP on - Debug on ]