problem with signal()

#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 ..

put this in suicide

void suicide(int num)
{
    write(1,"suicide\n",8);
}

or put a \n in your printf, or use fflush(stdout);

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 ..???

if stdout is connected to a terminal then it does not fflush until a newline is written.

some operating systems require the signal handler to be reinstalled after calling, eg

void suicide(int num)
{ 
     ... set flags etc ....

     signal(num,suicide);
}

thanks porter .. i m using fedora core 7 ,,it seems that it doesn't need the signal handler to be called again.

#include<stdio.h>
#include<signal.h>
void suicide();
main()
{
printf("use CTRL \\ for exiting \n");
printf("iiiiiiiiiiiiiis");
//signal(SIGINT,SIG_DFL);
signal(SIGQUIT,suicide);
for (;;);
}

/void suicide()
{ printf("hello here you r in the suicide code\n ");
}
/

void suicide(int num)
{
printf("hii u r in suicide\n");
}

why the second printf in main is being executed after pressing CTRL \ . the iiiiiiiis is being printed only after i m pressing CTRL \ ,,

#include<stdio.h>
main()
{printf("hello u\n");
printf("hello how r yu again ");
}

while here both the print statements are being executed in one go .. ???

I doubt whether it is operating system dependent.

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.

That counts as operating system dependent to me. :slight_smile:

I always recommend sigaction to avoid ambiguity.

I perfectly agree with that ! :slight_smile:

I thought either of the semantics - old or new signal semantics is possible to be used in any of the flavors of unix/linux.

One more question, was that quote by 'Stevens' from some forum or was that from a book ?

If thats from forum - which forum is it from ?

From the Bible, "UNIX Network Programming" by the late W. Richard Stevens.