10-07-2011
Note: In what follows, a "file description" and a "file descriptor" are not synonymous.
When you open() a file or use the pipe() system call, the kernel will create what's called a file description. This file description is a data structure that keeps track of the file offset, permissions, access mode, etc, associated with the opened resource. Aside from creating that file description, an entry is added to the process file descriptor table and you are given an integer index which points to that new entry; this is the file descriptor.
Both the file description and file descriptor tables are inside the kernel's address space. A file description is a system-wide entity. File descriptor tables are a per-process data structure. Each process has its own descriptor table. There can be multiple file descriptors pointing to the same underlying file description.
When you fork, the newly-created process is provided with its own copy of the parent's descriptor table. Initially, each entry in the child's descriptor table points to the same underlying open file description as its counterpart in the parent's table. The same is true when you exec() a new executable image, except that file descriptors which have had their close-on-exec flag set are closed.
An open file description is not closed until all file descriptors in all processes which point to that file description are closed.
Since different file descriptors in different processes can manipulate the same underlying file description, it can be considered a mode of interprocess communication.
That's probably a lot of jargon to digest at once, but I believe it covers the essentials.
Regards,
Alister
Last edited by alister; 10-07-2011 at 07:18 PM..
This User Gave Thanks to alister For This Post:
10 More Discussions You Might Find Interesting
1. Filesystems, Disks and Memory
Hello!
How I can increase or decrease predefined pipe buffer size?
System FreeBSD 4.9 and RedHat Linux 9.0
Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
2. Shell Programming and Scripting
Hi,
Can the cd command be invoked using pipes???
My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies
3. UNIX for Advanced & Expert Users
Hi...Can anyone please guide me on FIFO Pipes in UNIX.I have lerant things like creating fifo pipes,using them for reads and writes etc.I want to know what is the maximum amount of memory that such a pipe may have? Also can anyone guide me on where to get info on this topic from? (4 Replies)
Discussion started by: tej.buch
4 Replies
4. UNIX for Advanced & Expert Users
This is something I've given a lot of thought to and come up with no answer.
Say you have a data stream passing from a file, through process A, into process B. Process A only modifies a few bytes of the stream, then prints the rest of the stream unmodified. Is there any way to stream the file... (4 Replies)
Discussion started by: Corona688
4 Replies
5. Shell Programming and Scripting
How to have a conversation between 2 processes using named pipes? (5 Replies)
Discussion started by: kanchan_agr
5 Replies
6. UNIX for Dummies Questions & Answers
im trying to figure out how to do the following:
using pipes to combine grep and find commands to print all lines in files that start with the letter f in the current directory that contain the word "test" for example?
again using pipes to combine grep and find command, how can I print all... (1 Reply)
Discussion started by: ez45
1 Replies
7. Shell Programming and Scripting
I am really new to UNIX and programming in general and so apologies if this thread is a bit simple.
I have searched and found a piece of sample code for a training program I am currently undertaking, but seeing as I am relatively new, I dont completely understand how it works.
Here is the... (1 Reply)
Discussion started by: Makaer
1 Replies
8. UNIX for Dummies Questions & Answers
I am really new to UNIX and programming in general and so apologies if this thread is a bit simple.
I have searched and found a piece of sample code for a training program I am currently undertaking, but seeing as I am relatively new, I dont completely understand how it works.
Here is the... (6 Replies)
Discussion started by: Makaer
6 Replies
9. Programming
Hello all, I am trying to learn more about programming Unix pipes in C.
I have created a pipe that does od -bc < myfile | head
Now, I am trying to create od -bc < myfile | head | wc
Here is my code, and I know I might be off, thats why I am here so I can get some clarification.
#include... (1 Reply)
Discussion started by: petrca
1 Replies
10. Homework & Coursework Questions
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
Create a pipe to show the number of people who are logged into the system right now.
Create a pipe to show... (2 Replies)
Discussion started by: lakers34kb
2 Replies
LEARN ABOUT OPENDARWIN
close
CLOSE(2) BSD System Calls Manual CLOSE(2)
NAME
close -- delete a descriptor
SYNOPSIS
#include <unistd.h>
int
close(int d);
DESCRIPTION
The close() call deletes a descriptor from the per-process object reference table. If this is the last reference to the underlying object,
the object will be deactivated. For example, on the last close of a file the current seek pointer associated with the file is lost; on the
last close of a socket(2) associated naming information and queued data are discarded; on the last close of a file holding an advisory lock
the lock is released (see further flock(2)).
When a process exits, all associated file descriptors are freed, but since there is a limit on active descriptors per processes, the close()
function call is useful when a large quantity of file descriptors are being handled.
When a process forks (see fork(2)), all descriptors for the new child process reference the same objects as they did in the parent before the
fork. If a new process is then to be run using execve(2), the process would normally inherit these descriptors. Most of the descriptors can
be rearranged with dup2(2) or deleted with close() before the execve is attempted, but if some of these descriptors will still be needed if
the execve fails, it is necessary to arrange for them to be closed if the execve succeeds. For this reason, the call ``fcntl(d, F_SETFD,
1)'' is provided, which arranges that a descriptor will be closed after a successful execve; the call ``fcntl(d, F_SETFD, 0)'' restores the
default, which is to not close the descriptor.
RETURN VALUES
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and the global integer variable errno is set to
indicate the error.
ERRORS
Close() will fail if:
[EBADF] D is not an active descriptor.
[EINTR] An interrupt was received.
SEE ALSO
accept(2), flock(2), open(2), pipe(2), socket(2), socketpair(2), execve(2), fcntl(2)
STANDARDS
Close() conforms to IEEE Std 1003.1-1988 (``POSIX.1'').
4th Berkeley Distribution April 19, 1994 4th Berkeley Distribution