When fork()'ing file descriptors are copied between the two new processes and will cause concurrent operations to occur on the same file. 

Non-Compliant Code Example

char c;

int pid;


int fd = open(filename,O_RDWR,0);

read(fd,&c,1);

pid = fork();

if(pid == 0){/*child*/      read(fd,&c,1);      printf("child:%c\n",c);  }else{ /*parent*/      read(fd,&c,1);      printf("parent:%c\n",c);  }

This code's output cannot reliably be determined, and therefore should not be used.  Instead, file descriptors should not be passed through a fork() call and should be closed beforehand to prevent this error.

complient code example:

char c;

int pid;


int fd = open(filename,O_RDWR,0);

read(fd,&c,1);

close(fd);


pid = fork();

if(pid == 0){/*child*/      fd = open(filename,O_RDONLY,0);      read(fd,&c,1);      read(fd,&c,1);      printf("child:%c\n",c);  }else{ /*parent*/      fd = open(filename,O_RDWR,0);      read(fd,&c,1);      read(fd,&c,1);      printf("parent:%c\n",c);  }