当前位置:网站首页>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
边栏推荐
- [Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
- 1-IMU参数解析以及选择
- Taro小程序跨端开发入门实战
- 开发模式对测试的影响
- dedecms supports one-click upload of Word content
- OneFlow source code parsing: operator instructions executed in a virtual machine
- bus event bus use
- 「首席工程师」首席(Principal )工程师修炼之道
- 网络安全笔记5——数字签名
- Redis6 (1) - Introduction to NoSQL Database and Installation of Redis
猜你喜欢

Introduction to cross-end development of Taro applet

干货!ASSANet:让PointNet++更快更强

Dry goods!ASSANet: Making PointNet++ faster and stronger

3D rotating text animation js special effects

owl.carousel海报卡片Slider轮播切换

Research on motion capture system for indoor combined positioning technology

金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案

跨公网环境,路由策略,进行设备的访问

8月份DB-Engines 数据库排行榜最新战况

ISO9001在讲什么?过程方法和风险思维
随机推荐
"Chief Engineer" Principal (Principal) engineer's way of training
POJ 2891 Strange Way to Express Integers (扩展欧几里得)
突破次元壁垒,让身边的玩偶手办在屏幕上动起来!
ECCV 2022 | 视频理解新框架X-CLIP:仅用微调的成本,达到预训练的全能
Dialogue with Chen Ciliang: Nezha wants to popularize high-end products
CodeChef STRMRG String Merging (dp)
In August the DB list latest scores - database Engines
3D旋转文本动画js特效
js guessing game source code
Get started quickly and conquer three different distributed architecture calling schemes
【FAQ】【Push Kit】 华为怎么设置角标
MySQL executes the query process
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
2022.8.8-----leetcode.761
Several small projects that I have open sourced over the years
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
Cybersecurity Notes 5 - Digital Signatures
Redis(六)——Redis6的事务和锁机制(未完成,待补)
HCIP ---- VLAN
使用cpolar远程连接群晖NAS(升级固定链接2)