Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15339645
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2009-07-11 10:19:19

#include <stdio.h>
#include <fcntl.h>
int main(void)
{
    const char *pipe_name = ".luther.gliethtttp.0";
    int fd;

    unlink(pipe_name);
    mkfifo(pipe_name, 0666);

    fd = open (pipe_name, O_RDWR | O_NDELAY);
    if (fd < 0) {
        printf("Error to open pipe!\n");
        return -1;
    }

    for (;;) {
        static int count = 0;
        char buf[20];
        int len;
        len = sprintf(buf, "%d\n", count++);
        write(fd , buf, len);
        sleep(1);
    }
    return 0;
}

luther@gliethttp:/vobs/tmp$ gcc mkfifo_pipe.c
luther@gliethttp:/vobs/tmp$ ./a.out
打开另一个terminal,运行cat读取.
luther@gliethttp:/vobs/tmp$ cat .luther.gliethtttp.0   // 读取.luther.gliethtttp.0管道发过来的数据.
0
1
2
3
4
5
6
7

阅读(1592) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~