LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 572|回复: 1

一个FIFO的问题

[复制链接]
发表于 2007-3-28 22:08:15 | 显示全部楼层 |阅读模式
我写了wfifo.c 和 rfifo.c
wfifo.c在后台运行
程序如下:
/*****************wfifo.c********************/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <asm/limits.h>

int main(void)
{
        int resdfifo,dummyfd;
        int nLen;
        char szBuf[1024];
        mode_t mode =666;
        if((mkfifo("/var/ff",mode)<0)&&(errno!=EEXIST))
        {
                printf("creat fifo error\n");
                }

         dummyfd        =open("/var/ff",O_WRONLY,0);
       
        nLen=sprintf(szBuf,"wfifo send the message");
        if(write(dummyfd,szBuf,nLen+1)<0)
        {
                printf("open the fifo write error\n");
                close(dummyfd);
                exit(-1);
                }
        close(dummyfd);
        return 0;
        }

/****************rfifo.c******************/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <asm/limits.h>

int main(void)
{
        int nFd;
        int nLen;
        char szBuf[1024];
        if((nFd=open("/var/ff",O_RDONLY))<0)
        {
                printf("open the fifo read error\n");
                exit(-1);
                }
               
        while((nLen=read(nFd,szBuf,1024))>0)
        printf("read fifo: %s\n",szBuf);
        close(nFd);
        unlink("/var/ff");
        return 0;
}
然后编译
/> wfifo &
[16]
/> rfifo
read fifo: wfifo send the message
/> rfifo
open the fifo read error
pid 18: failed 65280

为什么我再读FIFO时,FIFO就空了呢
如何每次读FIFO时,FIFO里都有数据呢
求大虾指点
发表于 2007-3-30 08:37:50 | 显示全部楼层
因为你的wfifo写完之后就退出了,下次你再rfifo时,根本就没有wfifo去创建FIFO.所以会出现open the fifo read error.
还有,你是用root吧?否则,你是没权限写var/的。
         在debian 上mode=777,才行,要是666的话,也会出错。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表