From fd616a970463478473c5789e603ed3563ec3b4ad Mon Sep 17 00:00:00 2001 From: Wildan M Date: Fri, 16 Jan 2026 13:21:51 +0700 Subject: [PATCH 1/2] Add mutex inside fork test --- tests/pthread/mutex_fork.c | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/pthread/mutex_fork.c diff --git a/tests/pthread/mutex_fork.c b/tests/pthread/mutex_fork.c new file mode 100644 index 0000000000..9c8317fd11 --- /dev/null +++ b/tests/pthread/mutex_fork.c @@ -0,0 +1,63 @@ +/// test mutex inside fork + +#include +#include +#include +#include +#include + +#include "test_helpers.h" + +#define NUM_THREADS 4 +#define ITERATIONS 3 + +pthread_mutex_t global_lock; + +void* thread_func(void* arg) { + long tid = (long)arg; + + for (int i = 0; i < ITERATIONS; i++) { + printf("thread %ld loop %i lock\n", tid, i); + + pthread_mutex_lock(&global_lock); + + pid_t pid = fork(); + + if (pid < 0) { + perror("Fork failed"); + pthread_mutex_unlock(&global_lock); + pthread_exit(NULL); + } + + if (pid == 0) { + _exit(0); + } else { + printf("thread %ld unlock\n", tid); + pthread_mutex_unlock(&global_lock); + + waitpid(pid, NULL, 0); + } + + usleep(1000); + } + + return NULL; +} + +int main() { + pthread_t threads[NUM_THREADS]; + int status; + pthread_mutex_init(&global_lock, NULL); + + for (long i = 0; i < NUM_THREADS; i++) { + status = pthread_create(&threads[i], NULL, thread_func, (void*)i); + ERROR_IF(pthread_create, status, != 0); + } + + for (int i = 0; i < NUM_THREADS; i++) { + pthread_join(threads[i], NULL); + } + + pthread_mutex_destroy(&global_lock); + return 0; +} From d33bf64e5f8477d583c372a07ea787451cf4f4cd Mon Sep 17 00:00:00 2001 From: Wildan M Date: Fri, 16 Jan 2026 13:26:16 +0700 Subject: [PATCH 2/2] Remove the mutex, just fork and threads --- tests/pthread/{mutex_fork.c => thread_fork.c} | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) rename tests/pthread/{mutex_fork.c => thread_fork.c} (61%) diff --git a/tests/pthread/mutex_fork.c b/tests/pthread/thread_fork.c similarity index 61% rename from tests/pthread/mutex_fork.c rename to tests/pthread/thread_fork.c index 9c8317fd11..e4b0ee9c2e 100644 --- a/tests/pthread/mutex_fork.c +++ b/tests/pthread/thread_fork.c @@ -1,4 +1,4 @@ -/// test mutex inside fork +/// test fork inside thread #include #include @@ -11,33 +11,22 @@ #define NUM_THREADS 4 #define ITERATIONS 3 -pthread_mutex_t global_lock; void* thread_func(void* arg) { long tid = (long)arg; for (int i = 0; i < ITERATIONS; i++) { - printf("thread %ld loop %i lock\n", tid, i); - - pthread_mutex_lock(&global_lock); - - pid_t pid = fork(); + printf("thread %ld loop %i\n", tid, i); - if (pid < 0) { - perror("Fork failed"); - pthread_mutex_unlock(&global_lock); - pthread_exit(NULL); - } + pid_t pid = fork(); + ERROR_IF(fork, pid, < 0); if (pid == 0) { _exit(0); } else { - printf("thread %ld unlock\n", tid); - pthread_mutex_unlock(&global_lock); - waitpid(pid, NULL, 0); } - + usleep(1000); } @@ -47,17 +36,16 @@ void* thread_func(void* arg) { int main() { pthread_t threads[NUM_THREADS]; int status; - pthread_mutex_init(&global_lock, NULL); 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); } - pthread_mutex_destroy(&global_lock); return 0; }