08-28-2007
Semaphores are not inherently linked to memory, processes, code or files.
Semaphores are flags that processes, including processes that access files or shared memory, use to know a guaranteed status, which is often referred to as "atomic".
Let's say you are writing a shared memory application. You don't want to read or write to shared memory when another process is writing to it (if that process has write permissions). Also, you don't want to read a file that is being changed, etc. So, you create a semaphore and you designate two states:
- semaphore set, means that a process with write permission has a file descriptor open.
- semaphore clear, means that there is no process with write permissions accessing the shared memory.
Now, each process that accesses the shared memory must check the status of the semaphore when they (it) open the file (in this case shared memory) for access.
There are many applications for semaphores for interprocess communications, not limited to shared memory; but shared memory certainly required atomic flags for interprocess communications.
Think of this like a person on a single track of train track holding a flag. One train is flying down the track at 200 kph in one direction and other train is heading toward the same track segment from the opposite direction. There is a switch at one section of track where one train can wait while the other passes. A switchman, in the old days, would hold up a flag (later done with lights) to give status to both trains.
You can see that trusting the flags by the switchman is critically important, folks could dies if the flag is incorrect or the conductor of the train does not watch it.
The same is true in interprocess communications, and semaphores are guaranteed by the UNIX / Linux kernel to be atomic, dead right on.
File locks are not guaranteed by the kernel to be atomic, as I recall, so use them with caution.
9 More Discussions You Might Find Interesting
1. HP-UX
In my location we have Hp storage (san),from there they created luns,
so could u plz send me clear document for understanding LUN. (1 Reply)
Discussion started by: Prem
1 Replies
2. UNIX for Dummies Questions & Answers
Hi all
This is my first thread here.i confused with the concept of thread.Can anyone tell me this concept in detail.my Quation may be at primary level.
Thanx in advance for help. (1 Reply)
Discussion started by: vishwasrao
1 Replies
3. UNIX for Dummies Questions & Answers
Hi all,
I am new here ,i want to know about interrupts in detail.What r
Interrupts .how they r handeled.
Thanx in adavnce. (1 Reply)
Discussion started by: vishwasrao
1 Replies
4. Shell Programming and Scripting
Hi all, I used array a lot in C,VB,C# and java but now i am very new to shell programming,so i need a start of array in shell programming. All i want to do is read a string and put it into a character type array. For reading the string,i did this:
$ read a
now i want to put the content of a... (1 Reply)
Discussion started by: gautamshaw
1 Replies
5. AIX
hi.... Friends...
Why using mirroring ?
what is the use of mirroring? just any one tell about clearly....
thanks.... (4 Replies)
Discussion started by: Kannan841
4 Replies
6. UNIX for Dummies Questions & Answers
i couldn't get what does the metainit command represents in numeric values.
(i.e)
#metainit d66 2 1 c0t0d0s4 1 c0t0d0s5
??here 2 1 1 represnts what ??
can some one tell clearly about this... (6 Replies)
Discussion started by: sriniv666
6 Replies
7. UNIX for Advanced & Expert Users
Hi Gurus,
Please help me in below requirement.
Instance =5 (it is user parameter)
total=52 (it is user parameter
i need to split this to 5 and reminder as 1 instances totally 6
for example i need to splitt to each
52/5=10.4
1-10
11-20
21-30
31-40
41-50 (2 Replies)
Discussion started by: ragu.selvaraj
2 Replies
8. UNIX for Dummies Questions & Answers
Hi..
Could someone explain about setfacl,getfacl in unix and its uses.
Regards,
Suresh (1 Reply)
Discussion started by: suresh sunkara
1 Replies
9. UNIX for Dummies Questions & Answers
Is wc -c and wc -m same ?
Shellscript::cat file1
hello
Shellscript::cat file1 | wc -c
6
Shellscript::cat file1 | wc -m
6
Shellscript::file file1
file1: ASCII text
Shellscript::uname -a
Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/LinuxAtleast... (5 Replies)
Discussion started by: shellscripting
5 Replies
LEARN ABOUT OSX
sem_unlink
SEM_UNLINK(2) BSD System Calls Manual SEM_UNLINK(2)
NAME
sem_unlink -- remove a named semaphore
SYNOPSIS
#include <semaphore.h>
int
sem_unlink(const char *name);
DESCRIPTION
The named semaphore named name is removed. If the semaphore is in use by other processes, then name is immediately disassociated with the
semaphore, but the semaphore itself will not be removed until all references to it have been closed. Subsequent calls to sem_open() using
name will refer to or create a new semaphore named name.
If successful, sem_unlink() will return 0. Otherwise, -1 is returned and errno is set, and the state of the semaphore is unchanged.
ERRORS
sem_unlink() succeeds unless:
[EACCES] Permission is denied to be remove the semaphore.
[ENAMETOOLONG] name exceeded SEM_NAME_LEN characters.
[ENOENT] The named semaphore does not exist.
SEE ALSO
sem_close(2), sem_open(2), semctl(2), semget(2), semop(2)
HISTORY
sem_unlink() is specified in the POSIX Realtime Extension (1003.1b-1993/1003.1i-1995).
Darwin June 8, 2000 Darwin