2

我正在尝试熟悉 kthreads 并编写了一个非常简单的程序来在 C 中对其进行测试,指导来自:http ://tuxthink.blogspot.com/2011/02/kernel-thread-creation-1.html 。我在 MacOSX 上的 VMware 中运行 Ubuntu。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "cycle.h"
#include <linux/kthread.h>
#include <linux/sched.h>

int main(){
    static struct task_struct *kthread;

    thread1 = kthread_create(thread_fn, NULL, "thread1");
    wake_up_process(thread1);
    kthread_stop(thread1);

    return 0;
}

当我尝试使用 gcc (gcc test5.c -o test5.out) 编译它时,我得到“致命错误:linux/kthread.h:没有这样的文件或目录编译终止。”

当我去查看 /usr/include/linux/ 时,没有 kthread.h 文件,所以这似乎是合理的。当我搜索 kthread.hi 时,在 /usr/src/linux-headers-3.2.0-31/include/linux 中找到一个,在 /usr/src/linux-headers-3.2.0-29/include/linux 中找到一个,但是在我尝试将其中一个复制到 /usr/include/linux/ 后,我不断收到错误消息:

In file included from /usr/include/linux/kthread.h:4:0,
             from test5.c:5:
/usr/include/linux/err.h:22:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’     before ‘ERR_PTR’
/usr/include/linux/err.h:27:33: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PTR_ERR’
/usr/include/linux/err.h:32:33: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’     before ‘IS_ERR’
/usr/include/linux/err.h:37:33: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘IS_ERR_OR_NULL’
/usr/include/linux/err.h:49:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ERR_CAST’
/usr/include/linux/err.h:55:32: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PTR_RET’
In file included from test5.c:5:0:
/usr/include/linux/kthread.h:7:10: error: expected declaration specifiers or ‘...’ before numeric constant
/usr/include/linux/kthread.h:7:13: error: expected declaration specifiers or ‘...’ before numeric constant
/usr/include/linux/kthread.h:58:2: error: unknown type name ‘spinlock_t’
/usr/include/linux/kthread.h:59:19: error: field ‘work_list’ has incomplete type
/usr/include/linux/kthread.h:64:19: error: field ‘node’ has incomplete type
/usr/include/linux/kthread.h:66:2: error: unknown type name ‘wait_queue_head_t’
/usr/include/linux/kthread.h:67:2: error: unknown type name ‘atomic_t’
/usr/include/linux/kthread.h:128:1: error: unknown type name ‘bool’
test5.c: In function ‘main’:
test5.c:11:2: error: ‘thread1’ undeclared (first use in this function)
test5.c:11:2: note: each undeclared identifier is reported only once for each function it appears in
test5.c:11:12: error: ‘thread_fn’ undeclared (first use in this function)

任何有关如何解决此问题的想法将不胜感激!

4

1 回答 1

2

这些是用于内核空间而不是用户空间线程的内核线程!您应该将代码更改为具有适当 Makefile 的内核模块,或者将pthreads用于用户空间线程。也许你应该从HelloWorld内核模块开始

于 2012-10-29T20:58:47.880 回答