创建新流程并期望输出

时间:2012-03-06 22:57:36

标签: c multithreading parent-child

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

#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.

但到目前为止,它输出:

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

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

2 个答案:

答案 0 :(得分:0)

你没有杀死任何东西。有很多编译器错误,你设法躲闪,所以 我添加了一些头文件,并将错误留在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 %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;
}

答案 1 :(得分:0)

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

#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.