2012-10-29 179 views
1

我试图熟悉kthreads并编写了一个非常简单的程序来测试它在C中的指导从:http://tuxthink.blogspot.com/2011/02/kernel-thread-creation-1.html。我在MacOSX上的VMware上运行Ubuntu。致命错误:linux/kthread.h:没有这样的文件或目录编译终止

#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) 

如何解决这个任何想法将是非常非常感谢!

+1

'sudo apt-get install linux-headers'或者一个类似名字的包 – 2012-10-29 20:55:39

+1

@ H2CO3我不认为这会解决问题,因为我认为这是用户空间代码。 – iabdalkader

回答

1

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

+0

谢谢!该指南很有用。不幸的是,我在作者使用的建议Makefile中遇到了一些问题,但我原来遵循的指南中的Makefile工作正常。 – Skeppet

相关问题