歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> BSD >> 【FreeBSD操作系統設計與實現】注釋-4.7-1

【FreeBSD操作系統設計與實現】注釋-4.7-1

日期:2017/2/28 11:23:29   编辑:BSD


Posting of a Signal

【A signal is posted to a single process with the psignal() routine or to a group of processes with the gsignal() routine. The gsignal() routine invokes psignal() for each process in the specified process group. 】
QUOTE:---------------------------------------------------------------------FreeBSD6.1
1449 /*
1450 * Send a signal to a process group.
1451 */
1452 void
1453 gsignal(pgid, sig)
1454 int pgid, sig;
1455 {
1456 struct pgrp *pgrp;
1457
1458 if (pgid != 0) {
1459 sx_slock(&proctree_lock);
1460 pgrp = pgfind(pgid);
1461 sx_sunlock(&proctree_lock);
1462 if (pgrp != NULL) {
1423 pgsignal(pgrp, sig, 0);
1464 PGRP_UNLOCK(pgrp);
1465 }
1466 }
1467 }
1468
1469 /*
1470 * Send a signal to a process group. If checktty is 1,
1471 * limit to members which have a controlling terminal.
1472 */
1473 void
1474 pgsignal(pgrp, sig, checkctty)
1475 struct pgrp *pgrp;
1476 int sig, checkctty;
1477 {
1478 register struct proc *p;
1479
1480 if (pgrp) {
1481 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1482 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1483 PROC_LOCK(p);
1484 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1485 psignal(p, sig);
1486 PROC_UNLOCK(p);
1487 }
1488 }
1489 }
---------------------------------------------------/usr/src/sys/kern/kern_sig.c

進程組的hash鏈表pgrphashtbl[]是由/sys/kern/kern_proc.c文件中定義的struct sx類型變量proctree_lock進行保護的,這是一個Shared/Exclusive類型的鎖。如果需要對被保護對象執行讀操作,則只需獲得共享鎖,且多個線程可同時請求對同一個對象的共享鎖。如果需要對被保護對象執行寫操作,則需要獲得互斥鎖。對於同一個對象,只能有一個線程持有其互斥鎖。在gsignal()函數中,由於只是讀取進程組hash鏈表中的內容,因此只需在讀取前後獲取和釋放共享鎖即可(1459、1461行)。

1460行根據進程組ID從進程組hash鏈表中找到進程組的指針,傳給pgsignal()函數。pgsignal()函數遍歷該進程組鏈表,對於鏈表上的每一個進程都調用psignal()函數發送相應信號。
Copyright © Linux教程網 All Rights Reserved