I have a little problem about parent/child processes in C: How can the child process access file descriptors that the parent process opened after the fork?
-
1Your question is a bit unclear. Does the parent process open the files, then fork, or fork then opens the files? – Mat Jan 20 '13 at 20:30
-
the parent process fork then opens the files, so how the child can use these files? – user1995143 Jan 20 '13 at 20:38
-
Anton's answer is correct. There's no portable way of doing that, you'll need OS-specific APIs. – Mat Jan 20 '13 at 20:39
5 Answers
Assuming the question is about parent process forking and then opening new file descriptors: The short answer is: it cannot.
There are platform-specific ways to pass file descriptors between processes (e.g. SCM_RIGHTS ancillary message sent with unix socket), but they don't depend on parent-child relationship of processes.

- 20,999
- 2
- 37
- 69
File descriptors are carried over a fork
system call, so the child process can use them at will. That's how IPC with pipes (see man 2 pipe
) is usually done.
If you need to access file descriptors opened after the fork, you can send them over a UNIX socket with sendmsg
. See How to use sendmsg() to send a file-descriptor via sockets between 2 processes?
After fork(), The child process gets its own copy of the parent's file descriptors.
Each of the child's file descriptors refers to the same open file description with the corresponding file descriptor of the parent.
Once they have their own copies of file descriptors, child/parent relationship does not matter. Its the same as two different processes accessing the same file with their own set of file descriptors. After which file locking and synchronisation may come into role.
[redited - Sorry misread the question, you can't as the processes do not share the same memory space. Best if you open the file descriptor before the fork or use some other IPC mechanism depending on what you are trying to achieve]
The child process will inherit the file descriptors of the parent and therefore can be used directly.
void example()
{
int fd = open("My file", O_WRONLY );
pid_t pid = fork();
if(pid == 0)
{
/* child */
char * child_msg = "Hi there from child\n";
write(fd, my_msg, sizeof(my_msg);
/* ... other stuff */
}
else
{
/* parent */
char * parent_msg = "Hi there from parent\n";
write(fd, my_msg, sizeof(my_msg);
/* ... other stuff */
}
}
Both messages will be written to "my file"

- 149
- 2
-
my problem is to use file descriptors creatad by the parent AFTER the fork, i know that file descriptors opened BEFORE the fork are inherited by the child. – user1995143 Jan 20 '13 at 20:42
-
Your question is unclear.
Anyway, if you are trying to use data in a child process, that uses parent, you can transfer data from parent to child. To simplify, just use anonymous pipes in POSIX int pipe(int anFD[2])
and then you can use write
and read
(of fread
and fwrite
functions). In parent process you have to use function popen
to open a file with write/read access.
Remember, that fork()
does a full copy (parent and child has shared pages and these pages are read only). If you are trying to write in a source page, kernel will check if child process is the one owner and then this source page will writable).

- 730,956
- 141
- 904
- 1,278

- 632
- 8
- 20
-
Welcome to Stack Overflow. Please note that questions and answers are expected to be written in semi-formal English without SMS-style contractions such as 'ur' and 'u'. I've updated this time; you should do it yourself next time. You've also got some issues; `popen()` is a mechanism for piping data to or from a _process_, not a _file_, and there's no obligation to use the function. You have to do some work (perhaps with `fdopen()`) to use `fwrite()` and `fread()` on a pipe. They use file streams, but `pipe()` returns file descriptors. – Jonathan Leffler Jan 20 '13 at 21:34
-
The parent and child process do indeed share pages after a `fork()`. Typically, the text (code) is readonly, but the data is marked COW — copy-on-write — so that it is copied only when one of the processes modifies the data. – Jonathan Leffler Jan 20 '13 at 21:40