Semaphores and File Descriptors


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Semaphores and File Descriptors
# 1  
Old 04-19-2009
Semaphores and File Descriptors

What is the difference between a file descriptor and a semaphore?

My basic understanding is:

- a file descriptor is a small positive integer that the system uses instead of the file name to identify an open file or socket.

- a semaphore is a variable with a value that indicates the status of a common resource


How do they specifically differ? Some of the definitions I have read, they sound like the same thing.
# 2  
Old 04-20-2009
The difference is in their purpose and how they are used to achieve this purpose.

Lets start with file descriptors and an example:

Suppose a small taxi company. They operate 10 cars. Of course every car has a distinct "name" - the number on the license plate. But usually - for shortness - the staff will adress these cars by their own internal numbering and talk about the "number-1-car", the "number-2-car", etc.. Probably they have a garage for the cars and the leftmost booth houses the number-1-car, the next booth the number-2-car and so on and the rightmost place has the number-10-car.

Even if a car gets replaced this convention will still hold. The new car will perhaps have a different license plate but it will still be the "new number-3-car", because it gets the place of the old number-3-car.

The shell uses file descriptors in quite the same way: The shell keeps a list of open files like some clerk in our example has a list of the cars and their "real names", i.e. the number on the license plate. But the programs just send their output to the "fourth open file" without having to care which file in fact this is.

Per default there are 3 file descriptors predefined for every UNIX process: stdin, stdout and stderr. The "files" these descriptors point to are the keyboard and the screen (stdout and stderr both point to the screen). (Note that in UNIX most things are handled as a file. The keyboard is such a pseudo-file and a program "reading" it will get what you type as the files content. The same way what you "write" to the stdin or stderr file is printed on the screen.)

You can now redirect these file descriptors to other files like you replace old cars with new ones, and the programs (drivers) will send their output to the new file instead of the old, because they do not rely on filenames for output but on file descriptors. Like the drivers are told to "operate the nr.-3-car for this shift" and they will do so even if the nr.-3-car has been replaced since their last shift.

OK, i think we got that covered. Lets get to the semaphores:

Suppose you and your friend share an appartment. Most of the times this is no problem, but sometimes one o you takes a girl with him and this one doesn't want to be disturbed. This means the other one has to sleep somewhere else. You develop a little code therefore: if one doesn't want to be disturbed he will place a little post-it-sticker at the door. As long as the sticker is there the other one has no business in the apartment. Once the girl has gone the sticker is removed by the one inside. The one outside has to check himself and stay away from the door as long as the sticker is in place.

Semaphores work in exactly this way: two processes both would like to use some resource. Say, they both have to write to some output file. But the cannot write both at the same time and therefore a little code is developed: there is a third file both programs are aware of (the sticker) and each program will first check the contents of this file. If it is "0" this means the output file is free for access (no sticker there, appartment available). When this program opens the shared file it first places a "1" into the signal file (puts a sticker on the appartment door) so the other program will know it cannot access the shared file. Here is some pseudo-code showing how it works:

Without semaphores:
Code:
write_to output_file

With a semaphore file:
Code:
while signal_file reads "1" do
     wait
done
write_to signal_file "1"  /* "raise" the signal prohibiting access for the other process */
write_to output_file "<whatever your output is>"
write_to signal_file "0" /* "lower" the signal allowing access for the other process */

The signal file in this example *is* not a semaphore - it *serves as* a semaphore. A semaphore is not so much a certain device but the way of observing and manipulating such signals - a way of interprocess communication. To come back to the initial example it is not the sticker itself, but the way of communicating through this code.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Questions about file descriptors

Hi, I'm playing with KSH I entered following command in terminal { echo "stdout" >&1; echo "stderr" >&2; } > out And I get only stoud in a new file out. My question is: Where did my stderr vanish ? (5 Replies)
Discussion started by: solaris_user
5 Replies

2. HP-UX

exec and file descriptors

Hi, I speak and write english more or less, so I hope my asking be clear. :) In the company I am working, they are using control-m software to lunch shell scripts. So i put this command in all shell scripts: export LOGFILE_tmp=$PRODUC_DATA/tmp/${SCRIPT}_${PAIS}_`date... (0 Replies)
Discussion started by: anamcara
0 Replies

3. UNIX for Dummies Questions & Answers

Write/read to file descriptors

Is it possible to write to file descriptor 0 and read from 1 or 2? How could this be implemented? (3 Replies)
Discussion started by: machshev
3 Replies

4. UNIX for Dummies Questions & Answers

how to list the files using File Descriptors

hello, I have written a script named listall.sh with the following codes init. #!/bin/bash PATH="/proj/cmon/$1" echo $PATH if ; then echo "Usage: $0 ***" exit 1 else ls -l $PATH/*.sc fi Here there are 3 subdirectories (namely - src, data and jobs)under /proj/cmon, so... (2 Replies)
Discussion started by: shyjuezy
2 Replies

5. UNIX for Advanced & Expert Users

File Descriptors + cron

Hi All, This thread is going to be a discussion basically bringing out more information from the experts on cron jobs and the associated file handles. So, here is the question. There is definitely a constant ' n ' as the maximum number of file handles alloted to a process ' p '. Will... (7 Replies)
Discussion started by: matrixmadhan
7 Replies

6. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies

7. Shell Programming and Scripting

Multiple co-processor file descriptors

I have a script that creates a KSH co-process for Oracle sqlplus and I am presently interacting with it via print -p and read -p. I also need to interact with another Oracle database what isn't permitted to have any direct connection to the first. Presently, I simply disconnect from the first... (10 Replies)
Discussion started by: tmarikle
10 Replies

8. UNIX for Dummies Questions & Answers

file descriptors

i m trying to learn processes in unix and i've been reading this but i don't quite get it. its regarding file descriptors. : each is a part of file pointers, they point to another area. indexes into an Operating system maintained table called "file descriptor table". one table per process. may... (3 Replies)
Discussion started by: a25khan
3 Replies

9. UNIX for Advanced & Expert Users

File Descriptors

Hello all, A few questions on file descriptors ... scenario : Sun Ultra 30 with Sun OS 5.5.1 , E250 with Solaris 2.6 In one of my servers, the file descriptor status from the soft limit and hard limits are 64 and 1024 respectively for root user. Is the soft limit (64) represents the... (3 Replies)
Discussion started by: shibz
3 Replies

10. Programming

File Descriptors

Hi, I have written a daemon process, to perform certain operations in the background. For this I have to close, the open file descriptors, Does anybody know how to find out the number of open file descriptors ? Thanks in Advance, Sheetal (2 Replies)
Discussion started by: s_chordia
2 Replies
Login or Register to Ask a Question