- #include<sys/types.h>
- #include<sys/stat.h>
- #include<errno.h>
- #include<string.h>
- #include<stdio.h>
- #include<unistd.h>
- #include<fcntl.h>
- int
- main (void)
- {
- char buf[1024];
- char fn[] = "myfifo";
- int ret = mkfifo (fn, S_IRUSR | S_IWUSR);
- if (ret == -1)
- {
- printf ("mkfifo error:%s\n", strerror (errno));
- return 1;
- }
- int rd = open (fn, O_RDONLY | O_NONBLOCK);
- int fd = open (fn, O_WRONLY | O_NONBLOCK, S_IRWXU);//此处注意,必须先打开读,否则会打不开fifo,根据论坛提到的如果设置非阻塞,必须先open一个fifo读。
- if (fd == -1)
- {
- printf ("open error:%s\n", strerror (errno));
- return 1;
- }
- write (fd, "hello", 5);
- read (rd, buf, 5);
- write (1, buf, 5);//向设备1写入,即显示屏
- close (fd);
- unlink (fn);
- return 0;
- }
转载于:https://www.cnblogs.com/dpf-learn/p/7700977.html