-1

我正在尝试使用forkwithexecvp同时运行两个 shell 命令。我有两个问题,当我输入时mkdir folder1&mkdir folder2,它会创建一个名为的文件夹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&who&date. 我尝试了一种递归方法,它给了我相同的行为。这是我的代码:

#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;
}
4

2 回答 2

1

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

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);
于 2017-07-14T12:22:46.970 回答
0

关于第一个问题,您需要\n从行中截断。关于第二个问题,您可以使用头文件中的system函数,stdlib.h它不会终止您的程序。

于 2017-07-15T04:20:32.633 回答