Skip to content

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;
}

Not explored

Read the assumption first. Then find out whether the scheduler agrees with it.

Everything you do here stays in this browser.Part of liter8.sh