当前位置:网站首页>Fcntl function
Fcntl function
2022-07-22 18:22:00 【Lewin~】
adopt fcntl Function to set the blocking property of the file
Set to block : fcntl(fd, F_SETFL, 0);
Set to non blocking : fcntl(fd, F_SETFL, O_NONBLOCK);
If it's a jam , There's no data in the pipeline ,read Will be waiting , It will not continue to run until there is data , Otherwise one Wait for
If it's non blocking ,read Function runtime , I will first check whether there is data in the pipeline , If there's data , That's right Read data in normal operation , If there is no data in the pipeline , be read The function immediately returns , Continue to run the following code
Case study :
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd_pipe[2];
char buf[] = "hello world";
pid_t pid;
if (pipe(fd_pipe) < 0)
{
perror("fail to pipe");
exit(1);
}
pid = fork();
if (pid < 0)
{
perror("fail to fork");
exit(0);
}
if (pid == 0)
{
while(1)
{
sleep(5);
write(fd_pipe[1], buf, strlen(buf));
}
}
else
{
// take fd_pipe[0] Set to block
//fcntl(fd_pipe[0], F_SETFL, 0);
// take fd_pipe[0] Set to non blocking
fcntl(fd_pipe[0], F_SETFL, O_NONBLOCK);
while(1)
{
memset(buf, 0, sizeof(buf));
read(fd_pipe[0], buf, sizeof(buf));
printf("buf=[%s]\n", buf);
sleep(1);
}
}
return 0;
}
Execution results :
边栏推荐
- Internet Download Manager2022智能win最新版下载器
- Aruba学习笔记04-Web UI --Configuration面板介绍
- Gbase8s database specifies the comparison order with set collation
- 「武汉理工大学 软件工程复习」第五章 | 软件体系结构
- Solve typescript error: a computed property name in an interface must refer to an expression why type
- QT notes - unpolish() and polish() of QT dynamic attributes
- C # entry series (XXVII) -- Brief Analysis of LINQ
- [opencv introduction practice] use the front camera of the computer for face detection
- [exception] generate guid and datetime, import test data to the database
- Dokcer运行Nacos容器自动退出问题
猜你喜欢
【Bug】datetime格式化失败
QT笔记——Qt动态属性 之 unpolish() 和 polish()
女嘉賓報名
[exception] generate guid and datetime, import test data to the database
QT笔记—— VS一个项目生成多个exe文件
QT notes - qudpsocket of network communication
逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
"Review of software engineering in Wuhan University of technology" Chapter 5 | software architecture
fcntl函数
Yunyuanyuan (10) | introduction to kubernetes in kubernetes
随机推荐
How to solve the gloomy life under the middle-aged crisis of it
如何搭建清晰易懂的数据看板?
Gbase8s database union operator
基于流谱理论的SSL/TLS协议攻击检测方法
Concurrent model values actor and CSP
Gbase8s database comparison performed by database objects
Hblock盘活企业级存储市场
一线互联网P7面试题总结---补充中
Design of miner type identification mechanism based on reputation management model
QT notes - vs generating multiple exe files for a project
"Review of software engineering in Wuhan University of technology" Chapter 7 | software testing
Shell script uses expect automatic interactive login to remote host for batch shutdown
一招教你招聘数据可视化~还有人不会这些数据分析小案例吗?
[opencv introduction practice] use the front camera of the computer for face detection
可重入函数
面向5G mMTC的网络切片安全研究
《微信小程序-进阶篇》Lin-ui组件库的安装与引入
飞机大战中对象池模式子弹使用
Dokcer运行Nacos容器自动退出问题
Calculate deposit interest