linux內核實驗七實現

實驗題目:字符設備驅動程序實驗
實驗目的:
問題A
分析調試以上scull 設備驅動,並用字符設備有關的系統命令測試其功能,進而解析
字符設備I/O 驅動的構造和基本工作過程。樣例程序限定了內存讀寫的字節數,請重寫一
個不限定設備讀寫長度的scull 設備,它可以使用命令cp /dev/zero /dev/scull0 喫光
所有RAM 存儲器。
問題B
在驅動程序內部,阻塞在read 調用的進程在數據到達時被喚醒;通常硬件會發一箇中
斷來通知這個事件,然後作爲中斷程序處理的一部分驅動程序會喚醒等待進程。當沒有硬
件或中斷處理程序時(如以上scull 字符設備模擬程序)我們可以使用一個緩衝區和另一
個進程寫進程來產生數據並喚醒讀取進程;類似的,阻塞在緩衝區write 調用上的寫進程
也可以有另一讀進程喚醒。這就是實現類管道設備的一種特殊設備驅動。請將以上scull
設備驅動改造爲這種管道類設備驅動程序。

程序完整源代碼:
測試程序如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
main(int argc,char *argv[])
{
int fd ;
char num;
int jieshou;
jieshou=atoi(argv[1]);
if(jieshou==1)
{
fd= open("/dev/scullpipe",O_RDWR);
int jishu=0;
while(1)//send data
{
//printf("1\n");
if (fd != -1 )
{
if(read(fd, &num, sizeof(char)))
printf("%c", num);
}
else
{
printf("Device open failure\n");
}
}
close(fd);
}
else
{
fd = open("/dev/scullpipe",O_RDWR|O_APPEND);
int jishu=0;
while(1)//receive data
{
if (fd != -1 )
{
//printf("Please input the num written to scull\n");
scanf("%c", &num);
write(fd, &num, sizeof(char));
//printf("The scull is %d\n", num);
}
else
{
printf("Device open failure\n");
}
}
close(fd);
}
}

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