p_wbuf=w_buf;      if(pipe(pipe_fd)<0)   {     printf("pipe create error\n");     return -1;   }      if((pid=fork())==0)   {     printf("\n");     close(pipe_fd[1]);     sleep(3);    //确保父进程关闭写端     r_num=read(pipe_fd[0],r_buf,100);     printf("read num is %d   the data read from the pipe is %d\n",r_num,atoi(r_buf));          close(pipe_fd[0]);     exit();   }   else if(pid>0)   {     close(pipe_fd[0]);//read     strcpy(w_buf,"111");     if(write(pipe_fd[1],w_buf,4)!=-1)       printf("parent write over\n");     close(pipe_fd[1]);//write     printf("parent close fd[1] over\n");     sleep(10);   } }    /************************************************** * 程序输出结果: * parent write over * parent close fd[1] over * read num is 4   the data read from the pipe is 111 * 附加结论: * 管道写端关闭后,写入的数据将一直存在,直到读出为止. ****************************************************/ 
  向管道中写入数据 
    - 向管道中写入数据时,linux将不保证写入的原子性,管道缓冲区一有空闲区域,写进程就会试图向管道写入数据。如果读进程不读走管道缓冲区中的数据,那么写操作将一直阻塞。 
 
 
  注:只有在管道的读端存在时,向管道中写入数据才有意义。否则,向管道中写入数据的进程将收到内核传来的SIFPIPE信号,应用程序可以处理该信号,也可以忽略(默认动作则是应用程序终止)。 
  对管道的写规则的验证1:写端对读端存在的依赖性 
#include <unistd.h> #include <sys/types.h>    main() {   int pipe_fd[2];   pid_t pid;   char r_buf[4];   char* w_buf;   int writenum; 
		    
                      
		      
		      
		   |