码迷,mamicode.com
首页 > 其他好文 > 详细

fifo read

时间:2016-05-20 13:05:44      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#define FIFO    "/tmp/myfifo"

int main(int argc,char *argv[])
{
    int fd;         //file description
    char buf_r[50];    //read buffer
    
    if((mkfifo(FIFO,O_CREAT|O_EXCL|0666)<0) && (errno!=EEXIST))
    {
        printf("Create FIFO fail!\n");
    }

    
    fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
    if(fd==-1)
    {
        printf("Open the FIFO fail!\n");
        exit(0);
    }

    memset(buf_r,0,sizeof(buf_r));
    while(1)
    {
        memset(buf_r,\0,sizeof(buf_r));
        if(read(fd,buf_r,50)<0)
        {
            if(errno==EAGAIN)
                printf("There are no data in FIFO!\n");
        }
        if(buf_r[0]!=\0)
            printf("Reading data from FIFO are: %s\n",buf_r);
        sleep(1);
    }

    return 0;
}

 

fifo read

标签:

原文地址:http://www.cnblogs.com/foggia2004/p/5511552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!