forktest.c 766 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Test that fork fails gracefully.
  2. // Tiny executable so that the limit can be filling the proc table.
  3. #include "types.h"
  4. #include "stat.h"
  5. #include "user.h"
  6. #define N 1000
  7. void
  8. printf(int fd, char *s, ...)
  9. {
  10. write(fd, s, strlen(s));
  11. }
  12. void
  13. forktest(void)
  14. {
  15. int n, pid;
  16. printf(1, "fork test\n");
  17. for(n=0; n<N; n++){
  18. pid = fork();
  19. if(pid < 0)
  20. break;
  21. if(pid == 0)
  22. exit();
  23. }
  24. if(n == N){
  25. printf(1, "fork claimed to work N times!\n", N);
  26. exit();
  27. }
  28. for(; n > 0; n--){
  29. if(wait() < 0){
  30. printf(1, "wait stopped early\n");
  31. exit();
  32. }
  33. }
  34. if(wait() != -1){
  35. printf(1, "wait got too many\n");
  36. exit();
  37. }
  38. printf(1, "fork test OK\n");
  39. }
  40. int
  41. main(void)
  42. {
  43. forktest();
  44. exit();
  45. }