一,前言
动手写驱动(2)--Apple的学习笔记之前已经做完了锁,现在做阻塞和非阻塞练习。
工程5是一个fifo来模拟阻塞。就是写入后才能读,读完清空后就阻塞不能读了。主要就是测试这个效果。另外驱动的功能包括写满后阻塞写,需要读取清空后才可以继续写。通过宏定义可以修改为非阻塞,用户主动请求尝试不断读取信息,这样很耗费CPU,正常不会使用的,只是为了和阻塞休眠情况做对比。
工程5源码上传在gitee上https://gitee.com/applecai/linux-driver-study
二,遇到的问题
1.signal_pending没有添加头文件。通过在include文件夹中搜索到头文件如下。另外发现一个signal_pending(current)的宏定义。
image.png
2.APP中while一直读,read应该按返回值长度判断来读取,我一开始读取的最后一个byte都为D,比我也如的内容要多一个字节,原因是没有判断EOF,所以不正确,应该判断readval==1后读取一个%c。
三,测试效果
工程5的阻塞效果
define SUPPORT_NONBLOCK 为0。阻塞的话,读取不到数据则进程休眠,让出cpu,通过top看到cpu占用率为0.
# cd /usr/study/
# insmod applepaper5.ko
[ 16.628345] applepaper5: loading out-of-tree module taints kernel.
[ 16.636220] Registered character driver
[ 16.640318] Registered character driver
[ 16.645580] Registered character driver
# ./applepaper5 /dev/applepaper6 &
# echo "Hi AppleCai" > /dev/applepaper6
[ 35.646176] written 12 bytes,current_len:12
[ 35.650472] read 1 bytes,current_len:11
# [ 35.656943] read 1 bytes,current_len:10
[ 35.660827] read 1 bytes,current_len:9
[ 35.665313] read 1 bytes,current_len:8
[ 35.669103] read 1 bytes,current_len:7
[ 35.673411] read 1 bytes,current_len:6
[ 35.677197] read 1 bytes,current_len:5
[ 35.680971] read 1 bytes,current_len:4
[ 35.685707] read 1 bytes,current_len:3
[ 35.689499] read 1 bytes,current_len:2
[ 35.693801] read 1 bytes,current_len:1
[ 35.697587] read 1 bytes,current_len:0
Hi AppleCai
# echo "Good night!" > /dev/applepaper6
[ 57.438287] written 12 bytes,current_len:12
[ 57.442583] read 1 bytes,current_len:11
# [ 57.449291] read 1 bytes,current_len:10
[ 57.453405] read 1 bytes,current_len:9
[ 57.457184] read 1 bytes,current_len:8
[ 57.460958] read 1 bytes,current_len:7
[ 57.465580] read 1 bytes,current_len:6
[ 57.469370] read 1 bytes,current_len:5
[ 57.473769] read 1 bytes,current_len:4
[ 57.477553] read 1 bytes,current_len:3
[ 57.481326] read 1 bytes,current_len:2
[ 57.485669] read 1 bytes,current_len:1
[ 57.489453] read 1 bytes,current_len:0
Good night!
# top
Mem: 20816K used, 482640K free, 44K shrd, 0K buff, 1952K cached
CPU: 0% usr 10% sys 0% nic 90% idle 0% io 0% irq 0% sirq
Load average: 0.06 0.03 0.01 1/53 114
PID PPID USER STAT VSZ %VSZ %CPU COMMAND
114 110 root R 1500 0% 10% top
110 1 root S 1536 0% 0% -sh
1 0 root S 1500 0% 0% init
81 1 root S 1492 0% 0% /sbin/syslogd -n
85 1 root S 1488 0% 0% /sbin/klogd -n
113 110 root S 688 0% 0% ./applepaper5 /dev/applepaper6
49 2 root IW 0 0% 0% [kworker/0:3-nfs]
60 2 root IW< 0 0% 0% [kworker/u3:2-xp]
48 2 root IW 0 0% 0% [kworker/0:2-eve]
65 2 root IW< 0 0% 0% [kworker/u3:3-xp]
15 2 root IW 0 0% 0% [kworker/0:1-rcu]
10 2 root IW 0 0% 0% [rcu_sched]
62 2 root IW 0 0% 0% [kworker/u2:3-rp]
13 2 root SW 0 0% 0% [kdevtmpfs]
63 2 root IW 0 0% 0% [kworker/u2:4-rp]
61 2 root IW 0 0% 0% [kworker/u2:2-rp]
7 2 root IW 0 0% 0% [kworker/u2:0-rp]
9 2 root SW 0 0% 0% [ksoftirqd/0]
45 2 root IW 0 0% 0% [kworker/u2:1-rp]
41 2 root IW< 0 0% 0% [kworker/u3:0-xp]
define SUPPORT_NONBLOCK 为1。通过open函数设置阻塞,非阻塞的话,就是不断的读,用户需要主动查询io状态,通过top看到cpu占用率为92%
# cd /usr/study/
# insmod applepaper5.ko
[ 17.386455] applepaper5: loading out-of-tree module taints kernel.
[ 17.395648] Registered character driver
[ 17.399782] Registered character driver
[ 17.404778] Registered character driver
# ./applepaper5 /dev/applepaper6 &
# echo "hello AppleCai" > /dev/applepaper6
[ 44.461110] written 15 bytes,current_len:15
[ 44.465535] read 1 bytes,current_len:14
[ 44.469499] read 1 bytes,current_len:13
# [ 44.476826] read 1 bytes,current_len:12
[ 44.480756] read 1 bytes,current_len:11
[ 44.485496] read 1 bytes,current_len:10
[ 44.489440] read 1 bytes,current_len:9
[ 44.493746] read 1 bytes,current_len:8
[ 44.497531] read 1 bytes,current_len:7
[ 44.501307] read 1 bytes,current_len:6
[ 44.505793] read 1 bytes,current_len:5
[ 44.509579] read 1 bytes,current_len:4
[ 44.513816] read 1 bytes,current_len:3
[ 44.517597] read 1 bytes,current_len:2
[ 44.521371] read 1 bytes,current_len:1
[ 44.525670] read 1 bytes,current_len:0
hello AppleCai
# top
Mem: 20824K used, 482632K free, 44K shrd, 0K buff, 1952K cached
CPU: 16% usr 83% sys 0% nic 0% idle 0% io 0% irq 0% sirq
Load average: 0.42 0.11 0.04 2/54 115
PID PPID USER STAT VSZ %VSZ %CPU COMMAND
114 111 root R 688 0% 92% ./applepaper5 /dev/applepaper6
115 111 root R 1500 0% 8% top
111 1 root S 1536 0% 0% -sh
1 0 root S 1500 0% 0% init
82 1 root S 1492 0% 0% /sbin/syslogd -n
86 1 root S 1488 0% 0% /sbin/klogd -n
48 2 root IW 0 0% 0% [kworker/0:2-eve]
49 2 root IW 0 0% 0% [kworker/0:3-nfs]
41 2 root IW< 0 0% 0% [kworker/u3:0-xp]
68 2 root IW< 0 0% 0% [kworker/u3:3-xp]
15 2 root IW 0 0% 0% [kworker/0:1-rcu]
64 2 root IW 0 0% 0% [kworker/u2:5-rp]
10 2 root IW 0 0% 0% [rcu_sched]
7 2 root IW 0 0% 0% [kworker/u2:0-rp]
60 2 root IW< 0 0% 0% [kworker/u3:2-xp]
9 2 root SW 0 0% 0% [ksoftirqd/0]
45 2 root IW 0 0% 0% [kworker/u2:1-rp]
13 2 root SW 0 0% 0% [kdevtmpfs]
5 2 root IW 0 0% 0% [kworker/0:0-rcu]
8 2 root IW< 0 0% 0% [mm_percpu_wq]
网友评论