fork.c 619 B

1234567891011121314151617181920212223242526272829
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include "test_helpers.h"
  5. void prepare() {
  6. puts("Hello from prepare");
  7. }
  8. void parent() {
  9. // Make sure we print in the right order and also don't exit
  10. // before the fork does.
  11. int us_status = usleep(1000);
  12. ERROR_IF(usleep, us_status, == -1);
  13. UNEXP_IF(usleep, us_status, != 0);
  14. puts("Hello from parent");
  15. }
  16. void child() {
  17. puts("Hello from child");
  18. }
  19. int main(void) {
  20. int status = pthread_atfork(prepare, parent, child);
  21. ERROR_IF(pthread_atfork, status, == -1);
  22. int pid = fork();
  23. ERROR_IF(fork, pid, == -1);
  24. }