如何等待直到数据在管的另一端部写入 [英] How to wait till data is written on the other end of pipe

查看:169
本文介绍了如何等待直到数据在管的另一端部写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在C应用程序
父子进程通过管道进行通信。
写管道之前,父进程执行另一个语句。在样品code,我已经使用睡眠(10),以延迟。
在子过程中,应阅读从管道中的数据。
但数据无法读取管在子进程读端。

I am developing an application in C. Parent and child process communicate through pipe. Before writing to pipe, parent process execute another statements. In sample code, i have used sleep(10) to make delay. In the child process, it should read the data from the pipe. But data is not read on the read end of pipe in child process.

int main()
{
    int pid;
    FILE *fp;
    fp = fopen("test.txt","w");
    char *buff;
    int fd[2];
    int count = 0 ;
    pipe(fd);
    pid = fork();
    if(pid == 0)
    {
        close(fd[1]);
        ioctl(fd[0], FIONREAD, &count);
        fprintf(fp,"Value of count: %d ",count);
        buff = malloc(count);
        fprintf(fp,"\n TIME before read: %s",__TIME__);
        read(fd[0], buff, count);
        fprintf(fp,"\nbuffer: %s\n TIME after read %s", buff, __TIME__);
    }
    else{
        close(fd[0]);
        sleep(10);    //delay caused by application specific code replaced with sleep
        write(fd[1],"THIS is it",10);
    }
    fclose(fp);
    return 0;
}

如何让子进程等到数据在另一端写的?

How to make child process wait till data is written on the other end?

推荐答案

您管阻塞模式打开,不管你做什么来改变这种状况,这是你打算什么可能性。

Your pipe is opened in blocking mode, and you do nothing to change that, which is likely what you intended.

但是,因为你做的第一件事就是请求数据等待管道的大小,然后盲目地跳进读取的字节数(这在所有的可能性会在那个code中的时间为零,因为父执行不写任何东西),你不阻止,而是刚刚离开,因为你要求什么。

However, since the first thing you do is request the size of data waiting on the pipe, then blindly jump into reading that many bytes (which in all likelihood will be zero at the time that code executes since the parent hasn't written anything yet) you don't block, and instead just leave because you requested nothing.

有许多方法可以做到这一点,包括一个选择环路。如果您愿意的的上一读,直到可用的数据,那么这样做的一个字节,并填写剩余的数据之后。

There are a number of ways to do this, including a select-loop. If you would rather block on a read until data is available, then do so on a single byte and fill in the remaining data afterward.

这绝不是如何做到这一点的例子的右键的,但它是如何可以等待一个字节一个简短的样本,要求管道的读大小,得到该数据的其余部分,阅读它,然后继续,直到该管道已没有留下任何数据和父母关闭他们的结局:

This is by no means an example of how to do this right, but it is a short sample of how you can wait on a single byte, request the read-size of the pipe to get the rest of the data, read it, and continue this until the pipe has no data left and the parent shuts down their end:

我希望你觉得它有用。

#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main()
{
    int pid = 0;

    // create pipe pair
    int fd[2];
    pipe(fd);

    pid = fork();
    if (pid == 0)
    {
        // child side
        char *buff = NULL;
        char byte = 0;
        int count = 0;

        // close write side. don't need it.
        close(fd[1]);

        // read at least one byte from the pipe.
        while (read(fd[0], &byte, 1) == 1)
        {
            if (ioctl(fd[0], FIONREAD, &count) != -1)
            {
                fprintf(stdout,"Child: count = %d\n",count);

                // allocate space for the byte we just read + the rest
                //  of whatever is on the pipe.
                buff = malloc(count+1);
                buff[0] = byte;
                if (read(fd[0], buff+1, count) == count)
                    fprintf(stdout,"Child: received \"%s\"\n", buff);
                free(buff);
            }
            else
            {   // could not read in-size
                perror("Failed to read input size.");
            }
        }

        // close our side
        close(fd[0]);
        fprintf(stdout,"Child: Shutting down.\n");
    }
    else
    {   // close read size. don't need it.
        const char msg1[] = "Message From Parent";
        const char msg2[] = "Another Message From Parent";
        close(fd[0]);
        sleep(5); // simulate process wait
        fprintf(stdout, "Parent: sending \"%s\"\n", msg1);
        write(fd[1], msg1, sizeof(msg1));
        sleep(5); // simulate process wait
        fprintf(stdout, "Parent: sending \"%s\"\n", msg2);
        write(fd[1], msg2, sizeof(msg2));
        close(fd[1]);
        fprintf(stdout,"Parent: Shutting down.\n");
    }
    return 0;
}


输出

Parent: sending "Message From Parent"
Child: count = 19
Child: received "Message From Parent"
Parent: sending "Another Message From Parent"
Parent: Shutting down.
Child: count = 27
Child: received "Another Message From Parent"
Child: Shutting down.

这篇关于如何等待直到数据在管的另一端部写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆