通过父级读取数据并使用c中的管道将其发送给子级

时间:2016-09-16 18:20:49

标签: c pipe fork

我正在尝试使用管道将数据从父级传递给子级。但是在子进程执行之前,父进程正在关闭。

我提供的代码请告诉我出错的地方。

    int main(void)
   {   
    int     fd[2], nbytes;
    pid_t   childpid;
    char    string[50];
    char    readbuffer[100];
    FILE *fp;
    pipe(fd);
    char var[100];

    if((childpid = fork()) == -1)
    {
            perror("fork");
            exit(1);
    }

    if(childpid != 0)
    {
    printf("PID of parent %u\n",getpid());                
    /* Child process closes up input side of pipe */
            close(fd[0]);
    fp = fopen("test.txt","r");     

    //scanning from file
    while(fscanf(fp,"%s\n",string)!=EOF)
    {
        strcat(var,string);
    }

    //printf("%s",string);
            /* Send "string" through the output side of pipe */
            write(fd[1], var, (strlen(var)+1));
            close(fd[1]);
           // exit(0);
    }
    else
    {
            printf("PID of child %u\n",getppid());
            /* Parent process closes up output side of pipe */

    close(fd[1]);

    //open a file
    //fp = fopen("test.txt","r");       

    //scanning from file
    //fscanf(fp,"%s\n",string);
    //printf("%s",string);
    //printinf from file
    //fprintf(fp,string);
            /* Read in a string from the pipe */
            nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
            printf("Received string: %s", readbuffer);
            //exit(0);
            close(fd[0]);
    } 
    return(0);
    }

我得到的输出是这样的:

父7432的PID

xyz @ ubuntu:〜/ Desktop $ PID of child 4686

收到字符串:Canyoucanacan。

我不希望父进程在子进程执行结束之前关闭。

1 个答案:

答案 0 :(得分:1)

在家长中,您必须等待孩子完成waitpid [或等效]。

因此,if/else的父部分的最后一行应该是:

waitpid(childpid,NULL,0);

以下是显示展示位置的更新代码:

int
main(void)
{
    int fd[2],
     nbytes;
    pid_t childpid;
    char string[50];
    char readbuffer[100];
    FILE *fp;

    pipe(fd);
    char var[100];

    if ((childpid = fork()) == -1) {
        perror("fork");
        exit(1);
    }

    if (childpid != 0) {
        printf("PID of parent %u\n", getpid());
        /* Child process closes up input side of pipe */
        close(fd[0]);
        fp = fopen("test.txt", "r");

        // scanning from file
        while (fscanf(fp, "%s\n", string) != EOF) {
            strcat(var, string);
        }

        // printf("%s",string);
        /* Send "string" through the output side of pipe */
        write(fd[1], var, (strlen(var) + 1));
        close(fd[1]);
        // exit(0);

#if 1
        waitpid(childpid,NULL,0);
#endif
    }

    else {
        printf("PID of child %u\n", getppid());
        /* Parent process closes up output side of pipe */

        close(fd[1]);

        // open a file
        // fp = fopen("test.txt","r");

        // scanning from file
        // fscanf(fp,"%s\n",string);
        // printf("%s",string);
        // printinf from file
        // fprintf(fp,string);
        /* Read in a string from the pipe */
        nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
        printf("Received string: %s", readbuffer);
        // exit(0);
        close(fd[0]);
    }

    return (0);
}