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

标签 c exec

我正在尝试 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/

相关文章:

python - python 中的 Pickle 和 exec

c - 为什么 argv 以空指针结束?

c++ - 搜索算法以查找列表中的 k 个最小值

c - Linux:给定一个ID,检查它的PID或TID是否在C中

c - 如何写入执行进程的标准输入?

c - 在 C 中使用 execve() 调用 "pbmtextps"失败?

c# - 将类从 C#(AVL 树节点)转换为 C

c - 使用 csv 文件的输入填充结构体数组

c - 在不知道边界的情况下从文本文件读取二维数组?

c - 在 C 中使用 execvp 执行单独的命令