Linux Kernel Threads in Device Drivers
Purpose
This examples shows how to create and stop a kernel thread.
The driver is implemented as a loadable module. In the init_module() routine five kernel threads are created. This kernel threads sleep one second, wake up, print a message and fall asleep again. On unload of the module (cleanup_module), the kernel threads are killed.
The example has been tested with Linux kernel 2.4.2 on Intel (uni processor only) and Alpha platform (COMPAQ Personal Workstation 500au (uni processor), DS20 and ES40 (SMP).
A version for the 2.2 kernel can be found here. Note: depending on the context of the creator of the threads the new threads may inherit properties from the parent you do not want to have. The new version avoids this by having keventd create the threads. The 2.2. kernel do not have a keventd, so this approach is not implementable there.
Functions in example
start_kthread: creates a new kernel thread. Can be called from any process context but not from interrupt. The functions blocks until the thread started.
stop_kthread: stop the thread. Can be called from any process context but the thread to be terminated. Cannot be called from interrupt context. The function blocks until the thread terminated.
init_kthread: sets the environment of the new threads. Is to be called out of the created thread.
exit_kthread: needs to be called by the thread to be terminated on exit
Creation of new Thread
A new thread is created with kernel_thread(). The thread inherits properties from its parents. To make sure that we do not get any weired properties, we let keventd create the new thread.
The new thread is created with start_kthread(). It uses a semaphore to block until the new thread is running. A down() blocks the start_kthread() routine until the corresponding up() call in init_kthread() is executed.
The new thread must call init_kthread() in order to let the creator continue.
Stop of new Thread
stop_kthread() sets a flag that the thread uses to determine whether do die or not and sends a SIGKILL to the thread. This signal causes the thread to be woken up. On wakeup it will check for the flag and then terminate itself by calling exit_kthread and returning from the thread function. With a semaphore the stop_kthread() function blocks until the thread terminated.
Initialization of new Thread
Within the new created thread, init_kthread() needs to be called. This function sets a signal mask, initialises a wait queue, the termination flag and sets a new name for the thread. With a up() call it notifies the creator that the setup is done.
Exit of new Thread
When the thread receives the notification to terminate itself, is calls the exit_kthread() function. It notifies the stop_kthread() function that it terminated with an up() call.
The new Thread itself
The new thread is implemented in the example_thread() function. It runs an endless loop (for(;;)). In the loop it falls asleep with the interruptible_sleep_on_timeout() function. It comes out of this function either when the timeout expires or when a signal got caught.
The "work" in the thread is to print out a message with printk.
Kernel Versions
The example has been tested on 2.4.2.
Example Device Driver Code
The example consists of four files: kthread.h, kthread.c, thread_drv.c and a Makefile
kthread.h
#ifndef _KTHREAD_H
#define _KTHREAD_H
#include <linux/config.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/tqueue.h>
#include <linux/wait.h>
#include <asm/unistd.h>
#include <asm/semaphore.h>
/* a structure to store all information we need
for our thread */
typedef struct kthread_struct
{
/* private data */
/* Linux task structure of thread */
struct task_struct *thread;
/* Task queue need to launch thread */
struct tq_struct tq;
/* function to be started as thread */
void (*function) (struct kthread_struct *kthread);
/* semaphore needed on start and creation of thread. */
struct semaphore startstop_sem;
/* public data */
/* queue thread is waiting on. Gets initialized by
init_kthread, can be used by thread itself.
*/
wait_queue_head_t queue;
/* flag to tell thread whether to die or not.
When the thread receives a signal, it must check
the value of terminate and call exit_kthread and terminate
if set.
*/
int terminate;
/* additional data to pass to kernel thread */
void *arg;
} kthread_t;
/* prototypes */
/* start new kthread (called by creator) */
void start_kthread(void (*func)(kthread_t *), kthread_t *kthread);
/* stop a running thread (called by "killer") */
void stop_kthread(kthread_t *kthread);
/* setup thread environment (called by new thread) */
void init_kthread(kthread_t *kthread, char *name);
/* cleanup thread environment (called by thread upon receiving termination signal) */
void exit_kthread(kthread_t *kthread);
#endif
kthread.c
#include <linux/config.h>
#include <linux/version.h>
#if defined(MODVERSIONS)
#include <linux/modversions.h>
#endif
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/tqueue.h>
#include <linux/wait.h>
#include <linux/signal.h>
#include <asm/semaphore.h>
#include <asm/smplock.h>
#include "kthread.h"
/* private functions */
static void kthread_launcher(void *data)
{
kthread_t *kthread = data;
kernel_thread((int (*)(void *))kthread->function, (void *)kthread, 0);
}
/* public functions */
/* create a new kernel thread. Called by the creator. */
void start_kthread(void (*func)(kthread_t *), kthread_t *kthread)
{
/* initialize the semaphore:
we start with the semaphore locked. The new kernel
thread will setup its stuff and unlock it. This
control flow (the one that creates the thread) blocks
in the down operation below until the thread has reached
the up() operation.
*/
init_MUTEX_LOCKED(&kthread->startstop_sem);
/* store the function to be executed in the data passed to
the launcher */
kthread->function=func;
/* create the new thread my running a task through keventd */
/* initialize the task queue structure */
kthread->tq.sync = 0;
INIT_LIST_HEAD(&kthread->tq.list);
kthread->tq.routine = kthread_launcher;
kthread->tq.data = kthread;
/* and schedule it for execution */
schedule_task(&kthread->tq);
/* wait till it has reached the setup_thread routine */
down(&kthread->startstop_sem);
}
/* stop a kernel thread. Called by the removing instance */
void stop_kthread(kthread_t *kthread)
{
if (kthread->thread == NULL)
{
printk("stop_kthread: killing non existing thread!\n");
return;
}
/* this function needs to be protected with the big
kernel lock (lock_kernel()). The lock must be
grabbed before changing the terminate
flag and released after the down() call. */
lock_kernel();
/* initialize the semaphore. We lock it here, the
leave_thread call of the thread to be terminated
will unlock it. As soon as we see the semaphore
unlocked, we know that the thread has exited.
*/
init_MUTEX_LOCKED(&kthread->startstop_sem);
/* We need to do a memory barrier here to be sure that
the flags are visible on all CPUs.
*/
mb();
/* set flag to request thread termination */
kthread->terminate = 1;
/* We need to do a memory barrier here to be sure that
the flags are visible on all CPUs.
*/
mb();
kill_proc(kthread->thread->pid, SIGKILL, 1);
/* block till thread terminated */
down(&kthread->startstop_sem);
/* release the big kernel lock */
unlock_kernel();
/* now we are sure the thread is in zombie state. We
notify keventd to clean the process up.
*/
kill_proc(2, SIGCHLD, 1);
}
/* initialize new created thread. Called by the new thread. */
void init_kthread(kthread_t *kthread, char *name)
{
/* lock the kernel. A new kernel thread starts without
the big kernel lock, regardless of the lock state
of the creator (the lock level is *not* inheritated)
*/
lock_kernel();
/* fill in thread structure */
kthread->thread = current;
/* set signal mask to what we want to respond */
siginitsetinv(¤t->blocked, sigmask(SIGKILL)|sigmask(SIGINT)|sigmask(SIGTERM));
/* initialise wait queue */
init_waitqueue_head(&kthread->queue);
/* initialise termination flag */
kthread->terminate = 0;
/* set name of this process (max 15 chars + 0 !) */
sprintf(current->comm, name);
/* let others run */
unlock_kernel();
/* tell the creator that we are ready and let him continue */
up(&kthread->startstop_sem);
}
/* cleanup of thread. Called by the exiting thread. */
void exit_kthread(kthread_t *kthread)
{
/* we are terminating */
/* lock the kernel, the exit will unlock it */
lock_kernel();
kthread->thread = NULL;
mb();
/* notify the stop_kthread() routine that we are terminating. */
up(&kthread->startstop_sem);
/* the kernel_thread that called clone() does a do_exit here. */
/* there is no race here between execution of the "killer" and real termination
of the thread (race window between up and do_exit), since both the
thread and the "killer" function are running with the kernel lock held.
The kernel lock will be freed after the thread exited, so the code
is really not executed anymore as soon as the unload functions gets
the kernel lock back.
The init process may not have made the cleanup of the process here,
but the cleanup can be done safely with the module unloaded.
*/
}
thread_drv.c
#include <linux/config.h>
#include <linux/version.h>
#include <linux/module.h>
#if defined(MODVERSIONS)
#include <linux/modversions.h>
#endif
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include "kthread.h"
#define NTHREADS 5
/* the variable that contains the thread data */
kthread_t example[NTHREADS];
/* prototype for the example thread */
static void example_thread(kthread_t *kthread);
/* load the module */
int init_module(void)
{
int i;
/* create new kernel threads */
for (i=0; i <NTHREADS; i++)
start_kthread(example_thread, &example);
return(0);
}
/* remove the module */
void cleanup_module(void)
{
int i;
/* terminate the kernel threads */
for (i=0; i<NTHREADS; i++)
stop_kthread(&example);
return;
}
/* this is the thread function that we are executing */
static void example_thread(kthread_t *kthread)
{
/* setup the thread environment */
init_kthread(kthread, "example thread");
printk("hi, here is the kernel thread\n");
/* an endless loop in which we are doing our work */
for(;;)
{
/* fall asleep for one second */
interruptible_sleep_on_timeout(&kthread->queue, HZ);
/* We need to do a memory barrier here to be sure that
the flags are visible on all CPUs.
*/
mb();
/* here we are back from sleep, either due to the timeout
(one second), or because we caught a signal.
*/
if (kthread->terminate)
{
/* we received a request to terminate ourself */
break;
}
/* this is normal work to do */
printk("example thread: thread woke up\n");
}
/* here we go only in case of termination of the thread */
/* cleanup the thread, leave */
exit_kthread(kthread);
/* returning from the thread here calls the exit functions */
}
Makefile
# set to your kernel tree
KERNEL = /usr/src/linux
# get the Linux architecture. Needed to find proper include file for CFLAGS
ARCH=$(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
# set default flags to compile module
CFLAGS = -D__KERNEL__ -DMODULE -I$(KERNEL)/include
CFLAGS+= -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing
all: thread_mod.o
# get configuration of kernel
include $(KERNEL)/.config
# modify CFLAGS with architecture specific flags
include $(KERNEL)/arch/${ARCH}/Makefile
# enable the module versions, if configured in kernel source tree
ifdef CONFIG_MODVERSIONS
CFLAGS+= -DMODVERSIONS -include $(KERNEL)/include/linux/modversions.h
endif
# enable SMP, if configured in kernel source tree
ifdef CONFIG_SMP
CFLAGS+= -D__SMP__
endif
# note: we are compiling the driver object file and then linking
# we link it into the module. With just one object file as in
# this example this is not needed. We can just load the object
# file produced by gcc
# link the thread driver module
thread_mod.o: thread_drv.o kthread.o
ld -r -o thread_mod.o thread_drv.o kthread.o
# compile the kthread object file
kthread.o: kthread.c kthread.h
gcc $(CFLAGS) -c kthread.c
# compile the thread driver
thread_drv.o: thread_drv.c kthread.h
gcc $(CFLAGS) -c thread_drv.c
clean:
rm -f *.o
Bugs
The code assumes that keventd is running with PID 2.
Comments, Corrections
Please send comments, corrections etc. to the address below.
from:
http://www.linuxforum.net/forum/showflat.php?Cat=&Board=linuxK&Number=282973&page=15&view=collapsed&sb=5&o=all
只有注冊用戶登錄后才能發表評論。 | ||
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
![]() |
||
相關文章:
|
||
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
27 | 28 | 29 | 30 | 31 | 1 | 2 | |||
3 | 4 | 5 | 6 | 7 | 8 | 9 | |||
10 | 11 | 12 | 13 | 14 | 15 | 16 | |||
17 | 18 | 19 | 20 | 21 | 22 | 23 | |||
24 | 25 | 26 | 27 | 28 | 29 | 30 | |||
31 | 1 | 2 | 3 | 4 | 5 | 6 |
常用鏈接
留言簿(10)
隨筆分類(307)
- Algorithm(22)
- apache(1)
- Assembly(1)
- browser(2)
- C++_BASIS(39)
- Compiling Theorem(1)
- CPU(1)
- cvs(3)
- Database(8)
- Designed Patterns(3)
- FileFormat(4)
- FileSystem(1)
- freebsd(1)
- Game(2)
- gdb(1)
- Gossips(12)
- GP_STL(3)
- interview(8)
- java
- life and living(1)
- linux kernel(12)
- Linux_Coding(43)
- Linux_Driver
- Linux_SysAdmin(26)
- makefile(3)
- misce(5)
- MultiCore(1)
- Network(14)
- OS(18)
- RegularExpression(1)
- schedule(1)
- SearchEngine(1)
- security(2)
- Shell(25)
- Socket(18)
- storage(2)
- Template(4)
- VC_MFC(4)
- vi(5)
- website(1)
- windows(7)
隨筆檔案(297)
- 2012年10月 (1)
- 2012年9月 (1)
- 2012年7月 (1)
- 2012年6月 (7)
- 2012年5月 (3)
- 2012年4月 (2)
- 2011年9月 (3)
- 2011年8月 (3)
- 2011年6月 (3)
- 2011年5月 (2)
- 2011年3月 (2)
- 2011年1月 (1)
- 2010年12月 (2)
- 2010年11月 (6)
- 2010年10月 (4)
- 2010年9月 (7)
- 2010年8月 (12)
- 2010年7月 (6)
- 2010年6月 (5)
- 2010年5月 (11)
- 2010年4月 (16)
- 2010年3月 (20)
- 2010年2月 (18)
- 2010年1月 (26)
- 2009年12月 (34)
- 2009年11月 (36)
- 2009年10月 (5)
- 2009年9月 (1)
- 2009年7月 (2)
- 2009年6月 (3)
- 2009年5月 (6)
- 2009年4月 (6)
- 2009年3月 (11)
- 2009年2月 (6)
- 2008年11月 (1)
- 2008年10月 (1)
- 2008年9月 (3)
- 2008年8月 (4)
- 2008年7月 (16)
algorithm
- andytan
- algorithm, linux, os, network,etc
- EXACT STRING MATCHING ALGORITHMS
- httperf -- a web perf test tool
- Java多線程
- 編程夜未眠
- 布薩空間
- 結構之法
- 沈一峰 google技術博客
- 小兵的窩
Books_Free_Online
C++
- Bjarne Stroustrup's C++ Style and Technique FAQ
- boyplayee column
- C Plus Plus
- CPP Reference
- LearnC++Website
- Welcome to Bjarne Stroustrup's homepage!
database
Linux
Linux shell
linux socket
misce
- cloudward
- 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的
network
OSS
- Google Android
- Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
- os161 file list
overall
搜索
最新評論

- 1.?re: memcached完全剖析系列教程《轉》
- mark
- --zgpxgame
- 2.?re: 用prctl給線程命名
- 評論內容較長,點擊標題查看
- --none
- 3.?re: 用prctl給線程命名
- 請問大俠: 用top命令的時候可以顯示修改后的線程名么?如何做呢?
- --dhao123@sina.com
- 4.?re: 解決Linux pthread_create內存泄漏問題
-
我試過,每一種方法有的時候不行。
第二種是可以的。
- --朱先生
- 5.?re: 著名程序庫的比較和學習經驗
- 評論內容較長,點擊標題查看
- --buy dissertation
- 6.?re: linux的消息隊列與共享內存編程
- 內容選擇得很好,謝謝
- --朱志超
- 7.?re: 著名程序庫的比較和學習經驗
- 評論內容較長,點擊標題查看
- --LillianHancock
- 8.?re: 解決Linux pthread_create內存泄漏問題[未登錄]
- 不錯,支持一個。
- --jack
- 9.?re: 淺談游戲服務器---功能模塊上來看[未登錄]
- 不錯 好文!! 期待博主繼續
- --cppexplore
- 10.?re: 全面整理的C++面試題
- 評論內容較長,點擊標題查看
- --chatler
- 11.?re: 微軟面試中簡單的算法題目(轉)
- 評論內容較長,點擊標題查看
- --chatler
- 12.?re: Browsers, processes, cookies and session state
- 每個IE Instance該是不同的進程吧,可以獲取進程ID,在每個instance里建一個名稱包含進程id的目錄名,就可以分目錄存儲了吧。
- --chatler
- 13.?re: Browsers, processes, cookies and session state
-
文章說的很清楚,多謝
我有一個問題:
如何為每個ie instance ie實例的 Persistent cookies cookie 指定不同的存儲目錄? - --domolo
- 14.?re: 從一道面試題看指針與數組的區別
- 一個字,強!
- --路過
- 15.?re: 一個關于單向鏈表的面試題
- 評論內容較長,點擊標題查看
- --chatler
閱讀排行榜
- 1.?Windows Socket五種I/O模型(8339)
- 2.?最大公約數(Gcd)兩種算法(Euclid && Stein)<轉>(5497)
- 3.?用prctl給線程命名(5056)
- 4.?Linux core dump file詳解 <轉>(4495)
- 5.?算法面試題(3400)
- 6.?64位與32位編程的數據類型區別(3235)
- 7.?解決Linux pthread_create內存泄漏問題(3126)
- 8.?NUMA與英特爾下一代Xeon處理器學習心得<轉>(2993)
- 9.?c語言抓取網頁數據(2832)
- 10.?CVSNT服務器配置——添加用戶、解決無法登陸(2735)
- 11.? pthread_join函數及linux線程(2644)
- 12.?一個基于Event Poll(epoll)的TCP Server Framework,淺析epoll(2574)
-
13.?為 C/C++ 項目構建您自己的內存管理器
(2558) - 14.?memcached完全剖析系列教程《轉》(2505)
- 15.?G++編譯選項(2404)
- 16.?STL容器 erase的使用陷井<轉載>(2193)
- 17.?epoll使用例子(2117)
- 18.?linux的消息隊列與共享內存編程(2069)
- 19.?gdb帶參數調試(2061)
- 20.?The Linux Kernel Module Programming Guide(2012)
- 21.?一個關于單向鏈表的面試題(1909)
- 22.?c中strncmp與memcmp的區別(1884)
- 23.?優化Derby數據庫技巧(1831)
- 24.?一個基于完成端口的TCP Server Framework,淺析IOCP(1758)
- 25.?自己整理的指令(1747)
- 26.?autotools制作Makefile 和configure文件(1632)
- 27.?Google C++ Style Guide(1623)
- 28.?linux系統調用函數(1621)
- 29.?An In-Depth Look into the Win32 Portable Executable File Format(1605)
- 30.? vim大小寫轉換(1582)
- 31.?淺談游戲服務器---功能模塊上來看(1552)
- 32.?教你用c實現http協議(1501)
-
33.?MIPS architecture
(1496) - 34.?Aix下查看占用端口的進程(1480)
- 35.?史上最強bash函數庫(1407)
- 36.?linux trap詳解(1341)
- 37.?ms,google,vmware,nvidia美國總部面試題(1323)
- 38.?多CPU上的原子操作(1316)
- 39.?power函數寫法《轉》(1281)
-
40.?Critical Section
(1253)
評論排行榜
- 1.?著名程序庫的比較和學習經驗(3)
- 2.?解決Linux pthread_create內存泄漏問題(2)
- 3.?用prctl給線程命名(2)
-
4.?Browsers, processes, cookies and session state
(2) - 5.?全面整理的C++面試題(1)
- 6.?linux的消息隊列與共享內存編程(1)
- 7.?一個關于單向鏈表的面試題(1)
- 8.?從一道面試題看指針與數組的區別 <轉>(1)
- 9.?淺談游戲服務器---功能模塊上來看(1)
- 10.?微軟面試中簡單的算法題目(轉)(1)
- 11.?memcached完全剖析系列教程《轉》(1)
- 12.?死鎖和活鎖 deadlock and livelock(0)
- 13.?IT公司筆試算法題(0)
- 14.?effective c++(0)
-
15.?為 C/C++ 項目構建您自己的內存管理器
(0) -
16.?Comparing Two High-Performance I/O Design Patterns
(0) - 17.?boost 庫 enable_shared_from_this 實現原理分析<轉>(0)
-
18.?Which is asymptotically larger: lg(lg* n) or lg*(lg n)$? <
>(0) - 19.?TCMalloc : Thread-Caching Malloc(0)
- 20.?有抗癌效果的神奇食物(0)
- 21.?ubuntu下編譯內核(0)
- 22.?MySQL索引背后的數據結構及算法原理(0)
- 23.?wget進行整站下載<轉>(0)
- 24.?What are the difference between DDL, DML and DCL commands?<轉載>(0)
- 25.?通過證書方式實現ssh的無密碼登陸<轉>(0)
- 26.?50個有關編程的至理名言<轉>(0)
- 27.?How I explained Design Patterns to my wife《reprint》(0)
- 28.?How to explain OOD to my wife-code project《轉載》(0)
- 29.?ifconf.c《轉載》(0)
- 30.?vim configuration(0)
- 31.?<轉>how to start a kernel thread(0)
- 32.?Linux系統性能指標介紹(0)
- 33.?Linux進程的狀態和調度(0)
- 34.?關鍵字的作用 auto static register const volatile extern (0)
- 35.?epoll使用例子(0)
- 36.?Windows Socket五種I/O模型(0)
- 37.?TCP的流量控制 (0)
- 38.?Linux下各類TCP網絡服務器的實現源代碼《轉》(0)
- 39.?fopen 文本文件與二進制文件區別(0)
- 40.?Linux目錄掃描程序(0)