创建新流程并期待输出 [英] Create new processes and expect output

查看:58
本文介绍了创建新流程并期待输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,最终将用于让一个子进程通过管道将随机生成的字符发送到另一个子进程以转换为大写值和输出,但在我到达之前,我正在尝试创建子进程并产生一些预期的输出.我编写了以下应用程序:

I am writing a program that will eventually be used to have one child process send randomly generated characters through a pipe to another child process to convert to uppercase values and output, but before I get that far, I am trying to create the child processes and make some expected output. I have written the following application:

#include <stdio.h> /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h> /* fork */
#include <stdlib.h> /* _exit */
#include <errno.h> /* errno */

int main()
{
    pid_t writer;
    pid_t reader;

    writer = fork();
    if (writer == -1)
    {
        fprintf(stderr, "can't fork, error %d\n", errno);
        exit(1);
    }

    if (writer == 0)
    {
        printf("Writer process created.\n");
        reader = fork();
        if (reader == -1)
        {
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(1);
        }

        if (reader == 0)
        {
            printf("Reader process created.\n");
            kill(reader);
            printf("Reader was successfully murdered.\n");
        }
        kill(writer);
    }
    wait();
    printf("Writer killed.\n");
    return 0;
}

我希望输出如下:

Writer process created.
Reader process created.
Reader was successfully murdered.
Writer killed.

但到目前为止,它输出:

But as of this point, it outputs:

Writer process created.
Reader process created.
Reader was successfully murdered.
Writer killed.
Writer killed.
Writer killed.

就好像它从创建父进程和两个子进程的那一刻开始经历了整个代码迭代,这也让我相信它们没有在适当的时候被杀死我希望他们这样做.有人能指出我正确的方向吗?

It is as if it is going through the entire iteration of the code for the parent process, and two child processes from the point that they are created, which also leads me to believe that they are not being killed at the appropriate time that I want them to. Could someone point me in the right direction?

推荐答案

这是带有管道的完整工作示例(它们还同步进程,因此您不必终止它们).请记住,阅读器的阅读过程非常低效(差一个字符 - 我将其作为练习留给您:))

Here is your full working example with pipes (they also synchronize processes, so you do not have to kill them). Bear in mind that reader process is reading very inefficiently (by one character - I'll leave it as an exercise to you :))

#include <stdio.h> /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h> /* fork */
#include <stdlib.h> /* _exit */
#include <errno.h> /* errno */
#include <string.h> /* strlen */

int main()
{
    pid_t writer;
    pid_t reader;
    char buf;
    int pipefd[2] = {-1,-1};
    const char* pcszTextToChild = "Text sent to child";

    writer = fork();
    if (writer == -1)
    {
        fprintf(stderr, "can't fork, error %d\n", errno);
        exit(1);
    }

    if (writer == 0)
    {/* writer process */
        printf("Writer process created.\n");
        if(pipe(pipefd) == -1)
        {
           perror("pipe");
           exit(EXIT_FAILURE);
        }
        reader = fork();
        if (reader == -1)
        {
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(1);
        }

        if (reader == 0)
        {/* reader process */
            close(pipefd[1]);  /* Close unused write end */
            printf("Reader process created.\n\tReading: ");
            fflush(stdout);
            while (read(pipefd[0], &buf, 1) > 0)
                write(STDOUT_FILENO, &buf, 1);
            write(STDOUT_FILENO, "\n", 1);
            close(pipefd[0]);
            printf("Exiting reader process.\n");
            exit(EXIT_SUCCESS);
        }
        else
        {/* writer process */
            close(pipefd[0]);          /* Close unused read end */
            write(pipefd[1], pcszTextToChild, strlen(pcszTextToChild));
            close(pipefd[1]);          /* Reader will see EOF */
            wait(NULL);                /* Wait for child */
            printf("Exiting writer process.\n");
            exit(EXIT_SUCCESS);
        }
    }
    wait();
    return 0;
}

结果:

Writer process created.
Reader process created.
    Reading: Text sent to child
Exiting reader process.
Exiting writer process.

这篇关于创建新流程并期待输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆