【问题标题】:read data through parent and send it to child using pipe in c通过父级读取数据并使用 c 中的管道将其发送给子级
【发布时间】:2016-09-16 18:20:49
【问题描述】:

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

我正在提供代码,请告诉我哪里出错了。

    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$ 子 4686 的 PID

收到的字符串:Canyoucanacan。

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

【问题讨论】:

    标签: c pipe fork


    【解决方案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);
    }
    

    【讨论】:

    • 嗨,克雷格,感谢您的及时回复……它对我有用。
    • 不客气。如果您想查看更高级的管道用法(即,孩子从输入管道读取并写入输出管道),请在此处查看我最近的回答:stackoverflow.com/questions/39497736/… 这是关于创建一个类似 shell 的程序管道,因此确切的子操作与您的不同,但它可能对未来有用。
    • 感谢您的回复克雷格。一定会看看的
    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 2021-06-19
    • 2017-05-04
    相关资源
    最近更新 更多