子进程在第一次迭代后不执行函数
Child process doesn't execute function after first iteration
程序设计
我的程序设计为创建了 6 个子进程。每个子进程将创建 3 个线程。因此,总共应该有 18 个线程。
当我创建一个子进程时,它调用一个名为 createThread 的函数并将该迭代的 childIndex 传递给它(即 0、1、2、3、4、5)。
函数执行时,应该:
- 将子 PID 存储到 struct childThreads 中的 pid 元素中。
- 进入for循环并创建线程3次并将其线程ID存储在同一结构的tid元素中
- 打印结果只是为了确保一切正常。
我的问题:
当子索引处于第一次迭代(即 0)时,输出符合预期。
但是,在下一次迭代到最后一次迭代时,函数会中途停止执行。
预期输出:
childThread[0].pid = 1111
childThread[0].tid[0] = 0
childThread[0].tid[1] = 1
childThread[0].tid[2] = 2
childThread[1].pid = 2222
childThread[1].tid[0] = 0
childThread[1].tid[1] = 1
childThread[1].tid[2] = 2
...
..
.
实际输出:
childThread[0].pid = 1111
childThread[0].tid[0] = 0
childThread[0].tid[1] = 1
childThread[0].tid[2] = 2
childThread[1].pid = 2222
childThread[2].pid = 3333
childThread[3].pid = 4444
childThread[4].pid = 5555
childThread[5].pid = 6666
我的代码
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
int numChild = 6;
int numThreadsPerChild = 3;
struct childAndThreads {
pid_t pid;
pthread_t *tids;
};
struct childAndThreads *childThread;
pid_t *pids;
void createThreads(int i) {
int childIndex = i;
childThread[childIndex].pid = getpid();
printf("childThread[%d].pid = %d\n", childIndex, childThread[childIndex].pid);
for(int i = 0; i < numThreadsPerChild; i++) {
childThread[childIndex].tids[i] = i;
}
for(int i = 0; i < numThreadsPerChild; i++) {
printf("childThread[%d].tid[%d] = %d\n", childIndex, i, i);
}
}
int main() {
pids = malloc(numChild * sizeof(pid_t));
childThread = malloc(numChild * sizeof(struct childAndThreads));
childThread->tids = malloc(numThreadsPerChild * sizeof(pthread_t));
for(int i = 0; i < numChild; i++) {
switch(pids[i] = fork()) {
case 0:
createThreads(i);
return 0;
case -1:
printf("Fork Error\n");
break;
default:
//I am a parent
break;
}
}
for(int i = 0; i < numChild; i++) {
wait(NULL);
}
}
childThread->tids = malloc(numThreadsPerChild * sizeof(pthread_t));
该行不正确。它只为第一个子进程分配 tids
。相反,它应该是:
for (int i = 0; i < numChild; i++) {
childThread[i].tids = malloc(numThreadsPerChild * sizeof(pthread_t));
}
程序设计
我的程序设计为创建了 6 个子进程。每个子进程将创建 3 个线程。因此,总共应该有 18 个线程。
当我创建一个子进程时,它调用一个名为 createThread 的函数并将该迭代的 childIndex 传递给它(即 0、1、2、3、4、5)。
函数执行时,应该:
- 将子 PID 存储到 struct childThreads 中的 pid 元素中。
- 进入for循环并创建线程3次并将其线程ID存储在同一结构的tid元素中
- 打印结果只是为了确保一切正常。
我的问题:
当子索引处于第一次迭代(即 0)时,输出符合预期。 但是,在下一次迭代到最后一次迭代时,函数会中途停止执行。
预期输出:
childThread[0].pid = 1111
childThread[0].tid[0] = 0
childThread[0].tid[1] = 1
childThread[0].tid[2] = 2
childThread[1].pid = 2222
childThread[1].tid[0] = 0
childThread[1].tid[1] = 1
childThread[1].tid[2] = 2
...
..
.
实际输出:
childThread[0].pid = 1111
childThread[0].tid[0] = 0
childThread[0].tid[1] = 1
childThread[0].tid[2] = 2
childThread[1].pid = 2222
childThread[2].pid = 3333
childThread[3].pid = 4444
childThread[4].pid = 5555
childThread[5].pid = 6666
我的代码
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
int numChild = 6;
int numThreadsPerChild = 3;
struct childAndThreads {
pid_t pid;
pthread_t *tids;
};
struct childAndThreads *childThread;
pid_t *pids;
void createThreads(int i) {
int childIndex = i;
childThread[childIndex].pid = getpid();
printf("childThread[%d].pid = %d\n", childIndex, childThread[childIndex].pid);
for(int i = 0; i < numThreadsPerChild; i++) {
childThread[childIndex].tids[i] = i;
}
for(int i = 0; i < numThreadsPerChild; i++) {
printf("childThread[%d].tid[%d] = %d\n", childIndex, i, i);
}
}
int main() {
pids = malloc(numChild * sizeof(pid_t));
childThread = malloc(numChild * sizeof(struct childAndThreads));
childThread->tids = malloc(numThreadsPerChild * sizeof(pthread_t));
for(int i = 0; i < numChild; i++) {
switch(pids[i] = fork()) {
case 0:
createThreads(i);
return 0;
case -1:
printf("Fork Error\n");
break;
default:
//I am a parent
break;
}
}
for(int i = 0; i < numChild; i++) {
wait(NULL);
}
}
childThread->tids = malloc(numThreadsPerChild * sizeof(pthread_t));
该行不正确。它只为第一个子进程分配 tids
。相反,它应该是:
for (int i = 0; i < numChild; i++) {
childThread[i].tids = malloc(numThreadsPerChild * sizeof(pthread_t));
}