我已经为内核级线程编写了以下命令:
#include <linux/module.h>
#include <linux/times.h>
#include <linux/kthread.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/time.h>
static struct task_struct *thread_st;
// Function executed by kernel thread
static int thread_fn(void *unused)
{
allow_signal(SIGKILL);
while (!kthread_should_stop())
{
printk(KERN_INFO "Thread is Running\n");
ssleep(3);
if(signal_pending(thread_st))
break;
}
printk(KERN_INFO "Thread has Stopped\n");
do_exit(0);
return 0;
}
// Module Initialization
static int __init init_thread(void)
{
struct timeval t0,t1;
double temp;
//for calculating time take to create a thread
do_gettimeofday(&t0);
printk(KERN_INFO "Creating Thread\n");
thread_st = kthread_run(thread_fn, NULL, "mythread");
do_gettimeofday(&t1);
if (thread_st){
temp = ((t1.tv_sec - t0.tv_sec)*1000000 + (t1.tv_usec - t0.tv_usec));
printk(KERN_INFO "Thread Created successfully \n Time Taken %.2f \n",temp);
}
else
printk(KERN_ERR "Thread creation failed\n");
return 0;
}
// Module Exit
static void __exit cleanup_thread(void)
{
printk(KERN_INFO "Cleaning Up\n");
if (thread_st)
{
kthread_stop(thread_st);
printk(KERN_INFO "Thread stopped");
}
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("OSHO AGYEYA");
MODULE_DESCRIPTION("kernel.threads");
module_init(init_thread);
module_exit(cleanup_thread);
Makefile如下:
obj-m += kt.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
使用make命令时出现此错误:
make -C /lib/modules/4.4.0-31-generic/build M=/home/osho/Desktop/15BCE1326_OSHO modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-31-generic'
CC [M] /home/osho/Desktop/15BCE1326_OSHO/kernelthread.o
/home/osho/Desktop/15BCE1326_OSHO/kernelthread.c: In function ‘init_thread’:
/home/osho/Desktop/15BCE1326_OSHO/kernelthread.c:44:1: error: SSE register return with SSE disabled
printk(KERN_INFO "Thread Created successfully \n Time Taken %.2f \n",temp);
^
scripts/Makefile.build:264: recipe for target '/home/osho/Desktop/15BCE1326_OSHO/kernelthread.o' failed
make[2]: *** [/home/osho/Desktop/15BCE1326_OSHO/kernelthread.o] Error 1
Makefile:1403: recipe for target '_module_/home/osho/Desktop/15BCE1326_OSHO' failed
make[1]: *** [_module_/home/osho/Desktop/15BCE1326_OSHO] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-31-generic'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
我已经尝试了所有可能的方法,但这个错误似乎没有。该怎么办?