Я уверен, что мне не хватает чего-то базового, но я пишу программу, в которой fork () использует несколько дочерних процессов, каждый из которых создает несколько потоков. Кажется, что вызов pthread_create никогда не работает из дочернего процесса. Вот пример кода, чтобы объяснить, что я имею в виду:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
// Listen for a request from user proc i
void *wait_for_req(void *cvoid) {
int req;
int read_ret;
printf("new thread started\n");
pthread_exit(NULL);
}
void spawn_read_threads(int proc_num, int n) {
int i;
printf("About to spawn %d read threads\n", n);
for (i=0; i<n; i++) {
pthread_t *t;
printf("spawning new thread\n");
int create_result = pthread_create(t, NULL, wait_for_req, NULL);
printf("_create returned %d\n", create_result);
pthread_join(*t, NULL);
}
}
int main() {
if (!fork())
spawn_read_threads(0, 1);
}
Вывод этой программы
About to spawn 1 read threads
spawning new thread
Но если я закомментирую, если закомментирую if (!fork())
:
About to spawn 1 read threads
spawning new thread
_create returned 0
new thread started
Так почему же в первом случае выполнение не проходит через create_result?
А если это полезно:
rob@ubuntu:/mnt/hgfs/Virtual Machines$ uname -a
Linux ubuntu 2.6.32-24-generic #42-Ubuntu SMP Fri Aug 20 14:24:04 UTC 2010 i686 GNU/Linux