kthread를 이용한 thread 생성 test 한 source
샘플 코드가 별로 없어서..
참조해서 구현 했음 좋겠다.
[kthread1.c]
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/wait.h>
#include <linux/kthread.h>
#include <asm/io.h>
struct task_struct *ts;
int thread1(void *data)
{
int count =0;
while(1)
{
count++;
printk(KERN_ALERT "Hi!! I am kernel thread1[%d]!\n", count );
msleep(1000);
if (kthread_should_stop())
break;
}
return 0;
}
int init_module(void)
{
printk(KERN_INFO "init_module() called thread1\n");
ts=kthread_run(thread1,NULL,"kthread");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "cleanup_module() called thread1\n");
kthread_stop(ts);
}
#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/wait.h>
#include <linux/kthread.h>
#include <asm/io.h>
struct task_struct *ts;
int thread1(void *data)
{
int count =0;
while(1)
{
count++;
printk(KERN_ALERT "Hi!! I am kernel thread1[%d]!\n", count );
msleep(1000);
if (kthread_should_stop())
break;
}
return 0;
}
int init_module(void)
{
printk(KERN_INFO "init_module() called thread1\n");
ts=kthread_run(thread1,NULL,"kthread");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "cleanup_module() called thread1\n");
kthread_stop(ts);
}
[Makefile]
obj-m += kthread1.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
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
정말 간단하게. 짠 걸로
kernel에서
$ insmod kthread1.ko
를 하면, 무한으로 count txt를 찍도록 한 thread
dmesg를 이용하여 확인 가능하다.