動手寫驅動(3)--Apple的學習筆記 一,前言 二,遇到的問題 三,測試效果

一,前言

動手寫驅動(2)--Apple的學習筆記之前已經做完了鎖,現在做阻塞和非阻塞練習。

工程5是一個fifo來模擬阻塞。就是寫入後才能讀,讀完清空後就阻塞不能讀了。主要就是測試這個效果。另外驅動的功能包括寫滿後阻塞寫,需要讀取清空後纔可以繼續寫。通過宏定義可以修改爲非阻塞,用戶主動請求嘗試不斷讀取信息,這樣很耗費CPU,正常不會使用的,只是爲了和阻塞休眠情況做對比。

工程5源碼上傳在gitee上https://gitee.com/applecai/linux-driver-study

二,遇到的問題

1.signal_pending沒有添加頭文件。通過在include文件夾中搜索到頭文件如下。另外發現一個signal_pending(current)的宏定義。



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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章