qfedu-linux-advanced-level/day4/fifo_write_2.c

38 lines
843 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
int main()
{
// 创建 fifo 的有名(文件名)管道
if (mkfifo("myfifo", 0755) != 0)
{
// 管道文件已经存在,代码将继续执行后面的部分,其他错误报错
// 如果errno等于EEXIST则表示管道文件已经存在代码将继续执行后面的部分
if (errno != EEXIST)
{
perror("mkfifo");
return 1;
}
}
printf("准备以只写方式打开 myfifo\n");
int fd = open("myfifo", O_WRONLY | O_NONBLOCK);
if (fd == -1)
{
perror("open");
return 1;
}
printf("open myfifo fd = %d\n", fd);
sleep(3); // 阻塞 5 秒
close(fd);
return 0;
}