Create new processes and expect output

梦想的初衷 提交于 2021-02-08 03:49:37

问题


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;
}

I would ideally like the output to come out as the following:

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?


回答1:


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;
}

Result:

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



回答2:


You are not kill-ling anything. There are a lot of compiler errors, that you managed to dodge, so I added some header files, and left the errors in kill() and wait(). Also consider catching the return codes and paying attention to them. With correct headers and/or error trapping you would have seen your problems earlier. Try a compile and see why things were not doing what you wanted. Fix the errors and things will improve for you.

#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;
}


来源:https://stackoverflow.com/questions/9593283/create-new-processes-and-expect-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!