免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: 天外飞客
打印 上一主题 下一主题

管道的问题(写的非原子性) [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-04-26 23:28 |只看该作者
原帖由 天外飞客 于 2007-4-25 22:38 发表

书上的一个例子:

#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <limits.h>
                                                           ...

```````````````````
writenum=write(pipe_fd[1],w_buf,4096);
        write(1,"completed\n",11);//在这里加上..
把4096换成4000:结果

the bytes write to pipe is 1024
completed
[root@localhost work]# child readnum is 1000
child readnum is 1000
child readnum is 1000
child readnum is 1000
child readnum is 1000
child readnum is 24
child readnum is 0
把4096 换成4100 结果:
the bytes write to pipe is 1024
child readnum is 1000
completed
[root@localhost work]# child readnum is 1000
child readnum is 1000
child readnum is 1000
child readnum is 1000
child readnum is 124
child readnum is 0

论坛徽章:
0
12 [报告]
发表于 2007-04-27 09:14 |只看该作者
LZ到底要说明什么?

论坛徽章:
0
13 [报告]
发表于 2007-04-27 09:20 |只看该作者
LZ看的什么书?方便说出名字吗

论坛徽章:
0
14 [报告]
发表于 2007-04-27 09:36 |只看该作者

回复 11楼 天外飞客 的帖子

这是正常的啊.
向管道中写入数据时,linux将不保证写入的原子性,管道缓冲区一有空闲区域,写进程就会试图向管道写入数据。如果读进程不读走管道缓冲区中的数据,那么写操作将一直阻塞。
因为你子进程一次只读1000个字节,所以就是这样的啊.

论坛徽章:
0
15 [报告]
发表于 2007-04-27 09:48 |只看该作者
程序哪里能说明“非原子性”?

论坛徽章:
0
16 [报告]
发表于 2007-04-27 11:01 |只看该作者

  1.    PIPE_BUF
  2.        POSIX.1 says that write(2)s of less than PIPE_BUF bytes must be atomic:
  3.        the  output  data  is  written  to  the  pipe as a contiguous sequence.
  4.        Writes of more than PIPE_BUF bytes may be non-atomic:  the  kernel  may
  5.        interleave  the  data  with  data  written by other processes.  POSIX.1
  6.        requires PIPE_BUF to be at least 512 bytes.   (On  Linux,  PIPE_BUF  is
  7.        4096 bytes.)  The precise semantics depend on whether the file descrip-
  8.        tor is non-blocking (O_NONBLOCK), whether there are multiple writers to
  9.        the pipe, and on n, the number of bytes to be written:

  10.        O_NONBLOCK disabled, n <= PIPE_BUF
  11.               All  n bytes are written atomically; write(2) may block if there
  12.               is not room for n bytes to be written immediately

  13.        O_NONBLOCK enabled, n <= PIPE_BUF
  14.               If there is room to write n bytes to  the  pipe,  then  write(2)
  15.               succeeds  immediately,  writing  all n bytes; otherwise write(2)
  16.               fails, with errno set to EAGAIN.

  17.        O_NONBLOCK disabled, n > PIPE_BUF
  18.               The write is non-atomic: the  data  given  to  write(2)  may  be
  19.               interleaved with write(2)s by other process; the write(2) blocks
  20.               until n bytes have been written.

  21.        O_NONBLOCK enabled, n > PIPE_BUF
  22.               If the pipe is full, then write(2)  fails,  with  errno  set  to
  23.               EAGAIN.   Otherwise,  from  1 to n bytes may be written (i.e., a
  24.               "partial write" may occur; the caller should  check  the  return
  25.               value  from  write(2)  to see how many bytes were actually writ-
  26.               ten), and these bytes may be interleaved with  writes  by  other
  27.               processes.
复制代码

论坛徽章:
0
17 [报告]
发表于 2007-04-27 11:08 |只看该作者
说出来什么书。

论坛徽章:
0
18 [报告]
发表于 2007-04-27 12:26 |只看该作者

论坛徽章:
0
19 [报告]
发表于 2007-04-27 12:27 |只看该作者
它上面解释错了.
我想知道为什么是这样的~~

论坛徽章:
0
20 [报告]
发表于 2007-04-27 12:38 |只看该作者
书上自己不明白乱写的很多。

例子中的程序正好说明write pipe (< 4096 bytes)是原子的。没有地方说明不是原子的呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP