[英]Linux Kernel Threads with semaphore freeze
我有兩個內核線程,我試圖以替代方式從這兩個線程中打印。 我正在使用信號量來同步這兩個線程。
int kthread_1_function(void *data)
{
while(1)
{
down(&lock);
pr_alert("In Kernel Thread 1!!!\n");
up(&lock);
//kthread_should_stop must be used to check if this thread should be stopped
if(kthread_should_stop())
return 1;
}
return 0;
}
同樣,我還有一個名為kthread_2_function的線程。 在init模塊中,我創建了這些內核線程( kthread_run
),並且cleanup_module停止了這些線程( kthread_stop
)。
以下是我對嘗試過的各種事情的觀察。
msleep(1000)
或mdelay(1000)
時,都會看到備用打印。 msleep(1000)
時,打印不會交替顯示。 mdelay(1000)
,我會看到備用打印,但是當我刪除模塊時,整個系統將凍結 有人可以幫我理解上述觀點嗎?
單個信號量不會給您提供交錯保證。 您需要兩個信號量才能執行交織。 例如
kthread_1_function:
down(&lock1);
pr_alert("In Kernel Thread 1!!!\n");
up(&lock2);
kthread_2_function:
down(&lock2);
pr_alert("In Kernel Thread 2!!!\n");
up(&lock1);
而且,在卸載模塊之前,需要先解除阻塞信號量,以使線程正常關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.