首页 字符设备实例

字符设备实例

举报
开通vip

字符设备实例字符设备实例 字符设备实例 字符设备驱动程序:设计两个终端设备文件实现一个字符设备驱动程序,使一对进程之间利用该字符设备驱动程序能互相传递可变长度的信息。 要求:使用终端文件的基本操作,如init(),open(),release(),read(),write(),ioctl()。 /**************************************************************/ /***** ch...

字符设备实例
字符设备实例 字符设备实例 字符设备驱动程序:设计两个终端设备文件实现一个字符设备驱动程序,使一对进程之间利用该字符设备驱动程序能互相传递可变长度的信息。 要求:使用终端文件的基本操作,如init(),open(),release(),read(),write(),ioctl()。 /**************************************************************/ /***** chardev.c *****/ /**************************************************************/ #ifndef __KERNEL__ # define __KERNEL__ #endif #ifndef MODULE # define MODULE #endif #define WE_REALLY_WANT_TO_USE_A_BROKEN_INTERFACE #define __NO_VERSION__ #include #include char kernel_version[]=UTS_RELEASE; #include #include #include #include #include #include #include #include #include #include #include #include #include "chardev.h" #define DEVICE_NAME "dynchar" static int usage,new_msg; // control flags static char *data; // open the device static int dynchar_open(struct inode *inode,struct file *filp) { MOD_INC_USE_COUNT; printk("This chrdev is in open!\n"); return 0; } // close the device static int dynchar_release(struct inode *inode,struct file *filp) { MOD_DEC_USE_COUNT; printk("This chrdev is in release!\n"); return 0; } // write to device -transfer from user space to kernel space static ssize_t dynchar_write(struct file *filp,const char *buf, size_t count,loff_t *offset) { if(count<0) return -EINVAL; if(usage || new_msg) return -EBUSY; usage=1; kfree(data); data=kmalloc(sizeof(char)*(count+1),GFP_KERNEL); if(!data) { return -ENOMEM; } copy_from_user(data,buf,count+1); // start transfering usage=0; new_msg=1; return count; } // read from device -transfer from kernel space to user space static ssize_t dynchar_read(struct file *filp,char *buf,size_t count, loff_t *offset) { int length; if(count<0) return -EINVAL; if(usage) return -EBUSY; usage=1; if(data==NULL) return 0; length=strlen(data); if(length #define DYNCHAR_MAJOR 42 #define DYNCHAR_MAGIC DYNCHAR_MAJOR #define DYNCHAR_RESET _IO(DYNCHAR_MAGIC,0) // reset the data #define DYNCHAR_QUERY_NEW_MSG _IO(DYNCHAR_MAGIC,1) // check for new message #define DYNCHAR_QUERY_MSG_LENGTH _IO(DYNCHAR_MAGIC,2) // get message length #define IOC_NEW_MSG 1 #endif /************************************************************/ /***** testproc.c, this is a test program for chardev.c *****/ /************************************************************/ #include #include #include #include #include #include #include #include #include #include "chardev.h" void write_proc(void); void read_proc(void); main(int argc,char **argv) { if(argc==1) { puts("syntax: testprog[write|read]!"); exit(0); } if(!strcmp(argv[1],"write")) { write_porc(); } else if(!strcmp(argv[1],"read")) { read_proc(); } else { puts("testprog: invalid command!"); } return 0; } void write_proc() { int fd,len,quit=0; int dbg; char buf[100]; fd=open("cdev0",O_WRONLY); if(fd<=0) { printf("Error opening device for writing!\n"); exit(1); } while(!quit) { printf("\n write>>"); gets(buf); if(!strcmp(buf,"exit")) quit=1; while(ioctl(fd,DYNCHAR_QUERY_NEW_MSG)) usleep(1000); len=write(fd,buf,strlen(buf)); if(len<0) { ; printf("Error writing to device!\n"); close(fd); exit(1); } printf("%d bytes written to device!\n",len); } close(fd); } void read_proc() { int fd,len,quit=0; char *buf=NULL; fd=open("cdev1",O_RDONLY); if(fd<0) { printf("Error opening device for reading!\n"); exit(1); } while(!quit) { printf("\n read>>"); while(!ioctl(fd,DYNCHAR_QUERY_NEW_MSG)) usleep(1000); // get the msg length len=ioctl(fd,DYNCHAR_QUERY_MSG_LENGTH,NULL); if(len) { if(buf!=NULL) free(buf); buf=malloc(sizeof(char)*(len+1)); len=read(fd,buf,len); if(len<0) { printf("Error reading from device!"); } else { if(!strcmp(buf,"exit")) { ; ioctl(fd,DYNCHAR_RESET); // reset quit=1; } else printf("%s\n",buf); } } } free(buf); close(fd); } - 4 -
本文档为【字符设备实例】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_380370
暂无简介~
格式:doc
大小:45KB
软件:Word
页数:7
分类:互联网
上传时间:2018-09-11
浏览量:61