unix下最简单的信号处理函数是signal函数:
1 2 |
#include <span style="color: #339933;"><</span>signal .<span style="color: #202020;">h</span><span style="color: #339933;">></span> <span style="color: #993333;">void</span> <span style="color: #009900;">(</span><span style="color: #339933;">*</span>signal<span style="color: #009900;">(</span><span style="color: #993333;">int</span> signo<span style="color: #339933;">,</span><span style="color: #993333;">void</span> <span style="color: #009900;">(</span><span style="color: #339933;">*</span>func<span style="color: #009900;">)</span><span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
sign参数是信号名,func是信号处理程序。
为了简化signal函数的书写,我们通常用typedef:
1 2 |
<span style="color: #993333;">typedef</span> <span style="color: #993333;">void</span> signfunc<span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> signfunc signal<span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #339933;">,</span>signfunc <span style="color: #339933;">*</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
下面的程序捕捉两个用户自定义的信号进行处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<span style="color: #339933;">#include <signal .h></span> <span style="color: #339933;">#include <stdio .h></span> <span style="color: #339933;">#include <stdlib .h></span> <span style="color: #993333;">typedef</span> <span style="color: #993333;">void</span> Sigfunc<span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> Sigfunc <span style="color: #339933;">*</span>signal<span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #339933;">,</span> Sigfunc <span style="color: #339933;">*</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> sig_usr<span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #993333;">int</span> main<span style="color: #009900;">(</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span><span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>signal<span style="color: #009900;">(</span>SIGUSR1<span style="color: #339933;">,</span>sig_usr<span style="color: #009900;">)</span><span style="color: #339933;">==</span>SIG_ERR<span style="color: #009900;">)</span> perror<span style="color: #009900;">(</span><span style="color: #ff0000;">"can't catch SIGUSR1"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>signal<span style="color: #009900;">(</span>SIGUSR2<span style="color: #339933;">,</span>sig_usr<span style="color: #009900;">)</span><span style="color: #339933;">==</span>SIG_ERR<span style="color: #009900;">)</span> perror<span style="color: #009900;">(</span><span style="color: #ff0000;">"can't catch SIGUSR2"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">for</span><span style="color: #009900;">(</span><span style="color: #339933;">;;</span><span style="color: #009900;">)</span> pause<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> sig_usr<span style="color: #009900;">(</span><span style="color: #993333;">int</span> signo<span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>signo<span style="color: #339933;">==</span>SIGUSR1<span style="color: #009900;">)</span> <span style="color: #000066;">printf</span><span style="color: #009900;">(</span><span style="color: #ff0000;">"received SIGUSR1<span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>signo<span style="color: #339933;">==</span>SIGUSR2<span style="color: #009900;">)</span> <span style="color: #000066;">printf</span><span style="color: #009900;">(</span><span style="color: #ff0000;">"received SIGUSR2<span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">else</span> <span style="color: #000066;">printf</span><span style="color: #009900;">(</span><span style="color: #ff0000;">"received signal %d <span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #339933;">,</span>signo<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> |
进行编译链接运行:
1 2 3 4 5 6 7 |
$ <span style="color: #c20cb9; font-weight: bold;">cc</span> s.c $ .<span style="color: #000000; font-weight: bold;">/</span>a.out <span style="color: #000000; font-weight: bold;">&</span> #后台运行 <span style="color: #7a0874; font-weight: bold;">[</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">]</span> <span style="color: #000000;">4749</span> $ <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-USR1</span> <span style="color: #000000;">4749</span> #发送用户自定义信号 $ received SIGUSR1 $ <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-USR2</span> <span style="color: #000000;">4749</span> #发送用户自定义信号 $ received SIGUSR2 |
转载请注明:爱开源 » unix简单的信号处理