Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

It is important to note that the signal() function behaves a little differently in Windows than it does on Linux/BSD systems. When a signal handler is installed with the signal() function in Windows, after the signal is triggered once, the default action is restored for that signal. Conversely, Linux/BSD systems leave the signal handler defined by the user in place until it is explicitly removed.

Implementation Details

The following code runs differently on Linux and Windows.

Code Block
#include <stdio.h>
#include <signal.h>

volatile sig_atomic_t e_flag = 0;

void handler(int signum) {
	e_flag = 1;
}

int main() {
	signal(SIGINT, handler);
	while(!e_flag) {}
	puts("Escaped from first while()");

	e_flag = 0;
	while(!e_flag) {}
	puts("Escaped from second while()");

	return 0;
}

Linux

When compiled with gcc 3.4.4 on Red Hat Linux, the signal handler is automatically reinstalled upon handler execution.

Code Block
% ./SIG01-A
^C
Escaped from first while()
^C
Escaped from second while()
%

Windows

When compiled with Microsoft Visual Studio 2005, version 8.0, the signal handler is not automatically reinstalled.

...

Code Block
void handler(int signum) {
   signal(signum, handler);

   /* rest of handling code */
}

References

Wiki Markup
\[[ISO/IEC 9899-1999TR2|AA. C References#ISO/IEC 9899-1999]\] "The {{signal}} function"