我正在尝试熟悉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)
非常感谢任何解决这个问题的想法!
答案 0 :(得分:2)
那些内核线程意味着要在内核空间而不是用户空间线程中使用!您应该使用正确的Makefile将代码更改为内核模块,或者对用户空间线程使用pthreads。也许你应该从HelloWorld内核模块
开始