gpt4 book ai didi

c - 为什么这个 execlp() 函数没有执行?

转载 作者:行者123 更新时间:2023-11-30 16:11:39 25 4
gpt4 key购买 nike

我正在尝试 fork 多个子进程,然后对每个子进程使用 execlp() 来执行另一组代码。 execlp() 似乎没有执行。

我尝试过 execl() 和 execlp(),但不知道我写错了什么。

 // create child procs that use execlp()
for (int i = 0; i < num; i++){
if ((pids[i] = fork()) < 0){
perror("fork");
abort();
} else if (pids[i] == 0){
// do child work here
execlp("./fileWriter", "./fileWriter", num_threads, NULL);
printf("got here in child proc\n");
exit(0);
}
}

我期望它执行一个单独的文件,现在我刚刚在其中编写了一条打印语句,以便我知道另一个文件何时实际运行。相反,我得到的是“在子进程中到达这里”,我添加了它,这样我就知道子进程何时跳过了 execlp() 命令。

最佳答案

听起来您大部分都在那里,但我会给您我正在运行的版本:

char cNum[20];
// create child procs that use execlp()
for (ii = 0; ii < num; ii++)
{
sprintf(cNum,"%d", num_threads);
if ((pids[ii] = fork()) < 0){
perror("fork");
abort();
} else if (pids[ii] == 0){
// do child work here
ret=execlp("./fileWriter", "./fileWriter", cNum, (char *) NULL);
printf("got here in child proc: %d\n",ret);
perror("execlp");
exit(0);
}
}

execlp 必须是一个字符串(您已经得到了该部分),如果您检查错误返回以查看错误来自何处,它会“很有帮助”。

关于c - 为什么这个 execlp() 函数没有执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58566586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com