Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.sao.ru/hq/sts/linux/book/c_marshall/subsubsection2_18_4_3_2.html
Дата изменения: Unknown
Дата индексирования: Sat Sep 11 20:52:13 2010
Кодировка:

Поисковые слова: molecules
Receiving signals - <tt>signal()</tt>



Next: Times Up!! Up: Interrupts and Signals Previous: Sending Signals - kill()

Receiving signals - signal()

int (*signal(int sig, void (*func)()))() - that is to say the function signal() will call the func functions if the process receives a signal sig. Signal returns a pointer to function func if successful or it returns an error to errno and -1 otherwise.

func() can have three values:

SIG_DFL
- a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig.
SIG_IGN
- a pointer to system ignore function SIG_IGN() which will disregard the sig action (UNLESS it is SIGKILL).
A function address
- a user specified function.

SIG_DFLand SIG_IGN are defined in signal.h (standard library) header file.

Thus to ignore a ctrl-c command from the command line. we could do:

signal(SIGINT, SIG_IGN);

TO reset system so that SIGINT causes a termination at any place in our program, we would do:

signal(SIGINT, SIG_DFL);

So lets write a program to trap a ctrl-c but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT signal so we can terminate our program:

Finally lets write a program that communicates between child and parent processes using kill() and signal().

fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (== 0) or the parent (pid = child process id).

The parent can then send messages to child using the pid and kill().

The child picks up these signals with signal() and calls appropriate functions.

An example of communicating process using signals is sig_talk.c in Appendix .


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994