简单说明
有名管道会堵塞,直到被读取
测试代码
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
//int main(int argc,char*argv[])
int main(int argc,char**argv)
{
if(argc<2)
{
printf("argc<2\n");
return -1;
}
for(int i=0;i<argc;i++)
{
printf("argv[%d]: %s\n",i,argv[i]);
}
char*fifo_name="my_fifo";
int fd;
if(strcmp(argv[1],"-write")==0)
{
printf("this write mode \n");
if(access(fifo_name,0)==-1)
{
printf("there is not fifo,create it.\n");
mkfifo(fifo_name,0777);
}
fd=open(fifo_name,O_WRONLY);
char*tmp_str="hello,fifo";
write(fd,tmp_str,strlen(tmp_str));
close(fd);
}else if(strcmp(argv[1],"-read")==0)
{
printf("this read mode \n");
fd=open(fifo_name,O_RDONLY);
char buf[32]={0};
read(fd,buf,32);
close(fd);
printf("recv: %s\n",buf);
}
return 0;
}
测试结果
[root@localhost myapp]# ./a.out -write
argv[0]: ./a.out
argv[1]: -write
this write mode
[root@localhost myapp]# ./a.out -read
argv[0]: ./a.out
argv[1]: -read
this read mode
recv: hello,fifo