当前位置:网站首页>blocking non-blocking poll mechanism asynchronous
blocking non-blocking poll mechanism asynchronous
2022-08-10 11:08:00 【life needs depth】
1 Blocking
Blocking operation: When performing device operations, if resources cannot be obtained, the process will be suspended until the operable conditions are met before the operation is performed.
read()-->HelloRead
How to implement blocking in the driver?You can define a sleep waiting queue
wait_queue_head_t q;//Define a sleep waiting queue head
init_waitqueue_head(&q);//Initialize the sleep waiting queue
Use: helloRead:
wait_event_interruptible(q,con)-->Sleep the process requesting to read data--"The sleep period can be interrupted
wait_event(q,con)-->The sleep period cannot be interrupted
HelloWrite:-->Wake up the read process to read data after writing data
wake_up_interruptible(&q);-->Wake up the sleep process that can be interrupted
wake_up;
con=1;
con: The wake-up condition of the sleep process process: 1 wake up 0 sleep
Note: When the process is running normally, the process is waiting to be run in the running pair column.
When the process sleeps, the process waits in the sleep waiting queue to be woken up to run
2 Non-blocking
Non-blocking operation: It means that when the device operation cannot be performed, it does not sleep or suspend the process, but immediately returns an incorrect value to the requesting process, and the underlying device driver passes the non-stopThe query device operation can be performed until the operation can be performed and a correct result is returned to the requesting process.
read-->non-blocking
application layer: open("dev/haha0",O_RDWR|O_NONBLOCK);
Driver layer: HelloRead()
if (no data can be read in the device)
{
if (the O_NONBLOCK flag is passed in by the application layer)
{
Return an incorrect value to the application
} }
else
{
Blocking the process;
blocking the process;
blocking the process
g_charcount: Indicates the number of characters actually written to the kernel
3 poll mechanism
io multiplexing--"select poll epoll
HelloPoll(struct file *pFile, struct poll_table_struct *ptable)
Implementation: 1 Put the process of monitoring the file descriptor into the monitoring queue--"poll_wait(pFile,&q,ptable)
ptable: polling table
Add the process currently operating the file to the sleep waiting queue, add the queue to ptable.
2 The system monitors the sleeping process in the monitoring queue. If there is IO data on the file descriptor of the sleeping process, the system will activate the process.
return mask;
mask: a bit mask used to describe whether the operation can be executed immediately without blockingStatus (each BIT bit can be represented by a macro)
POLLIN: data can be read
POLLRDNORM: normal data can be read
POLLOUT: data can be written
POLLERR: specified file descriptioncharacter error
4 Asynchronous IO
Asynchronous IO: The read process goes to the device to read data. If the device has no data to read, then the current process is blocked. If the kernel detects that the device has data to read, it will send a notification signal to the read process.The read process will execute the processing action corresponding to the signal to read the data.
Application:
A (read data process)
1 Register a signal handler function signal(SIGIO,hander);
2 Open the device file fd=open("/dev/haha0",...);
3 3 Set the host of the file descriptor fcntl(fd,F_SETOWN,getpid());
4 Read the flag of the file descriptor int flags=fcntl(fd,F_GETFL);
5 Set the file descriptor flag fcntl(fd,F_SETFL,flags|FASYNC)->Asynchronous notification flag
while(1);
hander-->read()
Note: fcntl: is a system call function, used to set or get a fileDescriptor properties
B (write data process)
1 open("/dev/haha0");
2 write-->helloWrite-->signal to the reading process
3 close
Driver:
1 HelloFasync
1 Define a global asynchronous notification structure--"Associated signal sender and receiver
2 Initialize asynchronous notification structure
fasync_helper(fd,pFile,on,&fasync)
2 HelloWrite
kill_fasync(&fasync,SIGIO,POLL_IN);
SIGIO-->fasync-->fd(read)--"getpid()==A
边栏推荐
- ESP8266 Tutorial 2 - Burn AT Firmware
- Store limited time seckill function system
- 三相380V整流后的电压
- owl.carousel海报卡片Slider轮播切换
- what is rtems
- The impact of development mode on testing
- ECCV 2022 | 视频理解新框架X-CLIP:仅用微调的成本,达到预训练的全能
- [Microservice Architecture] Microservices and SOA Architecture (2)
- 3 injured in 'electrical accident' at Google data center
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
猜你喜欢
mysql5.7安装部署-yum安装
"Time Series Database" uses cassandra to scan time series data
TCP/IP笔记
The impact of development mode on testing
自动化测试及Selenium
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
Store limited time seckill function system
Network Security Note 6 - Digital Certificates and Public Key Infrastructure
Cybersecurity Notes 5 - Digital Signatures
组合模式:Swift 实现
随机推荐
4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
Flutter实战-请求封装(五)之Isolate线程改造
跨公网环境,路由策略,进行设备的访问
Research on motion capture system for indoor combined positioning technology
解决ASP.NET Core在Task中使用IServiceProvider的问题
第3章-线性方程组(3)
Double.doubleToLongBits()方法使用
2022.8.9-----leetcode.1413
C#List的使用以及Linq的使用
what is rtems
【C语言】浮点数四舍五入
[Microservice Architecture] Microservices and SOA Architecture (2)
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
Unsafe的一些使用技巧
OSSCore 开源解决方案介绍
短视频软件开发——平台同质化如何破局
WebView2 通过 PuppeteerSharp 实现爬取 王者 壁纸 (案例版)
技能大赛训练题:组策略一
owl.carousel海报卡片Slider轮播切换
网络安全笔记6——数字证书与公钥基础设施