[lug] Semaphore stuff

Sebastian Sobolewski spsobole at mindless.com
Fri Feb 25 21:14:36 MST 2000


         Semaphores are used to control access to limited resources that 
may need to be used by multiple processes.  For instance a Printer. 
(Assuming no spooling)  Only one process can actually print directly to a 
printer but you may want to have multiple processes print.  What a 
semaphore will do is simply BLOCKS (or puts to sleep) any additional 
processes that attempt to access a printer if another process is already 
using it.  Once the first process completes the next one is "woken up" and 
allowed to use the Printer. (or another resource)  Think of it as a 
blocking function call. (These are usually handled by semaphores).

         By semaphore limit they may mean the maximum number of processes 
that may be allowed to wait on a resource.  But I've never heard of anyone 
actually talking about a semaphore limit. Usually a process limit is hit 
much earlier then a semaphore limit in any OS. (Except windows 95/98 .. 
that anoying Low Resources bug when you have 128 MB free)

         Anyhow according to my OS Design book:

         A Semaphore is an Operating System feature. (But can be 
implemented on a per application basis).  There are two possible operations 
on a semaphore either an UP or a DOWN.  Both these operations are atomic, 
which means that the OS nor CPU can interrupt a UP or DOWN call. (Doing so 
would allow a process to deadlock the OS)

         When a process performs a DOWN operation it first checks if the 
semaphore value is greater then 0.  If so it decrements the value and 
continues execution.  If the value is 0 the process is put to sleep by the 
OS and not allowed to complete the DOWN operation.

         The UP operation increments the semaphore.  If one or more 
processes were sleeping on that semaphore the OS picks one based on a 
predetermined algorithm  (Usually either by random or FIFO) and allows that 
process to complete it's DOWN call.


-Sebastian
At 06:35 PM 2/25/00 -0700, you wrote:

>----- Original Message -----
>From: Hugh Brown <hugh at math.byu.edu>
>To: <lug at lug.boulder.co.us>
>Sent: Friday, February 25, 2000 6:05 PM
>Subject: Re: [lug] Semaphore stuff
>
>
> > In addition to the original query, can someone explain briefly what a
> > semaphore is (with regards to OS's).
> >
> > Hugh
>
>Without going into a lot of gruesome detail, a semaphore is a flag (usually
>stored as a file)
>which marks the state of a specific resource (like a physical i/o port) as
>being unavailable,
>or awaiting input, or whatever.  For example, when a printer is actively
>accepting input from a
>print server, it is customary for a "lock" file to exist in the print-spool
>var directory and linked
>by name with the active device.  This precludes its use by another
>application, or a simultaneous
>print job (if you've got more than one lpd which can access the port).  Once
>the job concludes satisfactorily, the lock file is removed and the device is
>once again available.
>
>A semaphore can be a flag or variable inside a single program which does the
>same sort of
>thing, but within a single process.  An EOF semaphore is often used in C++
>code to signal when a file or i/o channel has run out of data.  Etc. etc.
>
>-- Dave Lovering
>
>
>_______________________________________________
>Web Page:  http://lug.boulder.co.us
>Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug






More information about the LUG mailing list