日期:2014-05-16  浏览次数:20731 次

求助:关于pipe写入的原子性
网上有篇文章叫做《深入理解Linux进程间通信》,其中有如下一段内容:
=========================================================================================

对管道的写规则的验证2:linux不保证写管道的原子性验证
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
main(int argc,char**argv)
{
  int pipe_fd[2];
  pid_t pid;
  char r_buf[4096];
  char w_buf[4096*2];
  int writenum;
  int rnum;
  memset(r_buf,0,sizeof(r_buf));  
  if(pipe(pipe_fd)<0)
  {
  printf("pipe create error\n");
  return -1;
  }
 
  if((pid=fork())==0)
  {
  close(pipe_fd[1]);
  while(1)
  {
  sleep(1);  
  rnum=read(pipe_fd[0],r_buf,1000);
  printf("child: readnum is %d\n",rnum);
  }
  close(pipe_fd[0]);
  exit();
  }
  else if(pid>0)
  {
  close(pipe_fd[0]);//write
  memset(r_buf,0,sizeof(r_buf));  
  if((writenum=write(pipe_fd[1],w_buf,1024))==-1)
  printf("write to pipe error\n");
  else  
  printf("the bytes write to pipe is %d \n", writenum);
  writenum=write(pipe_fd[1],w_buf,4096);
  close(pipe_fd[1]);
  }  
}
 
输出结果:
the bytes write to pipe 1000
the bytes write to pipe 1000 //注意,此行输出说明了写入的非原子性
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 120 //注意,此行输出说明了写入的非原子性
the bytes write to pipe 0
the bytes write to pipe 0
......

结论:
写入数目小于4096时写入是非原子的! 
如果把父进程中的两次写入字节数都改为5000,则很容易得出下面结论: 
写入管道的数据量大于4096字节时,缓冲区的空闲空间将被写入数据(补齐),直到写完所有数据为止,如果没有进程读数据,则一直阻塞。

=========================================================================================

抛开这个程序中的错误,请问这个程序为什么能说明写入的原子性,背后的原理是什么?求指导。

------解决方案--------------------
没看明白

pipe 写入的原子性,是说多个进程写入管道,如果每次写入的字节数目都小于PIPE_BUF,那么各个进程写入的数据彼此不会交叠

这个程序中,一个读,一个写,不知道演示了什么
------解决方案--------------------
探讨
没看明白

pipe 写入的原子性,是说多个进程写入管道,如果每次写入的字节数目都小于PIPE_BUF,那么各个进程写入的数据彼此不会交叠

这个程序中,一个读,一个写,不知道演示了什么