Cによるsleep sortの実装がないのもどうかと思ったので書いてみた

http://www.yuyak.com/1339 を見て、Cによるsleep sortの実装がないのもどうかと思ったので書いてみた。シンプルですな。

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char** argv)
{
  int values[] = { 1, 9, 5, 3 };
  int i;
  
  for (i = 0; i < sizeof(values) / sizeof(values[0]); i++)
    switch (fork()) {
    case -1:
      assert(0);
      break;
    case 0:
      /* child process */
      sleep(values[i]);
      printf("%d\n", values[i]);
      exit(0);
      break;
    default:
      break;
    }
  
  for (i = 0; i < sizeof(values) / sizeof(values[0]); i++)
    while (wait(NULL) <= 0)
      assert(errno == EINTR);
  
  return 0;
}