⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 my_dev.c

📁 操作系统课程设计 有进程同步问题 内存管理 设备管理
💻 C
字号:
#ifndef __KERNEL__#define __KERNEL__#endif#ifndef MODULE#define MODULE#endif#define __NO_VERSION__#include <linux/kernel.h>#include <linux/module.h>#if CONFIG_MODVERSIONS == 1#define MODVERSIONS#include <linux/modversions.h>#endif#include <linux/fs.h>#ifndef KERNEL_VERSION#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))#endif/* Conditional compilation. LINUX_VERSION_CODE is  * the code (as per KERNEL_VERSION) of this version. */#if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,0)#include <asm/uaccess.h>  /* for put_user */#endif#define SUCCESS 0#define DEVICE_NAME "kueng_char_dev"#define BUF_LEN 50static  int Device_Open =0;static char Message[BUF_LEN];static int Major;static int mydev_open(struct inode *inode,struct file *file){  static int counter = 0;   if(Device_Open)    return -EBUSY;    Device_Open=1;      MOD_INC_USE_COUNT;  return 0;}static int mydev_release(struct inode *inode,struct file *file){  Device_Open=0;  MOD_DEC_USE_COUNT;    return 0;}static ssize_t mydev_read(struct file *file,char *buffer, size_t length ,loff_t *f_pos){  int bytes_read=0;if(verify_area(VERIFY_WRITE,buffer,length)==-EFAULT)    return -EFAULT;    copy_to_user(buffer,Message,length);  return bytes_read;  }static ssize_t mydev_write(struct file *file, const char *buffer,size_t length,loff_t *f_pos){  int i = BUF_LEN>length?BUF_LEN:length;if(verify_area(VERIFY_READ,buffer,length)==-EFAULT)    return -EFAULT;    copy_from_user(Message,buffer,i);  return length;}struct  file_operations  Fops = {   release: mydev_release,  open: mydev_open,  read: mydev_read,  write:mydev_write};int init_module(void){  Major = register_chrdev(0,DEVICE_NAME,&Fops);  if(Major<0){    printk("%s device failed with %d\n","Sorry, registering the character",Major);    return Major;  }    printk("%s The major device number is %d\n","Registeration is a success.",Major);  return 0;}void cleanup_module(void){  int ret;  ret = unregister_chrdev(Major,DEVICE_NAME);  if(ret<0)    printk("Error in unregister_chrdev: %d\n",ret);}MODULE_LICENSE("GPL");MODULE_AUTHOR("KUENG");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -