当前位置: 技术问答>linux和unix
FIFO一段小程序,无法读出FIFO。
来源: 互联网 发布时间:2017-01-22
本文导语: #include #include #include #include #include #define FIFO "/root/fan/fifo_op/fifo" #define MODE O_RDWR|O_NONBLOCK //可读可写,非阻塞 int main() { int fd,r_num,w_num; char ptr[100]; memset(ptr,0,sizeof(ptr)); if((fd=mkfifo(FIFO,MODE))==-1){ printf("creat fifo ...
#include
#include
#include
#include
#include
#define FIFO "/root/fan/fifo_op/fifo"
#define MODE O_RDWR|O_NONBLOCK //可读可写,非阻塞
int main()
{
int fd,r_num,w_num;
char ptr[100];
memset(ptr,0,sizeof(ptr));
if((fd=mkfifo(FIFO,MODE))==-1){
printf("creat fifo failed!");
}
if(write(fd,"hello world",sizeof("hello world"))!=-1){
printf("write to fifo :hello worldn");
}
close(fd);
fd = open(FIFO,MODE,0755);
printf("%dn",fd);
printf("%dn",(read(fd,ptr,100)));
if((r_num=read(fd,ptr,100))!=-1){
printf("%dn",fd);
printf("%d bytes has been read ..n",r_num);
printf("%sn",ptr);
}
close(fd);
}
为什么会读不出来呢???read(fd,ptr,100)总是返回-1
|
NAME
mkfifo - make a FIFO special file (a named pipe)
SYNOPSIS
#include
#include
int mkfifo(const char *pathname, mode_t mode);
DESCRIPTION
mkfifo() makes a FIFO special file with name pathname. mode specifies the FIFO's permissions. It is modified by the process's umask in the usual
way: the permissions of the created file are (mode & ~umask).
A FIFO special file is similar to a pipe, except that it is created in a different way. Instead of being an anonymous communications channel, a
FIFO special file is entered into the file system by calling mkfifo().
Once you have created a FIFO special file in this way, any process can open it for reading or writing, in the same way as an ordinary file. How‐
ever, it has to be open at both ends simultaneously before you can proceed to do any input or output operations on it. Opening a FIFO for reading
normally blocks until some other process opens the same FIFO for writing, and vice versa. See fifo(7) for nonblocking handling of FIFO special
files.
#include
#include
int status;
...
status = mkfifo("/home/cnd/mod_done", S_IWUSR | S_IRUSR |
S_IRGRP | S_IROTH);
你创建都不对,读毛。