fork.m 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // TEST_CONFIG
  2. #include "test.h"
  3. void *flushthread(void *arg __unused)
  4. {
  5. while (1) {
  6. _objc_flush_caches(nil);
  7. }
  8. }
  9. int main()
  10. {
  11. pthread_t th;
  12. pthread_create(&th, nil, &flushthread, nil);
  13. alarm(120);
  14. [NSObject self];
  15. [NSObject self];
  16. int max = is_guardmalloc() ? 10: 100;
  17. for (int i = 0; i < max; i++) {
  18. pid_t child;
  19. switch ((child = fork())) {
  20. case -1:
  21. abort();
  22. case 0:
  23. // child
  24. alarm(10);
  25. [NSObject self];
  26. _exit(0);
  27. default: {
  28. // parent
  29. int result = 0;
  30. while (waitpid(child, &result, 0) < 0) {
  31. if (errno != EINTR) {
  32. fail("waitpid failed (errno %d %s)",
  33. errno, strerror(errno));
  34. }
  35. }
  36. if (!WIFEXITED(result)) {
  37. fail("child crashed (waitpid result %d)", result);
  38. }
  39. [NSObject self];
  40. break;
  41. }
  42. }
  43. }
  44. succeed(__FILE__ " parent");
  45. }