Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47633
  • 博文数量: 25
  • 博客积分: 960
  • 博客等级: 准尉
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 12:39
文章分类
文章存档

2011年(1)

2009年(20)

2008年(4)

我的朋友

分类: C/C++

2008-11-05 22:08:41

This is fifo_read.c

#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO "/home/xiang/code/fifo/myfifo"
main(int argc,char *argv[])
{
    char buf_r[100];
    int fd;
    int nread;
    if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
        printf("this is mkfifo errno \n");
    printf("........\n");
    memset(buf_r,0,sizeof(buf_r));
    fd=open(FIFO,O_RDONLY|O_NONBLOCK,066);
    if(fd==-1)
    {
        perror("open");
        exit(1);
    }
    while(1)
    {
        memset(buf_r,0,sizeof(buf_r));
        if((nread=read(fd,buf_r,100))==-1)
        {
            if(errno==EAGAIN)
                printf("no any data yet\n");
        }
        printf("read %s from FIFO\n",buf_r);
        sleep(1);
    }
    pause();
    unlink(FIFO);
}

This is fifo_write.c

#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO_SERVER "/home/xiang/code/fifo/myfifo"
main(int argc ,char * argv[])
{
    int fd;
    char buf_w[100];
    int nwrite;
    if(fd==-1)
        if(errno==ENXIO)
            printf("open error\n");
    fd=open(FIFO_SERVER,O_WRONLY | O_NONBLOCK,0);
    if(argc==1)
        printf("please input your message\n");
    strcpy(buf_w,argv[1]);
    printf("%s\n",argv[1]);
    printf("%s\n",buf_w);
    if((nwrite=write(fd,buf_w,100))==-1)
    {
        if(errno==EAGAIN)
            printf("write error\n");
        else
            printf("%s ok\n",buf_w);
    }
    else
    {
        printf("write %s to the FIFO_SERVER\n",buf_w);

    }
}


注意所建的myfifo 文件的权限,否则不能读取。
阅读(515) | 评论(0) | 转发(0) |
0

上一篇:聊天程序

下一篇:多线程web服务器

给主人留下些什么吧!~~