如何将信号传递给子进程

时间:2012-05-20 10:59:27

标签: c shell io

标题可能有点令人困惑,所以让我解释一下。我正在尝试编写一个简单的shell来练习我的编程。我得到了一个命令,fork,exec循环工作。但是,当我在子进程仍在执行时按CTRL-C时,我的shell终止,而不是子进程(但子进程将继续运行)。这是主要功能:

int main()
{
    dynarray *args; /* pointer to a dynamic array */
    int bytes_read;
    size_t nbytes = 0;
    char *command;
    pid_t pid;
    printf("Enter command: ");
    while ((bytes_read = getline(&command, &nbytes, stdin)) != -1) {
        if (bytes_read == -1) {
            perror("getline");
            exit(EXIT_FAILURE);
        }
        pid = fork();
        if (pid == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        else if (pid == 0) { /* child process */
            args = newdynarray();
            char *arg = strtok(command, " \n");
            while (arg != NULL) {
                addstring(args, arg);
                arg = strtok(NULL, " \n");
            }
            if (args->nval == 0) {
                freedynarray(args);
                continue;
            }

            addstring(args, NULL);
            char *fullpath = find_executable(args->strings[0]);
            if (fullpath == NULL) {
                fprintf(stderr, "Couldn't find executable: %s\n", command);
                exit(EXIT_FAILURE);
            }
            if (execv(fullpath, args->strings) == -1) {
                perror("execv");
                exit(EXIT_FAILURE);
            }
        } else {
            int status;
            waitpid(pid, &status, 0);
        }
        printf("Enter command: ");
    } 
    return 0;
}

我没有包含其他部分,因为我认为它们不相关。如何让我的子进程捕获stdin的所有输入,直到它终止?

2 个答案:

答案 0 :(得分:1)

您可以在父进程中为SIGINT注册信号处理程序,然后使用kill(2)向子进程发送信号,该进程的PID应存储在某处。

答案 1 :(得分:0)

How can I make my child process catch all the input from stdin until it terminates?从stdin键生成的信号(例如控件C)将被发送到最后一个进程以使用stdin,因此除非你可以强迫你的孩子使用该路径,否则你无能为力。

相反,您需要在shell进程中创建一个信号处理程序来捕获SIGINT(和其他),并将信号(使用kill()函数)重新发送到您想要接收它的进程。

相关问题