我正在尝试将变量传递给一个分叉的函数,大多数似乎都可以工作,但我需要给每个变量一个我试图传递的 argv[5] 之前未声明的 id。我的代码不断出现段错误,我不知道为什么。有任何想法吗?
主要的
char* arg_list_consumer[] = {
"./consume",
argv[1],
argv[2],
NULL
};
char* arg_list_producer[] = {
"./produce",
argv[1],
argv[2],
argv[3],
argv[4],
argv[5],
NULL
};
if (getppid()==1){
producerID=0;
}
if(producerID==0){
for(i=1; i<P; i++){
sprintf(arg_list_producer[5], "%d", i);
spawn ("./produce", arg_list_producer);
}
for(i=0; i<C; i++){
sprintf(arg_list_consumer[5], "%d", i);
spawn ("./consume", arg_list_consumer);
}
}
产卵
int spawn (char* program, char** arg_list)
{
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork();
if (child_pid != 0)
/* This is the parent process. */
return child_pid;
else {
/* Now execute PROGRAM, searching for it in the path. */
execvp (program, arg_list);
/* The execvp function returns only if an error occurs. */
fprintf (stderr, "an error occurred in execvp\n");
abort ();
}
}