2

我通过一本书介绍了我在驱动程序编程中的自我。

这本书是关于使用 2.6 内核进行 Linux 驱动程序编程的。但是现在我遇到了一个问题。我从书中复制了以下代码。并试图理解它。到现在为止还挺好。

#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/completion.h>


static int thread_id=0;
static wait_queue_head_t wq;
static DECLARE_COMPLETION( on_exit );
static int thread_code( void *data )
{
    unsigned long timeout;
    int i;
    daemonize("MyKThread");
    allow_signal( SIGTERM );
    for( i=0; i<10; i++ ) {
        timeout=HZ; // wait 1 second
        timeout=wait_event_interruptible_timeout(
            wq, (timeout==0), timeout);
        printk("thread_code: woke up ...\n");
        if( timeout==-ERESTARTSYS ) {
            printk("got signal, break\n");
            break;
        }
    }
    thread_id = 0;
    complete_and_exit( &on_exit, 0 );
}
static int __init kthread_init(void)
{
    init_waitqueue_head(&wq);
    thread_id=kernel_thread(thread_code, NULL, CLONE_KERNEL );
    printk("thread_id %u",thread_id);
    if( thread_id==0 )
        return -EIO;
    return 0;
}
static void __exit kthread_exit(void)
{

printk("thread_code: exit ...\n");
printk("thread_id %u\n",thread_id);
    if( thread_id )
    {
        kill_pid( thread_id, SIGTERM, 1 );//kill_proc --> kill_pid
    }
    wait_for_completion( &on_exit );

}
module_init( kthread_init );
module_exit( kthread_exit );
MODULE_LICENSE("GPL");

起初我用 kill_pid 替换了 kill_proc。

但是,如果我运行程序并提示 rmmod ...我会收到以下消息:

thread_code: exit ...
thread_id 995
Unable to handle kernel paging request for data at address 0x000003eb
Faulting instruction address: 0xc0047998
Oops: Kernel access of bad area, sig: 11 [#1]
SBC8548
last sysfs file: 
Modules linked in: bsp_6_9(-)
NIP: c0047998 LR: c0040540 CTR: c0040578
REGS: cf987de0 TRAP: 0300   Not tainted  (2.6.36)
MSR: 00029000 <EE,ME,CE>  CR: 40000424  XER: 00000000
DEAR: 000003eb, ESR: 00000000
TASK = cf9fcdb0[997] 'rmmod' THREAD: cf986000
GPR00: 00000000 cf987e90 cf9fcdb0 000003e3 00000000 000003e3 c015b6c8 000023fc 
GPR08: 00000034 000003e3 000023fc c0040578 20000422 1004a180 00000240 00000000 
GPR16: 101042f0 1009ea94 100f0000 10043008 00000000 bf91a110 10001bcc bf91a11c 
GPR24: bf91a118 00000000 c02cca7c c02cca88 0000000f 00000001 000003e3 fffffffd 
NIP [c0047998] pid_task+0x18/0x38
LR [c0040540] kill_pid_info+0x2c/0x64
Call Trace:
[cf987e90] [c015b6c8] serial8250_console_putchar+0x0/0x4c (unreliable)
[cf987eb0] [d102a16c] kthread_exit+0x50/0x124 [bsp_6_9]
[cf987ed0] [c006026c] sys_delete_module+0x174/0x210
[cf987f40] [c000dae0] ret_from_syscall+0x0/0x3c
--- Exception: c01 at 0xff3b2bc
    LR = 0x10000f80
Instruction dump:
8127000c 90890004 3c000020 60000200 90030010 4e800020 2c030000 4082000c 
38600000 4e800020 5480103a 7d230214 <80690008> 2f830000 41beffe8 54892036 
---[ end trace d2ac023715aedf88 ]---
Segmentation fault
-bash-3.2# 

Message from syslogd@ at Thu Jan  1 01:00:10 1970 ...

MPC_Embedded_Linux kernel: last sysfs file: thread_code: woke up ...

可能 send kill_pid 顺序不对,但是我没有找到解决办法。

4

1 回答 1

3

kill_pid将 astruct pid *作为第一个参数,而不是 a pid_t(即,它采用指向进程描述符结构的指针,而不是进程 ID)。struct pid *您可以使用类似的功能查找find_vpid

于 2012-08-28T06:50:10.773 回答