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

向有名管道写入数据后,不能把数据读出来???
有名管道,是两个人一进程之间的相互通信,下面给管道写入数据,但是并不能从管道中把数据读出来,还真的是让人纳闷啊:

(1)向管道中写入数据:

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo"

main(int argc,char** argv)
{
        int fd;
        char w_buf[100];
        int nwrite;
        if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
        {
                printf("peparing for reading bytes...\n");
        }
        /*′ò?a1üμà*/
        fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);

        if(argc==1)
        {
                printf("Please send something\n");
                exit(-1);
        }

        strcpy(w_buf,argv[1]);

        /* ?ò1üμàD′è?êy?Y */
        if((nwrite=write(fd,w_buf,100))==-1)
        {
                if(errno==EAGAIN)
                        printf("The FIFO has not been read yet.Please try later\n");
        }
        else
                printf("write %s to the FIFO\n",w_buf);
}

(2)向管道中读出数据:

#include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <errno.h>
  4 #include <fcntl.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 #define FIFO "/tmp/myfifo"
  9 
 10 main(int argc,char** argv)
 11 {
 12         char buf_r[100];
 13         int  fd;
 14         int  nread;
 15 
 16         /* ′′?¨1üμà */
 17 //      if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
 18 //              printf("cannot create fifoserver\n");
 19 
 20