Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有select()、read()、write()的I/O多路复用_C++_C - Fatal编程技术网

C++ 具有select()、read()、write()的I/O多路复用

C++ 具有select()、read()、write()的I/O多路复用,c++,c,C++,C,我正在尝试编写一个应用程序,该应用程序由父级多路复用来自不同子级的输入组成,使用select()、write()和read()以及管道 我想就以下几点寻求建议: 当child write()时,parent select()是否会检测并返回,即使子级仍在写入(即write()阻塞尚未返回) 如果子级正在执行write(),而父级正在执行read(),是否可能导致父级读取不完整的消息 Q1和Q2是否需要“保护”代码 e、 g.定义4字节头以指示消息长度。Parent read()首先检查消息长度的

我正在尝试编写一个应用程序,该应用程序由父级多路复用来自不同子级的输入组成,使用select()、write()和read()以及管道

我想就以下几点寻求建议:

  • 当child write()时,parent select()是否会检测并返回,即使子级仍在写入(即write()阻塞尚未返回)

  • 如果子级正在执行write(),而父级正在执行read(),是否可能导致父级读取不完整的消息

  • Q1和Q2是否需要“保护”代码

    e、 g.定义4字节头以指示消息长度。Parent read()首先检查消息长度的4个字节,然后循环并读取()直到得到完全相同的长度

  • 下面是父代码段和子代码段,仅供说明。我想知道孩子的体重变化是否会给父母带来问题

    父代码段

    int MAX_LENGTH=10000;
    char buf[MAX_LENGTH];
    
    int pipeFd[2];
    pipe(pipeFd);
    
    //fork child code and create pipe codes skipped..
    
    fd_set pipeSet;
    int r, nbytes;
    
    close(pipeFd[1]);
    while (1){
       FD_ZERO(&pipeSet);
       FD_SET(pipeFd[0], &pipeSet);
       memset(buf, 0, MAX_LENGTH);
    
       r=select(pipeFd[0] + 1, &pipeSet, NULL, NULL, NULL);
       if(FD_ISSET(pipeFd[0], &pipeSet))
           nbytes=read(pipeFd, buf, MAX_LENGTH);
    }
    
    close(pipeFd[0]);
    int buf_len;
    while (1){
       memset(buf, 0, MAX_LENGTH);
    
       //codes to get data from different means depending on child ID
       //e.g. msgget() from msg queue, user input... etc.
       //length of data varies
    
       write(pipeFd[1], buf, buf_len);
    }
    

    子代码段

    int MAX_LENGTH=10000;
    char buf[MAX_LENGTH];
    
    int pipeFd[2];
    pipe(pipeFd);
    
    //fork child code and create pipe codes skipped..
    
    fd_set pipeSet;
    int r, nbytes;
    
    close(pipeFd[1]);
    while (1){
       FD_ZERO(&pipeSet);
       FD_SET(pipeFd[0], &pipeSet);
       memset(buf, 0, MAX_LENGTH);
    
       r=select(pipeFd[0] + 1, &pipeSet, NULL, NULL, NULL);
       if(FD_ISSET(pipeFd[0], &pipeSet))
           nbytes=read(pipeFd, buf, MAX_LENGTH);
    }
    
    close(pipeFd[0]);
    int buf_len;
    while (1){
       memset(buf, 0, MAX_LENGTH);
    
       //codes to get data from different means depending on child ID
       //e.g. msgget() from msg queue, user input... etc.
       //length of data varies
    
       write(pipeFd[1], buf, buf_len);
    }
    
    当child write()时,parent select()是否会检测并返回,即使子级仍在写入(即write()阻塞尚未返回)

    一旦接收器的套接字接收缓冲区中有数据,套接字就变得可读。如果要在该套接字上选择可读性,则select()将返回。可能只接收到一个字节

    如果子级正在执行write(),而父级正在执行read(),是否可能导致父级读取不完整的消息

    TCP中没有消息这样的东西。父级总是可以读取的数据少于其请求的数据。孩子当时在做什么无关紧要

    Q1和Q2是否需要“保护”代码

    e、 g.定义4字节头以指示消息长度。Parent read()首先检查消息长度的4个字节,然后循环并读取()直到得到完全相同的长度


    这不是使用多路I/O的正确方法。接收器应该读取数据,如果数据不完整,它应该返回到select循环,等待另一个读取事件,进行另一次读取,等等,直到数据完成。请注意,在读取计数字和数据时,可能会发生不完全读取。

    这是否回答了您的问题?这个代码看起来更像C而不是C++。考虑阅读和使用BooS.asi.谢谢@ 5Go12EDER它是有用的。我会检查系统中的PiPeyBuf限制,看看消息的长度是否超过那个。@多态土豆,是的,这些代码是其中的一部分,还有C++代码。哈哈。谢谢你的建议,但我不能用Boost来完成任务(非技术原因)。