2008 May 15th Thursday (五月 十五日 木曜日)

本文介绍了一个简单的Linux内核模块,该模块使用Proc文件系统来实现读写操作,并通过队列同步多个进程对同一Proc文件的访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Block processing. #include <linux/module.h> /*Specifically,a module*/ #include <linux/kernel.h> /*We're doing kernel work*/ #include <linux/proc_fs.h> /*Necessary because we use the proc fs*/ #include <linux/sched.h> /*For putting processes to sleep and waking them up*/ #include <asm/uaccess.h> /*for get_user and put_user*/ #define MESSAGE_LENGTH 80 static char Message[MESSAGE_LENGTH]; static struct proc_dir_entry *Our_Proc_File; #define PROC_ENTRY_FILENAME "sleep" /* * Since we use the file operations struct, we can't use the special proc * output provisions - we have to use a standard read function, which is this * function */ static ssize_t module_output(struct file*file, /* see include/linux/fs.h */     char*buf, /*The buffer to put data to(in the user segment) */     size_t len, /*The length of the buffer*/     loff_t *offset) {     static int finished=0;     int i;     char message[MESSAGE_LENGTH+30];     /*     * Return 0 to signify end of file - that we have nothing     * more to say at this point.     */     if(finished){         finished = 0;         return 0;     }         /*     * If you don't understand this by now, you're hopeless as a kernel     * programmer.     */     sprintf(message,"Lastinput:%s/n",Message);     for(i=0; i<len&&message[i]; i++)         put_user(message[i],buf+i);     finished=1;     returni; /*Return the number of bytes "read"*/ } static ssize_t module_input(struct file *filp, const char *buff,size_t len,loff_t *off){     inti;     /*     * Put the input into Message, where module_output will later be     * able to use it     */     for(i=0;i<MESSAGE_LENGTH - 1 && i < length; i++)         get_user(Message[i],buf+i);     /*     * we want a standard, zero terminated string     */     Message[i]='/0';     /*     * We need to return the number of input characters used     */     return i; } /* * 1 if the file is currently open by somebody */ int Already_Open = 0; /* * Queue of processes who want our file */ DECLARE_WAIT_QUEUE_HEAD(WaitQ); /* * Called when the /proc file is closed */ int module_close(struct inode *inode, struct file *file) {     /*     * Set Already_Open to zero, so one of the processes in the WaitQ will     * be able to set Already_Open back to one and to open the file.  All     * the other processes will be called when Already_Open is back to one,     * so they'll go back to sleep.     */     Already_Open=0;     /*     * Wake up all the processes in WaitQ, so if anybody is waiting for the     * file, they can have it.     */     wake_up(&WaitQ);     module_put(THIS_MODULE);     return 0; /*success*/ } /* * This function decides whether to allow an operation (return zero) or not * allow it (return a non-zero which indicates why it is not allowed). * * The operation can be one of the following values: * 0 - Execute(run the"file" - meaningless in our case) * 2 - Write(input to the kernel module) * 4 - Read(output from the kernel module) * * This is the real function that checks file permissions. The permissions * returned by ls -l are for reference only, and can be overridden here. */ static int module_permission(struct inode *inode, int op, struct nameidata *nd) {     /*     * We allow everybody to read from our module, but only root (uid 0)     * may write to it     */     if(op==4||(op==2&&current->euid==0))         return 0;     return -EACCES; } staticstructfile_operationsFile_Ops_4_Our_Proc_File={     .read=module_output, /*"read" from the file*/     .write=module_input, /*"write" to the file*/     .open=module_open, /*called when the/proc file is opened*/     .release=module_close, /*called when it's closed*/ }; static struct inode_operationsInode_Ops_4_Our_Proc_File={     .permission=module_permission, /*check for permissions*/ }; int init_module(){     int rv = 0;         Our_Proc_File = create_proc_entry("test", 0644, NULL);         printk(KERN_INFO "Trying to create /proc/test:/n");         if (Our_Proc_File == NULL){         rv= -ENOMEM;         remove_proc_entry("test", &proc_root);         printk(KERN_INFO "Error: Could not initialize /proc/test/n");     }     else{         Our_Proc_File->read_proc = procfile_read;         Our_Proc_File->owner = THIS_MODULE;         Our_Proc_File->mode = S_IFREG | S_IRUGO;         Our_Proc_File->uid = 0;         Our_Proc_File->gid = 0;         Our_Proc_File->size = 37;                 printk(KERN_INFO "Success!/n");     }         return rv; } void cleanup_module(){     remove_proc_entry("test", &proc_root);     printk(KERN_INFO "/proc/test removed/n"); }
一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值