📄 fifo_write.c
字号:
/*fifo_write.c*/
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <fcntl.h>
#include FIFO "/tmp/myfifo"
main(int argc,char** argv)
/*参数为即将写入的字节数*/
{
char w_buf[100];
int fd;
int nwrite;
if(fd == -1)
if(errno == ENXIO)
printf("open error:no reading process\n");
/*打开FIFO管道,并设置非阻塞标志*/
fd=open(FIFO,O_WRONLY|O_NONBLOCK,0);
if(argc == 1)
printf("Please send something\n");
strcpy(w_buf,argv[1]);//拷贝字符串,将参数argv[1]字符串拷贝至参数w_buf所指的地址.
/*向管道中写入字符串*/
if((nwrite=write(fd,w_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",w_buf);
}
exit(1);
}
while(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread = read (fd,buf_r,100)) == -1){
if(errno == EAGAIN)
printf("no data yet\n");
}
printf("read %s from FIFO\n",buf_r);
sleep(1);
}
pause();
unlink(FIFO);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -