...
Non-Compliant Code Example
In this non compliant code example, the programmer incorrectly accesses the file descriptor after the call to fork(). Independent on what the programmer is trying to accomplish with this code, it is still incorrect because it contains a race condition.
| Code Block | ||
|---|---|---|
| ||
char c;
int pid;
int fd = open(filename,O_RDWR,0);
if (fd == -1) {
/* Handle error */
}
read(fd,&c,1);
printf("root process:%c\n",c);
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 possible error.
Complaint code example:
In this compliant code example, the programmer correctly closes the file descriptor before forking and then reopening the file descriptor in each child. In this example the programmer wanted to read the second character of the file twice, but they could have read any number of any character in the file deterministically.
| Code Block | ||
|---|---|---|
| ||
char c;
int pid;
int fd = open(filename,O_RDWR,0);
if (fd == -1) {
/* Handle error */
}
read(fd,&c,1);
printf("root process:%c\n",c);
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);
close(fd);
}else{ /*parent*/
fd = open(filename,O_RDWR,0);
read(fd,&c,1);
read(fd,&c,1);
printf("parent:%c\n",c);
close(fd);
}
|
...