Race · 5 min
Two children, one file, no order
The assumption
The children write in the order they were forked.
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
int fd = open("/work/log", O_WRONLY|O_CREAT|O_APPEND, 0644);
pid_t a = fork();
if (a == 0) { write(fd, "a", 1); _exit(0); }
pid_t b = fork();
if (b == 0) { write(fd, "b", 1); _exit(0); }
int status;
waitpid(a, &status, 0);
waitpid(b, &status, 0);
close(fd);
return 0;
}