青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 297,  comments - 15,  trackbacks - 0
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(&current->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


posted on 2011-03-22 21:08 chatler 閱讀(706) 評(píng)論(0)  編輯 收藏 引用 所屬分類: linux kernel
<2010年3月>
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用鏈接

留言簿(10)

隨筆分類(307)

隨筆檔案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感覺(jué)這個(gè)博客還是不錯(cuò),雖然做的東西和我不大相關(guān),覺(jué)得看看還是有好處的

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

搜索

  •  

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国产精品精华液2区45| 欧美呦呦网站| 国产欧美日韩免费| 好吊视频一区二区三区四区| 日韩视频第一页| 久久这里只有精品视频首页| 亚洲精品网址在线观看| 久久精品视频免费| 国产精品久久久久久av下载红粉| 亚洲在线一区| 欧美va亚洲va香蕉在线| 性18欧美另类| 国产精品影音先锋| 亚洲国产婷婷| 狂野欧美激情性xxxx欧美| 亚洲在线成人| 国产精品美女xx| 一区二区三区 在线观看视| 欧美xx视频| 欧美视频免费在线| 日韩午夜激情av| 亚洲高清视频的网址| 日韩一区二区电影网| 日韩视频免费观看高清在线视频| 免费久久99精品国产自| 亚洲综合精品一区二区| 国产精品久久午夜夜伦鲁鲁| 久久亚洲免费| 国产精品v日韩精品v欧美精品网站| 久久中文字幕一区| 国产综合网站| 免费成人av在线看| 欧美体内she精视频| 蜜桃av噜噜一区| 欧美日韩四区| 亚洲一区欧美激情| 蜜臀av国产精品久久久久| 亚洲人久久久| 99国产麻豆精品| 午夜精品国产更新| 午夜精品免费| 欧美电影免费| 亚洲天堂av图片| 亚洲主播在线观看| 99视频在线精品国自产拍免费观看| 亚洲精品护士| 亚洲精品视频在线| 亚洲国产福利在线| 欧美一级片久久久久久久| 亚洲国产精品美女| 欧美制服丝袜| 亚洲激情欧美| 久久视频在线免费观看| 亚洲国产网站| 久久尤物电影视频在线观看| 久久成人18免费网站| 另类激情亚洲| 免费视频一区| 一区三区视频| 一本色道久久| 在线观看日产精品| 一本色道久久综合精品竹菊| 亚洲免费观看在线视频| 欧美国内亚洲| 久久不射电影网| 国产精品一区二区久久精品 | 国产精品99久久久久久www| 欧美日韩国产综合视频在线观看| 激情国产一区二区| 欧美亚洲免费在线| 久久久久久久综合色一本| 欧美大片免费观看在线观看网站推荐| 亚洲高清不卡一区| 久久夜色精品国产噜噜av| 欧美成人久久| 国产精品www网站| 一本色道久久综合亚洲精品不| 国产日韩精品一区观看| 亚洲激情一区二区| 国产精品午夜久久| 亚洲欧洲午夜| 亚洲午夜久久久久久尤物 | 亚洲国产黄色| 噜噜噜噜噜久久久久久91| 亚洲国产成人tv| 亚洲一区二区三| 国产一区二区日韩| 亚洲一区二区三区在线| 欧美在线视频在线播放完整版免费观看| 亚洲欧美在线免费| 99国产精品视频免费观看| 欧美久久九九| 亚洲国产99| 小处雏高清一区二区三区| 欧美日韩在线一区二区三区| 亚洲综合国产| 亚洲国产精品电影在线观看| 亚洲国产精品精华液网站| 欧美日韩国产综合一区二区| 欧美一区二区免费视频| 亚洲高清自拍| 欧美一级视频精品观看| 亚洲区欧美区| 亚洲欧洲视频在线| 欧美大片网址| 亚洲午夜精品久久久久久app| 欧美插天视频在线播放| 99精品国产一区二区青青牛奶| 亚洲制服av| 欧美福利电影网| 欧美亚洲一区二区三区| 日韩视频在线免费| 一区二区视频免费完整版观看| 夜夜嗨av色综合久久久综合网 | 亚洲一区欧美二区| 欧美大胆人体视频| 久久国产精品久久久久久久久久| 一区二区三区视频在线| 久久精品亚洲一区| 国产精品久久久久久久久久久久| 欧美电影免费观看高清| 亚洲性图久久| 日韩亚洲国产精品| 精品粉嫩aⅴ一区二区三区四区| 欧美一级片一区| 在线亚洲电影| 亚洲欧洲在线一区| 亚洲盗摄视频| 免费人成网站在线观看欧美高清 | 欧美国产1区2区| 欧美在线视屏| 欧美一区二区视频免费观看| 一区二区国产精品| 亚洲人人精品| 亚洲欧美第一页| 韩国av一区二区三区在线观看 | 亚洲国产视频直播| 亚洲欧洲日本mm| 黄色精品网站| 激情懂色av一区av二区av| 国产欧美精品国产国产专区| 国产精品久久97| 国产精品久久久久一区二区| 欧美午夜视频在线| 欧美日韩国产999| 欧美日韩一区不卡| 欧美亚日韩国产aⅴ精品中极品| 亚洲无亚洲人成网站77777| 欧美在线视频免费播放| 久久电影一区| 久久天天狠狠| 欧美电影在线| 亚洲激情黄色| 99re6这里只有精品| 一本久久综合亚洲鲁鲁五月天| 欧美在线3区| 久久久www成人免费毛片麻豆| 亚洲承认在线| 国产精品成人免费视频| 国产精品女人久久久久久| 国产婷婷成人久久av免费高清| 久久久国产成人精品| 久久亚洲一区| 欧美精品一区在线发布| 国产精品福利av| 激情校园亚洲| 99国内精品久久久久久久软件| 国产精品午夜国产小视频| 国语自产精品视频在线看8查询8| 欧美电影在线观看完整版| 国产精品爽爽爽| 欧美.www| 欧美一区二区三区免费视| 久久精品水蜜桃av综合天堂| 美日韩精品视频免费看| 国产精品视频精品| 在线观看日韩国产| 亚洲手机在线| 欧美va亚洲va日韩∨a综合色| 亚洲一区影院| 久久综合伊人77777麻豆| 亚洲日本精品国产第一区| 午夜视频一区| 免费亚洲电影| 国产亚洲欧美在线| 一区二区不卡在线视频 午夜欧美不卡在 | 久久久精品一区二区三区| 亚洲黄色天堂| 午夜在线电影亚洲一区| 欧美国产综合| 一区久久精品| 欧美一区二区三区的| 亚洲精品一区二区三区婷婷月| 美女精品一区| 亚洲网友自拍| 欧美日韩成人一区| 亚洲第一区中文99精品| 久久精品亚洲热| 91久久精品一区二区三区| 久久久久久成人|