#include // #include #include #include // void customFunction1(int n); void *customFunction1(void *n); void customFunction2(int n); void customFunction3(int n); // Call this function to get a backtrace. // void backtrace() { // unw_cursor_t cursor; // unw_context_t context; // // Initialize cursor to current frame for local unwinding. // unw_getcontext(&context); // unw_init_local(&cursor, &context); // // Unwind frames one by one, going up the frame stack. // while (unw_step(&cursor) > 0) { // unw_word_t offset, pc; // unw_get_reg(&cursor, UNW_REG_IP, &pc); // if (pc == 0) { // break; // } // printf("0x%lx:", pc); // char sym[256]; // if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) { // printf(" (%s+0x%lx)\n", sym, offset); // } else { // printf(" -- error: unable to obtain symbol name for this frame\n"); // } // } // } void *customFunction1(void *n) { int num = *((int *)n); if(num <= 0) { printf("End of recursion\n"); printf("pid: %d\n", getpid()); while (1) { sleep(1); } // never return, keep stack // return NULL; } else { printf("Calling customFunction2\n"); customFunction2(num-1); } return NULL; } void customFunction2(int n) { printf("Calling customFunction2\n"); if(n <= 0) { printf("End of recursion\n"); printf("pid: %d\n", getpid()); while (1) { sleep(1); } // never return, keep stack // return NULL; } customFunction3(n-1); } void customFunction3(int n) { printf("Calling customFunction3\n"); if(n <= 0) { printf("End of recursion\n"); printf("pid: %d\n", getpid()); while (1) { sleep(1); } // never return, keep stack // return NULL; } customFunction2(n-1); } int main() { int num1 = 4; int num2 = 5; int num3 = 6; pthread_t thread_id1, thread_id2, thread_id3; pthread_create(&thread_id1, NULL, customFunction1, &num1); pthread_create(&thread_id2, NULL, customFunction1, &num2); pthread_create(&thread_id3, NULL, customFunction1, &num3); pthread_join(thread_id1, NULL); pthread_join(thread_id2, NULL); pthread_join(thread_id3, NULL); return 0; }