c-创建新流程并期望输出



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

#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 %dn", errno);
        exit(1);
    }
    if (writer == 0)
    {
        printf("Writer process created.n");
        reader = fork();
        if (reader == -1)
        {
            fprintf(stderr, "can't fork, error %dn", 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.

但到目前为止,它输出:

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

就好像它正在经历父进程和两个子进程的整个代码迭代,从它们被创建的那一刻起,这也让我相信它们并没有在我希望的适当时间被杀死。有人能给我指明正确的方向吗?

这是您的完整的管道工作示例(它们还同步进程,因此您不必杀死它们)。请记住,读者的阅读过程是非常低效的(通过一个字符-我将把它留给你练习:)

#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 %dn", 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 %dn", errno);
            exit(1);
        }
        if (reader == 0)
        {/* reader process */
            close(pipefd[1]);  /* Close unused write end */
            printf("Reader process created.ntReading: ");
            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.

你没有杀死任何东西。有很多编译器错误,你设法避开了,所以我添加了一些头文件,并将错误留在kill()和wait()中。还要考虑捕捉返回代码并注意它们。如果有正确的标题和/或错误陷阱,你会更早地发现问题。试着编译一下,看看为什么事情没有按照你的意愿进行。纠正错误,事情就会有所改善。

#include <sys/types.h> /* pid_t */
#include <sys/wait.h>
#include <stdio.h> /* printf, stderr, fprintf */
#include <unistd.h> /* fork */
#include <stdlib.h> /* _exit */
#include <errno.h> /* errno */
#include <signal.h>
int main()
{
    pid_t writer;
    pid_t reader;
    writer = fork();
    if (writer == -1)
    {
        fprintf(stderr, "can't fork, error %dn", errno);
        exit(1);
    }
    if (writer == 0)
    {
        printf("Writer process created.n");
        reader = fork();
        if (reader == -1)
        {
            fprintf(stderr, "can't fork, error %dn", 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;
}

相关内容

  • 没有找到相关文章

最新更新