1 #include <sys/types.h> 2 #include <sys/wait.h> 3 #include <assert.h> 4 #if defined(TEST_CLONE) 5 #include <sched.h> 6 #endif 7 #include <stdint.h> 8 #include <stdio.h> 9 #include <unistd.h> 10 11 int g_val = 0; 12 13 void parent_func() { 14 g_val = 1; 15 printf("function run in parent\n"); 16 } 17 18 int child_func(void *unused) { 19 // we need to avoid memory modifications for vfork(), yet we want 20 // to be able to test watchpoints, so do the next best thing 21 // and restore the original value 22 g_val = 2; 23 g_val = 0; 24 return 0; 25 } 26 27 int main() { 28 alignas(uintmax_t) char stack[4096]; 29 30 #if defined(TEST_CLONE) 31 pid_t pid = clone(child_func, &stack[sizeof(stack)], 0, NULL); 32 #elif defined(TEST_FORK) 33 pid_t pid = TEST_FORK(); 34 if (pid == 0) 35 _exit(child_func(NULL)); 36 #endif 37 assert(pid != -1); 38 39 parent_func(); 40 int status, wait_flags = 0; 41 #if defined(TEST_CLONE) 42 wait_flags = __WALL; 43 #endif 44 pid_t waited = waitpid(pid, &status, wait_flags); 45 assert(waited == pid); 46 assert(WIFEXITED(status)); 47 printf("child exited: %d\n", WEXITSTATUS(status)); 48 49 return 0; 50 } 51