> What is the use of calling:
> signal (SIGSEGV,SIG_DFL);
> Even if it is not written the default action will be taken! Right?
When you did set it to something else but then want to switch back
to the default action SIG_DFL can be quite handy.
Quote:> I have a book, which says:
> The SIG_IGN and SIG_DFL are manifest constants defined in the
> <signal.h> header:
> #define SIG_DFL void(*)(int)0
> #define SIG_IGN void(*)(int)1
> But if you substitute SIG_DFL with void(*)(int)0 it doesn't work. Why?
First of all, what "doesn't work"? And this is the way it is defined
on some systems, but AFAIK there's nowhere written is stone that it
needs to be defined this way. E.g. SUSV3 just writes that both must be
constant expressions whose values match no declarable function, but
that's all. SIG_DFL and SIG_IGN are defined so you don't need to know.
Quote:> Could someone help me understand this prototype?
> void(*signal(int signal_num, void (*handler)(int)))(int);
> What does it mean when you can actually call the function with
> something as simple as:
> signal (SIGTERM,catch_sig);
The signal() function returns a pointer to the proviously used
signal handler function, which itself is a function that takes
a single int argument and returns void. So, when we leave out the
arguments of signal() it would be declared as
void ( *signal )( int )
The parentheses around *signal are important, without them it
would mean that signal() is a function that takes an int and
returns a void pointer. With the parentheses it means that
signal is a pointer to a function that takes an int and
Now, the signal() function is a function that takes two arguments,
an int and a pointer to a signal handler function (that again
returns void and takes an int argument. So, without the return
value the declaration of signal() would be
signal( int, void ( *sighandler )( int ) )
Finally, we have to conmbine this and get
void ( *signal( int, void ( *sighandler )( int ) ) )( int )
You can make things easier for yourself if you use an additional
typedef void ( *sighandler_t )( int );
That declares sighandler_t to represent a pointer to a function that
returns void and has an int argument, which is just the type of the
return value of the signal() function and the type of its second
argument. With this you can write the declaration of signal() as
sighandler_t signal( int, sighandler_t );
i.e. signal() is a function of two arguments, an int (the signal number)
and a pointer to a signal handler function (the one that is to be used
for the signal), and returns a pointer to a signal handler function (the
one that has been used until now for the signal).
When you now write
Quote:> signal (SIGTERM,catch_sig);
you set up a new signal handler, catch_sig, for SIGTERM and throw
away the return value, that would be pointing to the function that
has been dealing with SIGTERM signals until now.
Welcome to the interesting ways you do function declarations in C.
In principle it's logical, but it takes quite a bit of getting used
_ _____ _____
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring