|
-
February 11th, 2008, 11:16 PM
#1
Problem understanding fcntl and retaled issues.
First of all, I'd like to thank the people and the moderators who have helped me by answering my questions. Hopefully When I get better, I will help others in return for your favors.
my question is regarding the fcntl expression in the <fcntl.h> library.
the describtion of this expression is:
//-----------------------------------------------------------------------------------------
The fcntl() function shall perform the operations described below on open files. The fildes argument is a file descriptor.
//-----------------------------------------------------------------------------------------
what is a file descriptor? It was also mentioned for the expression read/write, as follow:
//-----------------------------------------------------------------------------------------
The read() function shall attempt to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf. The behavior of multiple concurrent reads on the same pipe, FIFO, or terminal device is unspecified.
//-----------------------------------------------------------------------------------------
Thank you.
-
February 12th, 2008, 12:07 AM
#2
Re: Problem understanding fcntl and retaled issues.
A file descriptor is a variable that keeps information about the file that the function shall operate on. The content of the descriptor is unknown for the caller.
See also this thread with a similar question and some good answers http://www.codeguru.com/forum/showthread.php?t=445835
-
February 12th, 2008, 12:16 PM
#3
Re: Problem understanding fcntl and retaled issues.
Thanks.
So file descriptor is kind of a Handler?
I kinda got the idea, maybe i'll understand better if i see where it was used.
But the main reason why i asked this question was to figure out what fcntl does with operation F_DUPFD. Here is the description:
The fcntl() function shall perform the operations described below on open files. The fildes argument is a file descriptor.
The available values for cmd are defined in <fcntl.h> and are as follows:
F_DUPFD
Return a new file descriptor which shall be the lowest numbered available (that is, not already open) file descriptor greater than or equal to the third argument, arg, taken as an integer of type int. The new file descriptor shall refer to the same open file description as the original file descriptor, and shall share any locks. The FD_CLOEXEC flag associated with the new file descriptor shall be cleared to keep the file open across calls to one of the exec functions.
-
February 12th, 2008, 04:37 PM
#4
Re: Problem understanding fcntl and retaled issues.
Have no experience of fcntl but here's what I understand from reading.
Calling fcntl with F_DUPFD returns a mirror file descriptor with exactly the same attributes as the one provided in the call exept for close-on-exec flag that should be cleared.
This means that any file operations made on the new/original file descriptor will be relected into the original/new file descriptor just as if they where the same. As mentioned, the new file descriptor has the close-on-exec flag cleared no matter what state the original file descriptor had.
The reason for this I guess is that it should be possible for one executable to pass over the continuation of a job to another executable.
Or with X1 & X2 as two different executable files:
- X1 opens a file with close-on-exec flag set
- X1 does some preparing work
- X1 calls fcntl to duplicate the file descriptor
- X1 uses exec to start X2 passing the duplicate file descriptor as a param
- X1 dies and original file descriptor is automatically closed
- X2 continues to operate on file from the exact point as X1 finished
- X2 closes the file descriptor and exits
This could just as well have been acheived by having X2 source code compiled and linked into X1 but by doing it the fcntl way X2 can be reused without the need of duplicating the X2 source code into every other Xn source code. Also, if a bug is found in X2 there is only one executable to update and distribute.
-
February 13th, 2008, 01:08 AM
#5
Re: Problem understanding fcntl and retaled issues.
Thanks for the reply. that gave me some perspective of what the expression is doing. But i have to look more deeply into it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|