作業系統›Ch2 I/O 運作方式
第 3 題/共 23 題
◀ OS 3/23
3. Signal Handling、sigaction、sigprocmask、sigsuspend
#OS-02-003難Signal Handlingsigactionsigprocmasksigsuspend

請閱讀下列程式碼片段,並回答五個小題。

(a) (2 pts) 這個process第一次執行時,會在哪一行被suspend? (b) (2 pts) 當process被suspend、且一個SIGUSR2 signal被送給這個process時,process會如何反應?(A) terminated,(B) resume,(C) no action,或(D) Add SIGUSR2 to the signal mask。 (c) (2 pts) 當SIGUSR2被process捕捉到時,此時的signal mask是什麼?(A) SIGUSR1和SIGUSR2,(B) SIGUSR1,(C) SIGINT和SIGUSR2,(D) Empty。 (d) (2 pts) 當SIGINT被送出並被捕捉時,process會從哪一行恢復執行? (e) (2 pts) 若第17、19、21行的sigaction()都換成signal(),那麼在捕捉到SIGINT後恢復執行時的signal mask是什麼?(A) SIGUSR1和SIGUSR2,(B) SIGUSR1,(C) SIGINT和SIGUSR2,(D) Empty。

1  %% include all the head files
2
3  static void sigHandling(int);
4
5  int
6  main(void)
7  {
8      sigset_t     newmask, oldmask, waitmask;
9      struct sigaction handling, saveintr;
10
11     pr_mask("program start: ");
12
13     handling.sa_handler = sigHandling;
14     sigemptyset(&handling.sa_mask);
15     handling.sa_flags = 0;
16
17     if (sigaction(SIGUSR1, &handling, &saveintr) < 0)
18         return(-1);
19     if (sigaction(SIGUSR2, &handling, &saveintr) < 0)
20         return(-1);
21     if (sigaction(SIGINT, &handling, &saveintr) < 0)
22         return(-1);
23
24     sigemptyset(&waitmask);
25     sigaddset(&waitmask, SIGUSR1);
26     sigemptyset(&newmask);
27     sigaddset(&newmask, SIGINT);
28
29     /*
30      * Block SIGINT and save current signal mask.
31      */
32     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
33         err_sys("SIG_BLOCK error");
34
35     /*
36      * Critical region of code.
37      */
38     pr_mask("in critical region: ");
39
40     /*
41      * Pause, allowing all signals except SIGUSR1.
42      */
43     if (sigsuspend(&waitmask) != -1)
44         err_sys("sigsuspend error");
45
46     pr_mask("after return from sigsuspend: ");
47
48     /*
49      * Reset signal mask which unblocks SIGINT.
50      */
51     if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
52         err_sys("SIG_SETMASK error");
53
54     /*
55      * And continue processing ...
56      */
57     pr_mask("program exit: ");
58
59     exit(0);
60 }
61
62 static void
63 sigHandling(int signo)
64 {
65     pr_mask("\n in sigHandling:");
66     printf("Received signal %d\n", signo);
67 }
📄 台大110
跳轉到第題
▤完整推導請見《WH 資工筆記 · 作業系統》Ch2 I/O 運作方式
本章題號 · 1–20 / 23