#1: 帮看下我的kernel module为什么会挂呢
Posted on 2012-08-23 03:42:28 by 得做大侠
#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/sysproto.h>
#include <sys/sysent.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/kthread.h>
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
void* p = 0;
int flag = 0;
struct proc *newpp = NULL;
struct thread *newthr = NULL;
struct mtx g_mtx;
int thrd1 = 1;
int thrd2 = 2;
struct task_args
{
int id;
};
static void thread_fun(void* arg)
{
while(1){
mtx_lock(&g_mtx);
tsleep(&p, 0, "nomessage", 50);
printf("in thread %d\n", *(int*)arg);
mtx_unlock(&g_mtx);
tsleep(&p, 0, "nomessage", 50);
if(flag == 3){
kthread_exit();
}
}
}
/*
* The function for implementing the syscall.
*/
static int
task_ops (struct thread *td, struct task_args *arg)
{
int result = 0;
printf("task id:%d\n", arg->id);
switch(arg->id){
case 1:
/*create thread 1*/
result = kthread_add(thread_fun, &thrd1, NULL, NULL, 0, 0,
"thread1");
if(result != 0){
printf("create thread failed\n");
}else{
printf("new thread1 created\n");
}
break;
case 2:
/*create thread 2*/
result = kthread_add(thread_fun, &thrd2, NULL, NULL, 0, 0,
"thread2");
if(result != 0){
printf("create thread failed\n");
}else{
printf("new thread2 created\n");
}
break;
case 3:
/*stop the thread*/
flag = 3;
break;
default:
break;
}
return 0;
}
/*
* The `sysent' for the new syscall
*/
static struct sysent my_sysent[2] = {
{
1, /* sy_narg */
(void*)task_ops /* sy_call */
},
};
/*
* The offset in sysent where the syscall is allocated.
*/
#if 0
static int offset[] = {NO_SYSCALL,NO_SYSCALL};
#endif
static int offset[] = {212};
char module_name[]= "task module";
/*
* The function called at load/unload.
*/
static int
load (struct module *module, int cmd, void *arg)
{
int error = 0;
switch (cmd) {
case MOD_LOAD :
printf ("module loaded\n");
mtx_init(&g_mtx, "testmutex", NULL, MTX_DEF);
break;
case MOD_UNLOAD :
printf ("module unloaded\n");
break;
default :
error = EOPNOTSUPP;
break;
}
return error;
}
SYSCALL_MODULE(mymodule, &offset[0], &my_sysent[0], load,
(void*)module_name);
ç³»ç»è°ç¨çå½¢å¼å¯¹æ¾å¼æ¾
./call 1 æ§è¡æ²¡æé®é¢
./call 2 åç³»ç»å°±æäº
å æ å¦ä¸å¾
http://hiphotos.baidu.com/abonege/pic/item/f942ab4bd11373f04953ac04a40f4bfbf
bed043a.jpg
--
[m[37mâ» æ¥æº:ã»æ°´æ¨ç¤¾åº http://newsmth.netã»[m
Report this message |