#include<signal.h>
void suicide();
main()
{
printf("use CTRL \\ for exiting \n");
//signal(SIGINT,SIG_DFL);
signal(SIGQUIT,suicide);
for (;;);
}
void suicide()
{ printf("hello here you r in the suicide code ");
}
i was just starting with signals .. and tried this ,, but in the above i am calling suicide function in the signal function , its not working why ??
also the default behavior of the <ctrl \ > is being suppressed .
can you tell me why it is so ,,
and when i tried it passing SIG_DFL it worked ..
ya a \n in the printf worked ,, Porter ,, can you tell me what was the problem ,, and now pressing CTRL \ first time is printing the message ,, but pressing it for the second time must quit it , i e the default action must be performed ,, but it is again printing the message ..???
It is dependent on the semantics of the signal used, whether older semantics is used or the newer semantics.
Once the function registered for signal handler is executed, the function has to be re-registered again to ensure that the function is executed when a signal of same type is received again
This is not needed with newer semantics, no need for re-registering it again and again.
Please do use tags, else it would be difficult to read your code.
Output that you get is logical.
"use CTRL \\ for exiting" ==> would be immediately printed once it encounters a "\n".
Yes, there is a "\n" in the printf, so its getting printed.
The next statement, "iiiiiiiiiiiiiis" now will be in the stdout buffer and would remain to be there, unless the buffer is full and the kernel explicitly flushes it out or there is a "\n" in the printf.
Neither of the conditions are satisfied here,
so when CTRL + \ is given to the running binary, the function 'suicide' is executed which prints the statement in the printf block and it has got a "\n".
So, the new statement - " hii u r in suicide ", is appended in the stdout buffer along with "iiiiiiiiiiiiiiiiis" and gets printed.