0

我正在尝试编写一个程序来执行和安排进程列表。我的 main.c 代码如下。运行时,我收到来自 perror 的错误,说没有这样的文件或目录。我猜这是因为我的 files.txt 中的文件/程序不是二进制可执行文件,但我不知道如何解决这个问题。files.txt 包含我要运行的程序列表。它们已经全部转换为二进制可执行文件。程序是一个数组,其中包含已被 readPrograms 函数读取的四个程序

int main(int argc, char ** argv) {
    pid_t pid[50];
    pid_t wpid;
    int i, j;
    int status = 0;
    char *newenvp[] = {NULL};
    char *newargv[] = {"./files.txt", NULL};

    printf("Before forking in the parent\n");
    int numProgs = readPrograms();

    for (i=0; i<numProgs; i++) {
        pid[i] = fork();
        if (pid[i] < 0) {
            perror("fork error");
            exit(EXIT_FAILURE);
        }
        else if (pid[i] == 0) {
            printf("Child process running\n");
            execve(programs[i], newargv, newenvp);
            perror("execve error");
            exit(EXIT_FAILURE);
        }
    }
    for (i=0; i<numProgs; i++) {
        wait(&status);
    }
    return 0;
}
char* programs[50];
int readPrograms();

文件.txt 下面

./first
./second
./third
./fourth

(我分别为所有这些文件做了“cc first.c -o first”)

4

1 回答 1

1

我怀疑错误出现在您未显示的代码中,readPrograms. 很可能您正在读取文本文件的行,fgets并且每个字符串的末尾都有一个换行符,而您的文件名中没有换行符。

于 2014-10-29T02:54:30.627 回答