后台 execvp:如何正确执行?

Background execvp : how to do it properly?

像许多其他人一样,我正在尝试模拟 shell。我已经在来自用户的字符串上正确使用 execvp 。解析字符串并生成一个字符串数组(每个单词都有其数组,在 space 字符处拆分),包括最后的 NULL

当我发现用户输入的最后一个词是&时,我设置了一个标志来通知我的shell该命令将在后台执行,同时让用户立即输入另一个命令。 "background-executed" 命令看到其 & 被传递给 execvp.

的字符串数组中的 NULL 字符替换

实际上,我一直在尝试在后台使用 pthread 到 运行 进程,但它的行为有点奇怪:命令传递给 execvp 通过线程的功能要求我在发送命令后按两次ENTER

这是我简化的 main 函数,用于模拟 shell:

int main (void) {

    fprintf (stdout, "%% ");

    bool running = true;

    while(running) {

        /* Ask for an instruction and parses it. */
        char** args = query_and_split_input();

        /* Executing the commands. */
        if (args == NULL) {  // error while reading input
            running = false;
        } else {
            printf("shell processing new command\n");

            int count = count_words(args);
            split_line* line = form_split_line(args, count);
            Expression* ast = parse_line(line, 0, line->size - 1);

            if(line->thread_flag) {
                pthread_t cmd_thr;

                /* Setting up the content of the thread. */
                thread_data_t       thr_data;
                thr_data.ast        = *ast;
                thr_data.line       = *line;

                /* Executing the thread. */
                int thr_err;
                if ((thr_err = pthread_create(&cmd_thr, NULL, thr_func, &thr_data))) {
                    fprintf(stderr, "error: pthread_create, rc: %d\n", thr_err);
                    return EXIT_FAILURE;
                }
                printf("thread has been created.\n");

            } else {
                run_shell(args);
            }
            free(line);

            printf("done running shell on one command\n");
        }
    }

    /* We're all done here. See you! */
    printf("Bye!\n");
    exit (0);
}

这是我线程的函数:

void *thr_func(void *arg) {
    thread_data_t *data = (thread_data_t *)arg;

    data->line.content[data->line.size-1] = NULL;  // to replace the trailing '&' from the command
    run_shell(data->line.content);

    printf("thread should have ran the command\n");
    pthread_exit(NULL);
}

以及 运行 命令的实际行:

void run_shell(char** args) {

    /* Forking. */
    int status;
    pid_t    pid; /* Right here, the created THREAD somehow awaits a second 'ENTER' before going on and executing the next instruction that forks the process. This is the subject of my first question. */
    pid = fork();

    if (pid < 0) {
        fprintf(stderr, "fork failed");

    } else if (pid == 0) {  // child
        printf("Child executing the command.\n");

        /* Executing the commands. */
        execvp(args[0], args);

        /* Child process failed. */
        printf("execvp didn't finish properly: running exit on child process\n");
        exit(-1);


    } else {  // back in parent
        waitpid(-1, &status, 0);  // wait for child to finish

        if (WIFEXITED(status)) { printf("OK: Child exited with exit status %d.\n", WEXITSTATUS(status)); }
        else { printf("ERROR: Child has not terminated correctly. Status is: %d\n", status); }

        free(args);
        printf("Terminating parent of the child.\n");
    }
}

所以基本上,举个例子,run_shell(args) 收到的是 ["echo","bob","is","great",NULL](在顺序执行的情况下)或 ["echo","bob","is","great",NULL,NULL](在要执行的命令的情况下)在后台执行)。

我留下了 printf 痕迹,因为它可能有助于您理解执行流程。

如果我输入 echo bob is great,输出(printf traces)是:

shell processing new command
Child executing the command.
bob is great
OK: Child exited with exit status 0.
Terminating parent of the child.
done running shell on one command

但是,如果我输入echo bob is great &,输出是:

shell processing new command
thread has been created.
done running shell on one command

然后我实际上需要再次按 ENTER 以获得以下输出:

Child executing the command.
bob is great
OK: Child exited with exit status 0.
Terminating parent of the child.
thread should have ran the command

(在最后一次执行时,我还得到了查询和解析用户输入的函数的踪迹,但这似乎无关紧要,所以我抽象了整个部分。)

所以我的问题是:

  1. 创建的线程如何在 运行 宁 execvp 之前等待第二个 ENTER? (thr_func 停止执行 run_shell 并等待 pid = fork(); 指令之前的第二个 ENTER
  2. 我是否有解决手头问题的正确方法? (试图在后台执行 shell 命令。)

不能用线程来模拟进程。好吧,严格来说你可以,但这样做没有用。问题是属于一个进程的所有线程共享同一个虚拟地址space。没有理由创建一个线程,因为您最终需要 fork() 来创建一个新进程(您将需要它,原因如下所述),那么如果其中一个将停止,为什么要创建两个执行线程一直在等待子流程完成。此架构没有用。

历史上需要 fork() 系统调用来创建一个新进程(具有不同的虚拟内存映射)以允许执行新程序。在调用exec(2)系统调用之前需要创建一个新的完整进程,因为进程地址space会被新程序的文本和数据段覆盖。如果您在一个线程中执行此操作,您将覆盖整个进程地址 space(这是 shell)并终止您可以代表该进程拥有的所有线程 运行 .要遵循的模式是(伪代码):

/* create pipes for redirection here, before fork()ing, so they are available
 * in the parent process and the child process */
int fds[2];
if (pipe(fds) < 0) { /* error */
    ... /* do error treatment */
}
pid_t child_pid = fork();
switch(child_pid) {
case -1: /* fork failed for some reason, no subprocess created */
    ...
    break;
case 0: /* this code is executed in the childd process, do redirections
         * here on pipes acquired ***before*** the fork() call */
        if (dup2(0 /* or 1, or 2... */, fds[0 /* or 1, or 2... */]) < 0) { /* error */
            ... /* do error management, considering you are in a different process now */
        }
        execvpe(argc, argv, envp);
        ... /* do error management, as execvpe failed (exec* is non-returning if ok) */
        break; /* or exit(2) or whatever */ 
    default: /* we are the parent, use the return value to track the child */
        save_child_pid(child_pid);
        ... /* close the unused file descriptors */
        close(fds[1 /* or 0, or 2, ... */]);
        ... /* more bookkeeping */
        /* next depends on if you have to wait for the child or not */
        wait*(...);  /* wait has several flavours */
} /* switch */

Exec 和 fork 系统调用分开有两个原因:

  • 您需要能够在两次调用之间进行管理,以在 exec() 之前的 child 中执行实际重定向。
  • 曾经有一段时间 unix 没有多任务或受保护,exec 调用只是将系统中的所有内存替换为要执行的新程序(包括内核代码,以应对未受保护的系统可能被正在执行的程序损坏)这在旧操作系统中很常见,我在 CP/M 或 TRS-DOS 等系统上见过它。 unix 中的实现几乎保留了 exec() 调用的所有语义,并仅添加了 fork() 不可用的功能。这很好,因为它允许 parent 和 child 进程在管道时间到来时进行必要的簿记。

只有当您需要不同的线程与每个 child 进行通信时,您才可能使用不同的线程来完成任务。但是认为一个线程与 parent 共享所有虚拟 space (如果我们可以讨论线程之间的 parent/child 关系)并且如果您执行 exec 调用,您将获得该虚拟space 覆盖整个进程(那里的所有线程)