Example of using sigaction()

/* Example of using sigaction() to setup a signal handler with 3 arguments
 * including siginfo_t.
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
static void hdl (int sig, siginfo_t *siginfo, void *context)
	printf ("Sending PID: %ld, UID: %ld\n",
			(long)siginfo->si_pid, (long)siginfo->si_uid);
int main (int argc, char *argv[])
	struct sigaction act;
	memset (&act, '\0', sizeof(act));
	/* Use the sa_sigaction field because the handles has two additional parameters */
	act.sa_sigaction = &hdl;
	/* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
	act.sa_flags = SA_SIGINFO;
	if (sigaction(SIGTERM, &act, NULL) < 0) {
		perror ("sigaction");
		return 1;
	while (1)
		sleep (10);
	return 0;



can anyone tell what are the command line arguments to be given to this program?please help

there the sigaction is

there the sigaction is registering for sigterm.... (sig-terminate)... open another terminal and with the pid of the process pass sigterm signal... the handler will invoke.... open another terminal and type this kill -SIGTERM <> i have started a blog on linux kernel development www.kernelarmy.blogspot.in.... i have just started writing eventually i will fill it with linuxkernel programming concepts

There are no command line

There are no command line arguments. Just kill this process while running and observe the output.


any one tell me how the program works?

This is an example for the

This is an example for the article, there are more details in it: http://www.linuxprogrammingblog.com/all-about-linux-signals


Thanks a lot man I was completely lost in the man page until you came with the light

printf in signal handler not allowed

I think that printf() is not allowed in signal handler according to signal(7) man page.

it IS allowed

signal(7) lists the allowed syscalls, printf is not a syscall, that's why it's allowed inside a signal handler. It's build on top of write syscall, which is allowed...

printf() does not simply use

printf() does not simply use write(). stdio uses locking internally, memory allocation which also use locking. Believe me, it will eventually lock when you printf() from inside a signal handler :)

You're right, printf() can't

You're right, printf() can't be used in a signal handler, but this example will work because th program does nothing while receiving signals, so there is no race.

superb! thanks! :)

superb! thanks! :)