加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

当使用unix管道(在C中)时,操作系统是否使用read()平衡每个write(

发布时间:2020-12-15 17:01:08 所属栏目:安全 来源:网络整理
导读:例如,我想从一个孩子到父母获得一个4个整数的数组.家长电话 read(apipe,buf,sizeof(int)*4); 孩子的电话 for(int i=0; i4;i++) write(bpipe,array[i],sizeof(int)); 这样做我打算做什么(给父母4个整数)或父母只是得到第一个整数? 我尝试在其他地方寻找这个
例如,我想从一个孩子到父母获得一个4个整数的数组.家长电话
read(apipe,buf,sizeof(int)*4);

孩子的电话

for(int i=0; i<4;i++)
    write(bpipe,&array[i],sizeof(int));

这样做我打算做什么(给父母4个整数)或父母只是得到第一个整数?

我尝试在其他地方寻找这个答案,但要么我不知道如何搜索,或者这太过于微妙(或者另一方面看似太明显),因此文学要详述.

编辑:为了进一步澄清,我试图写一个4部分的消息,并在一个read()中读取所有4个部分.查看已接受答案的评论.

使用字节读写工作,而不是消息.有关它们如何使用管道的详细信息,请参阅POSIX中的文档:

> http://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html

在您的代码中,我认为读取应始终为4个整数,原因如下:

Upon successful completion,where nbyte is greater than 0,read() shall mark for update the last data access timestamp of the file,and shall return the number of bytes read. This number shall never be greater than nbyte. The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte,if the read() request was interrupted by a signal,or if the file is a pipe or FIFO or special file and has fewer than nbyte bytes immediately available for reading. For example,a read() from a file associated with a terminal may return one typed line of data.

总共有4个可用于读取的整数,因为4 * sizeof(int)< PIPE_BUF因此写入此大小是原子的. 当被信号中断时,读取允许返回短读取可能会起作用,但是当有足够多的字节可用时,这种情况不应该发生(至少在现实世界中).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读