466
|
1 <refentry id="signal">
|
|
2 <refnamediv>
|
468
|
3 <refname>Signal</refname>
|
|
4 <refpurpose>catch or ignore interrupts</refpurpose>
|
466
|
5 </refnamediv>
|
|
6
|
|
7 <refsynopsisdiv>
|
|
8 <funcsynopsis>
|
468
|
9 <funcsynopsisinfo>
|
|
10 #include <signal.h>
|
578
|
11
|
|
12 typedef int (*sighandler_t)(int);
|
468
|
13 </funcsynopsisinfo>
|
466
|
14 <funcprototype>
|
578
|
15 <funcdef>sighandler_t <function>signal</function></funcdef>
|
|
16 <paramdef>int <parameter>interrupt</parameter></paramdef>
|
|
17 <paramdef>sighandler_t <parameter>address</parameter></paramdef>
|
|
18
|
466
|
19 </funcprototype>
|
|
20 </funcsynopsis>
|
|
21
|
|
22 </refsynopsisdiv>
|
|
23
|
|
24 <refsect1><title>Description</title>
|
|
25 <para>
|
557
|
26 This call is a comprehensive method of catching or ignoring
|
|
27 signals sent to the current process. Notice that "kill()" does
|
|
28 the sending of signals, and "signal()" does the catching.
|
466
|
29 </para>
|
556
|
30 <para>
|
607
|
31 Normally, a signal sent to a process causes it to terminate
|
|
32 with the status of the signal. If, in advance of the
|
|
33 anticipated signal, this system call is used, the program has
|
|
34 the choice of ignoring the signal or designating a function to
|
|
35 be executed when it is received. Different functions may be
|
|
36 designated for different signals.
|
|
37 </para>
|
|
38 <para>
|
|
39 The values for "address" have the following meanings:
|
|
40 <informaltable frame="none">
|
|
41 <tgroup cols="2">
|
640
|
42 <colspec colwidth="0.7in"/>
|
|
43 <colspec colwidth="3in"/>
|
607
|
44 <tbody>
|
|
45 <row>
|
|
46 <entry>0</entry>
|
|
47 <entry>reset to the default i.e. abort when received.</entry>
|
|
48 </row>
|
|
49 <row>
|
|
50 <entry>1</entry>
|
|
51 <entry>ignore; this will apply until reset to another value.</entry>
|
|
52 </row>
|
|
53 <row>
|
|
54 <entry>Otherwise</entry>
|
|
55 <entry>taken to be the address of a C function which
|
|
56 is to be executed on receipt of the signal.</entry>
|
|
57 </row>
|
|
58 </tbody>
|
|
59 </tgroup>
|
|
60 </informaltable>
|
|
61 </para>
|
|
62 <para>
|
|
63 If the latter case is chosen, when the signal is received by
|
|
64 the process the "address" is reset to 0, the default, before
|
|
65 the function is executed. This means that if the next signal
|
|
66 received should be caught then another call to "signal()"
|
|
67 should be made immediately. This is normally the first action
|
|
68 taken by the "interrupt" function. The function may access the
|
|
69 signal number which caused its execution by looking at its
|
|
70 argument. On completion of this function the program resumes
|
|
71 at the point at which is was "interrupted" by the signal.
|
|
72 </para>
|
|
73 <para>
|
|
74 An example should help to clarify all this. Suppose a program
|
|
75 needs to create a temporary file which should be deleted before
|
|
76 exiting. The body of the program might contain fragments like
|
|
77 this:
|
|
78 <programlisting>
|
|
79 pn = creat("temp",3); /* create a temporary file */
|
|
80 signal(2,intrupt); /* ensure tidying up */
|
|
81 signal(3,intrupt);
|
|
82 write(pn,string,count);
|
|
83 close(pn); /* finished writing */
|
|
84 unlink("temp"); /* delete it */
|
|
85 exit(0); /* normal exit */
|
|
86 </programlisting>
|
|
87 The call to "signal()" will ensure that if a keyboard or quit
|
|
88 signal is received then the function "intrupt()" will be
|
|
89 executed and this might be written:
|
|
90 <programlisting>
|
|
91 intrupt(sig)
|
|
92 {
|
|
93 close(pn); /* close it if open */
|
|
94 unlink("temp"); /* delete it */
|
|
95 exit(sig); /* received signal er exit status */
|
|
96 }
|
|
97 </programlisting>
|
|
98 </para>
|
|
99 <para>
|
|
100 In this case, as the function will be exiting before another
|
|
101 signal is received, it is unnecessary to call "signal()" again
|
|
102 to reset its pointer. Note that either the function
|
|
103 "intrupt()" should appear in the source code before the call to
|
|
104 "signal()", or it should be pre-declared.
|
|
105 </para>
|
|
106 <para>
|
556
|
107 The signals used by OS-9 are defined in the header file as
|
|
108 follows:
|
|
109 </para>
|
|
110 <programlisting>
|
|
111 /* OS-9 signals */
|
|
112 #define SIGKILL 0 /* system abort (cannot be caught or ignored)*/
|
|
113 #define SIGWAKE 1 /* wake up */
|
|
114 #define SIGQUIT 2 /* keyboard abort */
|
|
115 #define SIGINT 3 /* keyboard interrupt */
|
|
116
|
|
117 /* special addresses */
|
|
118 #define SIG_DFL 0 /* reset to default */
|
|
119 #define SIG_IGN 1 /* ignore */
|
|
120 </programlisting>
|
|
121 <para>
|
|
122 Please note that there is another method of trapping signals,
|
|
123 namely "intercept()" (q.v.). However, since "signal()" and
|
|
124 "intercept()" are mutually incompatible, calls to both of them
|
|
125 must not appear in the same program. The link-loader will
|
|
126 preven the creation of an executable program in which both are
|
|
127 called by aborting with an "entry name clash" error for
|
|
128 "_sigint".
|
|
129 </para>
|
466
|
130 </refsect1>
|
544
|
131
|
|
132 <refsect1><title>See Also</title>
|
|
133 <para>
|
|
134 <link linkend="intercept">intercept()</link>,
|
|
135 OS-9 shell command "kill",
|
|
136 <link linkend="kill">kill()</link>
|
|
137 </para>
|
|
138 </refsect1>
|
|
139
|
466
|
140 </refentry>
|