Thread是執行的最小單位,在多核CPU上,產生的Thread可以同時的運作,這意謂著使用Thread技術可以用來改善效能,但是,每個Thread間的資料同步則是另一個課題
使用步驟:
1. kthread_create_on_node() 2. wake_up_process() 3. kthread_should_stop() 4. kthread_stop()
ldd.S
.global init_module .global cleanup_module .section .modinfo, "ae" __UNIQUE_ID_0: .asciz "license=GPL" __UNIQUE_ID_1: .asciz "author=Steward Fu" __UNIQUE_ID_2: .asciz "description=Linux Driver" .section .data kthread_name: .asciz "mykthread" kthread_msg1: .asciz "kthread_handler++\n" kthread_msg2: .asciz "kthread_handler--\n" mykthread: .dcb 4 .align 2 .section .text kthread_handler: push {lr} ldr r0, =kthread_msg1 bl printk loop: bl kthread_should_stop cmp r0, #0 bne kthread_exit mov r0, #100 bl msleep b loop kthread_exit: ldr r0, =kthread_msg2 bl printk pop {pc} init_module: push {lr} ldr r0, =kthread_handler mov r1, #0 mov r2, #0xffffffff ldr r3, =kthread_name bl kthread_create_on_node ldr r1, =mykthread str r0, [r1] bl wake_up_process mov r0, #0 pop {pc} cleanup_module: push {lr} ldr r0, =mykthread ldr r0, [r0] bl kthread_stop pop {pc} .end
init_module: 產生多個Thread並且執行
kthread_handler: 列印字串
cleanup_module: 停止Thread執行
測試
# insmod /boot/main.ko kthread_handler++ # rmmod main kthread_handler--