onefork.c 541 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * onefork - simple test of fork
  3. *
  4. * relies only on fork, console write, and _exit
  5. *
  6. * parent prints "P", child prints "C", both exit
  7. *
  8. */
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. int
  15. main(int argc, char *argv[])
  16. {
  17. (void)argc;
  18. (void)argv;
  19. pid_t pid;
  20. pid = fork();
  21. if (pid < 0) {
  22. warn("fork");
  23. }
  24. else if (pid == 0) {
  25. /* child */
  26. putchar('C');
  27. putchar('\n');
  28. }
  29. else {
  30. /* parent */
  31. putchar('P');
  32. putchar('\n');
  33. }
  34. return(0);
  35. }