From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96078608810101.tru64-unix-managers.marc@archive.invalid> From: Gary Menna Date: Wed, 05 Feb 1997 03:01:46 +0000 To: tru64-unix-managers Subject: Summary Semaphores X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96078608810101 Following is the definitive reference for Semaphores. Thanks to all who replied and for the time out you took to explain your answers in such detail. And Thanks to Craig_Makin@ferntree.com.au who went to the trouble of Faxing me the answer. Original question : Does anyone know what semaphores are / do alan@nabeth : They are an interprocess communcation mechanism used for locking shared resources. Cooperating processes get and set the state of the semaphore to see if they can have access to the resource. I think (but I'm not sure) they can be setup so that a process will block waiting for the semaphore to become free. When the waiting process unblocks it attempts to get the semaphore, if it does, it knows that the other cooperating processes aren't using the particular resource. Cliff Krieger : Semaphores are a form of inter process communication (IPC). There are 3 main types, Message queues, shared memory, and semaphores. Message queues are just that. One process can send a message to another. The other process can, at its leisure get the message and do what it wants with it. Shared memory is just that. There is a chunk of memory that several processes can have access to. You could effectivly use it to pass messages, but it is usually used to move large amounts of data. I think that the loopback device for the network (127.0.0.1) uses shared memory. Semaphores are the weird ones. Basicly think of a semaphore as a queue. A process can put itself in the queue. When it gets in line it goes to sleep. It can only be awakened by another process, it can not wake itself. So, if you had three processes that need to each do something in round robin fashion, you would have the first two get in the semaphore queue and go to sleep. The third process would do what it had to do, wake the first process in the queue, and the go to sleep in the queue it self. Likewise the first process would then do its work, and then wake #2 followed by it going to sleep. IPC is a rather powerful tool in terms of process abstraction, as well as saving system resources. Unfortunatly they do not tend to get used by much since we have a monolith frame of mind. I expect that as multiprocessor systems become more commonplace this will change. You can look at the state of your systems IPC by doing an ipcs command. There is also an ipcrm command that you can use to clean up errant semaphores and other IPC thingys. Greg Merrell : Semaphores are a software mechanism for restricting access to shared resources. At a grosser level, you might have seen them as lock files for things like vipw. Most any good text on operating systems theory/practice should have a section on semaphores and how to use them. Lucio Chiappetti : In Italy semaphores (semafori) are the street lights ! :-) You know, the green, yellow, red things. Or also railway signals. Learn something new every day.....gary !! :-)) Alessandro Macri : Semaphores were introduced by Dijkstra in the mid 60s (there's a lot of literature). They are a simple method of interprocess communication and are often used to handle shared resources. Sigismondo Boschi : They are logical - operating system embedded - components, that decides which process (but not only, even inside the same process) can have access to a given resource. Differently from "locks" that simply deny the access if the resource is yet allocated, semaphores put in a waiting state the request and satify it in the moment the resource becomes free from other preceeding, or higher level, requests. The interface with them is done by using some calls, such the followings: # man -k semaphore (on a linux box) semctl (2) - semaphore control operations semget (2) - get a semaphore set identifier semop (2) - semaphore operations I have never used them on unix-like os, but I have done it on an Amiga I hope the clouds have been blown away! (anonymous) : Here is information from Oracle: "Semaphores can be thought of as flags (hence their name, semaphores). They are either on or off. A process can turn on the flag or turn it off. If the flag is already on, processes who try to turn on the flag will sleep until the flag is off. Upon awakening, the process will reattempt to turn the flag on, possibly suceeding or possibly sleeping again. Such behaviour allows semaphores to be used in implementing a post-wait driver - a system where processes can wait for events (i.e., wait on turning on a semphore) and post events (i.e. turning off of a semaphore). " and .. "One of the important features Unix provides for inter-process communication is the semaphore system. Semaphores are integer-valued objects set aside by the operating system that can be incremented or decremented atomically. They are designed to allow processes to synchronize execution, by only allowing one process to perform an operation on the semaphore at a time. The other process(es) sleep until the semaphores values are either incremented or set to 0, depending on the options used. Semaphores are generally not used one at a time, so Unix uses the concept of semaphore sets to make it easier to allocate and refer to semaphores. When your Unix kernel is configured, the maximum number of semaphores that will be available to the system is set. Also set are the maximum number of semaphores per set, and the maximum number of sets that can be allocated. These limits can only be changed by remaking the Unix kernel and rebooting the machine." Ken Teh : semaphores are used when 2 or more processes need access to a shared resource. They are used to synchronize access so that one process does not clobber what the other process is trying to do. A poor example is a line printer. If process 1 starts printing to a line printer and is preempted by the OS which then starts process 2 which also prints to the line printer, then the output gets clobbered. It's a poor example because line printing is done via the printer daemon, but you get the idea. Henry A. Flogel : Semaphores are KERNEL data structures, similar to boolean variables, that control access to system resources. A process can use a semaphore to hold a resources, request the use of a resource, etc ... Semaphores are usually used to control access to shared memory and disk. They are most often used by an RDBMS, but can be used by any application with complex Inter Process Communication requirements. Thanks again ... Gary Menna E-Mail g.menna@isu.usyd.edu.au Operations Supervisor Phone +61 2 351-5531 Information Technology Services Fax +61 2 351-6004 University of Sydney (H08) Plomo O Plata /\/\/\/\/\/\/\/\/\/\