linux核心中的訊號機制–從使用者層到核心層

來源:互聯網
上載者:User

linux核心中的訊號機制--從使用者層到核心層

Kernel version:2.6.14

CPU architecture:ARM920T

Author:ce123(http://blog.csdn.net/ce123)

1.簡介

如果進程要處理某一訊號,那麼要在進程中註冊該訊號。註冊訊號主要用來確定訊號值及進程針對該訊號值的動作之間的映射關係,即進程將要處理哪個進程和該訊號被傳遞給進程時,將執行何種操作。主要有兩個函數實現訊號的註冊:signal()和sigaction()。

2.signal()

signal()的函數原型如下:

void (*signal(int signum, void (*handler)(int)))(int); 

在使用該調用的進程中加入以下標頭檔:

#include <signal.h> 

上述聲明格式比較複雜,如果不清楚如何使用,也可以通過下面這種類型定義的格式來使用(POSIX的定義): 

typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); 

但這種格式在不同的系統中有不同的類型定義,所以要使用這種格式,最好還是參考一下手冊。在調用中,參數signum指出要設定處理方法的訊號。第二個參數handler是一個處理函數,或者是 

  • SIG_IGN:忽略參數signum所指的訊號。 
  • SIG_DFL:恢複參數signum所指訊號的處理方法為預設值。 

傳遞給訊號處理常式的整數參數是訊號值,這樣可以使得一個訊號處理常式處理多個訊號。系統調用signal()傳回值是指定訊號signum前一次的處理常式或者錯誤時返回錯誤碼SIG_ERR。 

signal()通過系統調用sys_signal()為一個指定的訊號設定使用者態處理函數。sys_signal()定義如下:

/* * For backwards compatibility.  Functionality superseded by sigaction. */asmlinkage unsigned longsys_signal(int sig, __sighandler_t handler){struct k_sigaction new_sa, old_sa;int ret;new_sa.sa.sa_handler = handler;new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;ret = do_sigaction(sig, &new_sa, &old_sa);return ret ? ret : (unsigned long)old_sa.sa.sa_handler;}

__sighandler_t的定義如下:

typedef void __signalfn_t(int);typedef __signalfn_t __user *__sighandler_t;

訊號由sys_signal()的第一個參數指定,訊號處理函數的地址由第二個參數指定。sys_signal()根據這兩個參數設定一個k_sigaction結構,然後調用do_sigaction(),該函數的定義我們會在後面具體講解。

2.sigaction()

sigaction()的函數原型如下:

sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);

sigaction()對應的系統調用為do_sigaction(),下面我們具體講解do_sigaction()函數,其定義如下:

2.1do_sigaction()

intdo_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact){struct k_sigaction *k;if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))return -EINVAL;k = &currentt->sighand->action[sig-1];spin_lock_irq(&currentt->sighand->siglock);if (signal_pending(current)) {/* * If there might be a fatal signal pending on multiple * threads, make sure we take it before changing the action. */spin_unlock_irq(&currentt->sighand->siglock);return -ERESTARTNOINTR;}if (oact)//把原來的k_sigaction儲存到oact結構中,這裡是對整個資料結構進行複製*oact = *k;if (act) {/* * POSIX 3.3.1.3: *  "Setting a signal action to SIG_IGN for a signal that is *   pending shall cause the pending signal to be discarded, *   whether or not it is blocked." * *  "Setting a signal action to SIG_DFL for a signal that is *   pending and whose default action is to ignore the signal *   (for example, SIGCHLD), shall cause the pending signal to *   be discarded, whether or not it is blocked" */if (act->sa.sa_handler == SIG_IGN ||    (act->sa.sa_handler == SIG_DFL &&     sig_kernel_ignore(sig))) {/* * This is a fairly rare case, so we only take the * tasklist_lock once we're sure we'll need it. * Now we must do this little unlock and relock * dance to maintain the lock hierarchy. */struct task_struct *t = current;spin_unlock_irq(&t->sighand->siglock);read_lock(&tasklist_lock);spin_lock_irq(&t->sighand->siglock);*k = *act; //把新的k_sigaction結構複製到進程的sighand->action中sigdelsetmask(&k->sa.sa_mask,      sigmask(SIGKILL) | sigmask(SIGSTOP));rm_from_queue(sigmask(sig), &t->signal->shared_pending);do {rm_from_queue(sigmask(sig), &t->pending);recalc_sigpending_tsk(t);t = next_thread(t);} while (t != current);spin_unlock_irq(&current->sighand->siglock);read_unlock(&tasklist_lock);return 0;}*k = *act; //把新的k_sigaction結構複製到進程的sighand->action中sigdelsetmask(&k->sa.sa_mask,      sigmask(SIGKILL) | sigmask(SIGSTOP));}spin_unlock_irq(&currentt->sighand->siglock);return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.