1 #include "test/jemalloc_test.h" 2 3 #ifndef _WIN32 4 #include <sys/wait.h> 5 #endif 6 7 TEST_BEGIN(test_fork) 8 { 9 #ifndef _WIN32 10 void *p; 11 pid_t pid; 12 13 p = malloc(1); 14 assert_ptr_not_null(p, "Unexpected malloc() failure"); 15 16 pid = fork(); 17 18 free(p); 19 20 p = malloc(64); 21 assert_ptr_not_null(p, "Unexpected malloc() failure"); 22 free(p); 23 24 if (pid == -1) { 25 /* Error. */ 26 test_fail("Unexpected fork() failure"); 27 } else if (pid == 0) { 28 /* Child. */ 29 _exit(0); 30 } else { 31 int status; 32 33 /* Parent. */ 34 while (true) { 35 if (waitpid(pid, &status, 0) == -1) 36 test_fail("Unexpected waitpid() failure"); 37 if (WIFSIGNALED(status)) { 38 test_fail("Unexpected child termination due to " 39 "signal %d", WTERMSIG(status)); 40 break; 41 } 42 if (WIFEXITED(status)) { 43 if (WEXITSTATUS(status) != 0) { 44 test_fail( 45 "Unexpected child exit value %d", 46 WEXITSTATUS(status)); 47 } 48 break; 49 } 50 } 51 } 52 #else 53 test_skip("fork(2) is irrelevant to Windows"); 54 #endif 55 } 56 TEST_END 57 58 int 59 main(void) 60 { 61 62 return (test( 63 test_fork)); 64 } 65