实验题目:字符设备驱动程序实验
实验目的:
问题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);
}
}