Chinaunix首页 | 论坛 | 博客
  • 博客访问: 440541
  • 博文数量: 113
  • 博客积分: 446
  • 博客等级: 下士
  • 技术积分: 1229
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-09 16:01
个人简介

Let's go!!!!!

文章分类

全部博文(113)

文章存档

2019年(5)

2018年(4)

2017年(9)

2016年(5)

2015年(39)

2014年(6)

2013年(28)

2012年(17)

分类: LINUX

2012-12-09 16:44:51

int mkfifo(char *pathname, mode mode )
    mkfifo函数需要两个参数,第一个参数(pathname)是将要在文件系统中创建的一个专用文件。第二个参数(mode)用来规定FIFO的读写权限。mkfifo函数如果调
用成功的话,返回值为0;如果调用失败返回值为-1。
    mkfifo函数的作用是在文件系统中创建一个文件,该文件用于提供FIFO功能,即命名管道。前边讲的那些管道都没有名字,因此它们被称为匿名管道,或简称管道。
对文件系统来说,匿名管道是不可见的,它的作用仅限于在父进程和子进程两个进程间进行通信。而命名管道是一个可见的文件,因此,它可以用于任何两个进程
之间的通信,不管这两个进程是不是父子进程,也不管这两个进程之间有没有关系。
//fifo_read.c
#incldue
#include
#include
#include
#include
#include
#include
#define FIFO "myfifo"
int main(int argc,char *argv[])
{
int fd;
char buf[100];
int nread;
if((mkfifo(FIFO,O_CREAT|O_EXCL))<0)
{
perror("mkfifo error");
exit(1);
}
printf("preparing for reading bytes...\n");
memset(buf,0,sizeof(buf));
if((open(FIFO,O_RDONLY|O_NONBLOCK,0))<0)
{
perror("open error");
exit(1);
}
while(1)
{
memset(buf,0,sizeof(buf));
if((nread=read(fd,buf,100))<0)
{
if(errno=EAGAIN)
printf("no data yet\n");
}
printf("read %s form fifo\n",buf);
sleep(1);
}
pause();
return 0;
}
//fifo_write.c
#incldue
#include
#include
#include
#include
#include
#include
#define FIFO_SERVER  "myfifo"
int main(int argc,char *argv[])
{
int fd;
char buf[100];
int nwrite;
if((fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0))<0)
{
perror("open error");
exit(1);
}
if(1==argc)
{
perror("need something to send");
exit(1);
}
strcpy(buf,argv[1]);
if((nwrite=write(fd,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",buf);
return 0;
}
//fifi_read.c
#incldue
#include
#include
#include
#include
#include
#include
#define FIFO "myfifo"
int main(int argc,char *argv[])
{
int fd;
char buf[100];
int nread;
if((mkfifo(FIFO,O_CREAT|O_EXCL))<0)
{
perror("mkfifo error");
exit(1);
}
printf("preparing for reading bytes...\n");
memset(buf,0,sizeof(buf));
if((open(FIFO,O_RDONLY|O_NONBLOCK,0))<0)
{
perror("open error");
exit(1);
}
while(1)
{
memset(buf,0,sizeof(buf));
if((nread=read(fd,buf,100))<0)
{
if(errno=EAGAIN)
printf("no data yet\n");
}
printf("read %s form fifo\n",buf);
sleep(1);
}
pause();
return 0;
}
阅读(1009) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~