diff --git a/tests/pthread/thread_fork.c b/tests/pthread/thread_fork.c new file mode 100644 index 0000000000..e4b0ee9c2e --- /dev/null +++ b/tests/pthread/thread_fork.c @@ -0,0 +1,51 @@ +/// test fork inside thread + +#include +#include +#include +#include +#include + +#include "test_helpers.h" + +#define NUM_THREADS 4 +#define ITERATIONS 3 + + +void* thread_func(void* arg) { + long tid = (long)arg; + + for (int i = 0; i < ITERATIONS; i++) { + printf("thread %ld loop %i\n", tid, i); + + pid_t pid = fork(); + ERROR_IF(fork, pid, < 0); + + if (pid == 0) { + _exit(0); + } else { + waitpid(pid, NULL, 0); + } + + usleep(1000); + } + + return NULL; +} + +int main() { + pthread_t threads[NUM_THREADS]; + int status; + + for (long i = 0; i < NUM_THREADS; i++) { + status = pthread_create(&threads[i], NULL, thread_func, (void*)i); + ERROR_IF(pthread_create, status, != 0); + } + + printf("joining threads\n"); + for (int i = 0; i < NUM_THREADS; i++) { + pthread_join(threads[i], NULL); + } + + return 0; +}