c - 为什么我的代码退出时退出代码为 : 0 even though the loop condition is still valid?

标签 c fork execvp unistd.h

我正在尝试将 forkexecvp 结合使用来同时运行两个 shell 命令。我有两个问题,当我输入 mkdirfolder1&mkdirfolder2 时,它会创建一个名为 folder1 的文件夹和另一个名为 folder2? 的文件夹(问号包含在文件夹名称中)。另一个问题是执行这两个命令后代码退出。

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define MAXLINE 80 /* The maximum length command */

int main(void) {
    char *args [MAXLINE / 2 + 1]; /* command line arguments */
    char *line = (char *) malloc((MAXLINE + 1) * sizeof (char));
    char *firstCommand = (char *) malloc((MAXLINE + 1) * sizeof (char));
    char *secondCommand = (char *) malloc((MAXLINE + 1) * sizeof (char));
    int shouldrun = 1; /* flag to determine when to exit program */
    pid_t pid;
    while (shouldrun) {
        printf("osh>");
        fflush(stdout);
        fgets(line, MAXLINE, stdin);
        if (strncmp(line, "exit", 4) == 0) {
            shouldrun = 0;
        } else {
            firstCommand = strsep(&line, "&");
            secondCommand = strsep(&line, "&");
            pid = fork();
            if (pid == 0) {
                // child
                if (secondCommand != NULL) {
                    char *token;
                    int n = 0;
                    do {
                        token = strsep(&secondCommand, " ");
                        args[n] = token;
                        n++;
                    } while (token != NULL);
                    execvp(args[0], args);
                }
            } else {
                // parent
                char *token;
                int n = 0;
                do {
                    token = strsep(&firstCommand, " ");
                    args[n] = token;
                    n++;
                } while (token != NULL);
                execvp(args[0], args);
            }
        }
    }
    return 0;
}
<小时/>

更新 1:

我试图遵循凯文的回答。我正在尝试同时执行多个进程,例如ps&ls&谁&日期。我尝试了一种递归方法,它给了我相同的行为。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define MAXLINE 80 /* The maximum length command */

void execute(char *command) {
    char *args [MAXLINE / 2 + 1]; /* command line arguments */
    char *parentCommand = strsep(&command, "&");
    pid_t pid = fork();;
    if (pid == 0) {
        // child
        if (command != NULL) {
            execute(command);
        }
    } else {
        // parent
        char *token;
        int n = 0;
        do {
            token = strsep(&parentCommand, " ");
            args[n] = token;
            n++;
        } while (token != NULL);
        execvp(args[0], args);
    }
}

int main(void) {
    char *line = (char *) malloc((MAXLINE + 1) * sizeof (char));
    int shouldrun = 1; /* flag to determine when to exit program */
    while (shouldrun) {
        printf("osh>");
        fflush(stdout);
        fgets(line, MAXLINE, stdin);
        if (strncmp(line, "exit", 4) == 0) {
            shouldrun = 0;
        } else {
            execute(line);
        }
    }
    return 0;
}

最佳答案

对于关于为什么它不循环的问题,您调用了一次 fork 但调用了 execvp 两次。如果成功,execvp 将不会返回。没有什么会返回来再次运行循环。您需要做的是为每个 execvp 调用一次 fork 。我建议您将 forkexecvp 调用移至单独的函数:

void run_command(const char* command) {
    /* I suggest you also check for errors here */
    pid_t pid = fork();
    if (pid == 0) {
        /* get args, call execvp */
    }
}

/* in your loop */
run_command(firstCommand);
run_command(secondCommand);

关于c - 为什么我的代码退出时退出代码为 : 0 even though the loop condition is still valid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101961/

相关文章:

c - 如何在c中打印程序编译位置的路径

编译输出代码

c - 没有嵌套的多个 child ?

c - 只需检查 c 中的状态过程

c - 在 main 中等待 execvp

c - 如何正确地 fork() 一个进程

c - 当静态链接 C 库时,是否将整个库添加到可执行文件中?

c - gcc 链接选项/LOCAL_CFLAGS -rdynamic 的作用是什么

c - forkN的作用是什么?

c - execvp 和参数类型 - ansi c