Same file descriptor after fork() -


i'm trying understand means duplicating file descriptor after calling fork() , possible effects on contention.

in "the linux programming interface" 24.2.1 (p517):

when fork() performed, child receives duplicates of of parent's file descriptors. these duplicates made in manner of dup(), means corresponding descriptors in parent , child refer same open file description.

when run same code:

#include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <sys/wait.h>  int main(void) {     char* fl = "/tmp/test_fd";     int fd;     fd = open(fl, o_creat|o_trunc|o_wronly, 0666);     if(!fork()) {         printf("cfd=%d\n", fd);         _exit(0);     } else {         int status;         printf("ffd=%d\n", fd);         wait(&status);         close(fd);         unlink(fl);     } } 

i same file descriptor (number?) both processes: ffd=3 , cfd=3. when run code using dup():

#include <unistd.h> #include <fcntl.h> #include <stdio.h>  int main(void) {     char* fl = "/tmp/test_fd";     int cfd, ffd;     ffd = open(fl, o_creat|o_trunc|o_wronly, 0666);     cfd = dup(ffd);     printf("ffd=%d\n", ffd);     printf("cfd=%d\n", cfd);     close(ffd);     unlink(fl); } 

i different file descriptors: ffd=3 , cfd=4.

then, have following questions:

  1. what means fork() creates copy of parent's file descriptors?
  2. is there contention when 2 processes (father , child) perform operation fstat() concurrently on same file descriptor?
  3. and 2 processes performing fstat() concurrently 2 different file descriptors pointing same file?

when see phrase "duplicate file descriptor", need understand "create new file descriptor points same thing other 1 pointing to".

so when duplicate fd 3, fd 4. aren't same number, identify same object.

in case of fork, must remember meaning of file descriptor contained within process. lots of processes have valid fd 3, they're not referring same object. fork, have duplicate of fd 3 fd 3, in different process 3's aren't inherently identical; they're identical because fork made copy.

you can close fd , open different file in child process, , you'll still have 2 processes in fd 3 valid, aren't copies of same thing anymore. or have child dup fd 3 fd 4, close fd 3, , you'd have fd 3 in parent , fd 4 in child referring same object.

the same number in different process doesn't mean anything.

also note object referred file descriptor isn't file. there's in between file descriptor , file, , it's called open file description. both fork , dup cause open open file description shared. main consequence of sharing when current file position (set lseek or advanced read , write) changes, of related file descriptors affected, , when flags (like o_nonblock) changed, related file descriptors affected.

in contrast if open same file twice, 2 file descriptors refer same file, through different open file descriptions, have independent flags , seek position.

fstat being readonly operation, don't see kind of "contention" you're imagining it.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -