免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2422 | 回复: 4
打印 上一主题 下一主题

为什么这么简单的程序会卡住呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-22 10:54 |只看该作者 |倒序浏览
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>

MODULE_LICENSE("Dual BSD/GPL");

dev_t t_iDevNo = 0;
struct cdev *t_pDev = NULL;

static ssize_t Read(struct file *pFile, char __user *pUsr, size_t count, loff_t *pLoff)
{
        printk("Read in myDev.\n");
        return 0;
}

static ssize_t Write(struct file *pFile, const char __user *pUsr, size_t count, loff_t *pLoff)
{
        printk("Write in myDev.\n");
        return 0;
}

static int Open(struct inode *inode, struct file *file)
{
        printk("Open in myDev.\n");
        return 0;
}

static int Release(struct inode *inode, struct file *file)
{
        printk("Release in myDev.\n");
        return 0;
}

static int __init MyDriverInit(void)
{
        int iRet = 0;
        int iDevMajor = 0;
        struct file_operations fileOps = {
                .owner = THIS_MODULE,
                .read = Read,
                .write = Write,
                .open = Open,
                .release = Release
        };

        if(iDevMajor > 0)        // 如果预设了有效值
        {
                t_iDevNo = MKDEV(iDevMajor, 0);
                iRet = register_chrdev_region(t_iDevNo, 1, "MyDrv");        // 静态申请
        }
        else
        {
                iRet = alloc_chrdev_region(&t_iDevNo, 0, 1, "MyDrv");        // 动态申请
        }

        if(iRet >= 0)
        {
                t_pDev = cdev_alloc();
                if(t_pDev != NULL)
                {
                        kobject_set_name(&t_pDev->kobj, "MyDrv");
                        t_pDev->owner = THIS_MODULE;
                        t_pDev->ops = &fileOps;
                        t_pDev->dev = t_iDevNo;
                        t_pDev->count = 1;
                        cdev_init(t_pDev, t_pDev->ops);
                        iRet = cdev_add(t_pDev, t_iDevNo, 1);
                        if(iRet != 0)
                        {
                                printk(KERN_NOTICE"cdev_add fail.\n");       
                                unregister_chrdev_region(t_iDevNo, 1);
                        }
                }
                else
                {
                        printk(KERN_NOTICE"kmalloc fail.\n");
                        unregister_chrdev_region(t_iDevNo, 1);
                }
        }
        else
        {
                printk(KERN_INFO"Can't get device number.\n");
        }

        printk(KERN_ALERT "Insert my driver.\n");
        return iRet;
}

static void __exit MyDriverExit(void)
{
        cdev_del(t_pDev);
        unregister_chrdev_region(t_iDevNo, 1);
        printk(KERN_ALERT "Remove my dirver.\n");
}

module_init(MyDriverInit);

module_exit(MyDriverExit);
编译后insmod MyDriver.ko,在/proc/devices/里看到是253,然后mknod /dev/MyDrv0 c 253 0
应用程序代码就
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
        int iDevId = open("/dev/MyDrv0", O_RDWR);
        if(iDevId > 0)
        {
                close(iDevId);
        }
        else
        {
                printf("Can't open /dev/MyDrv0.\n");
        }

        return 0;
}
大多数时候一运行就卡住了,偶尔会执行完,但是Open()里加的打印,没见效果。
有经验的人士请不吝指点下,哪儿写错了?

论坛徽章:
0
2 [报告]
发表于 2012-09-26 13:49 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2012-09-27 16:29 |只看该作者
这样子做驱动???不死掉才怪。模块加载完,fileOps 就释放了

论坛徽章:
0
4 [报告]
发表于 2012-09-29 15:44 |只看该作者
这个问题,居然能困住我很久很久,还是心态没摆正,忽视了细节。多谢stephen_du,leon_yu二位大侠指导!

论坛徽章:
0
5 [报告]
发表于 2012-09-30 06:54 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP