快速业务通道

linux内核线程的创建及在QEMU上的测试方法

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-04-13
v, devno, 1);
/* Fail gracefully if need be */
if (err)
printk (KERN_NOTICE "Error %d adding kthread%d", err, minor);
}

static struct file_operations kthread_remap_ops = {
.owner = THIS_MODULE,
.open = kthread_open,
.release = kthread_release,
.read = kthread_read,
.write = kthread_write,
.ioctl = kthread_ioctl,
};

static int kthread_fun(void * arg) //内核线程运行函数
{
while (!kthread_should_stop()) {
spin_lock(&kthread_dev_obj->queue_lock);
if(kthread_dev_obj->data_size){
spin_unlock(&kthread_dev_obj->queue_lock);
kthread_dev_obj->data[kthread_dev_obj->data_size]='''';
printk(kthread_dev_obj->data);//打印出用户空间数据
printk("in kthreadn");
kthread_dev_obj->data_size=0;
}
else{
set_current_state(TASK_INTERRUPTIBLE);
spin_unlock(&kthread_dev_obj->queue_lock);
schedule();
}
}
return 0;
}

static int kthread_init(void)
{
int result;
dev_t dev = MKDEV(kthread_major, 0);

/* Figure out our device number. */
if (kthread_major)
result = register_chrdev_region(dev, 1, "kthread");
else {
result = alloc_chrdev_region(&dev, 0, 1, "kthread");
kthread_major = MAJOR(dev);
}
if (result < 0) {
printk(KERN_WARNING "kthread: unable to get major %dn", kthread_major);
return result;
}
if (kthread_major == 0)
kthread_major = result;

kthread_dev_obj= kmalloc(sizeof(struct kthread_dev), GFP_KERNEL);
kthread_setup_cdev(&kthread_dev_obj->cdev, 0,&kthread_remap_ops);
printk("kthread device installed, with major %dn", kthread_major);
my_class= class_create(THIS_MODULE, "kthread");
// device_create(my_class, NULL, MKDEV(kthread_major, 0),NULL, "kthread");
device_create(my_class, NULL, MKDEV(kthread_major, 0), "kthread");//for
// 2.6.22
kthread_dev_obj->name="kthreadtest";//内核线程的名称
spin_lock_init(&kthread_dev_obj->queue_lock);

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号