#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

int main() {

  int fd, i, j;
  pid_t n;

  if ((fd = open("output_problem.txt", O_CREAT | O_APPEND | O_RDWR, 0666)) == -1) {
      perror("open");
      exit(-1);
  }

  for (i = 0; i < 10; i++) {
    n = fork();
    if (n < 0) {
      perror("fork");
      exit(-1);
    }
    if (n == 0) {
      char *output = malloc(2);
      for (j = 0; j < 10000; j++) {
        sprintf(output, "%d", i);
        output[1] = '\0';
        write(fd, output, strlen(output));
      }
      free(output);
      write(fd, "\n", 1);
      exit(0);
    }
  }

  for (i = 0; i < 10; i++) {
    wait(NULL);
  }

  close(fd);

  return 0;
}
