编写一个可读写的字符驱动程序(读写数据都基于驱动程序中的一个整形全局变量),并且编写一个应用程序进行测试。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
char buf[100];
fd=open("/dev/m_kernel",O_RDWR,S_IRUSR|S_IWUSR);
if(fd != -1)
{
while(1)
{
char num[101]={0};
read(fd,&num,sizeof(int));
printf("\nThe globalvar is %s\n",num);
printf("Please input the num written to globalvar\n");
scanf("%s",num);
write(fd,&num,sizeof(num));
read(fd,&num,sizeof(num));
char *k=num;
printf("The globalvar is %s\n",num);
}
close(fd);
}
else
{
printf("Device open failure\n");
}
}
//参考Linux 程序设计第三版(金国庆)