Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

sigaction in C displaying 2 messages

  • Please log in to reply




  • Member
  • PipPip
  • 25 posts
Hey I'm using signals in C, one signal increments a counter and the 2nd one resets counter and prints the count to the screen. But when I do a "killall -sigusr2 signalcode", it prints the count out twice. Somehow the first signal handler is passing on the message, but I need the handler to consume it instead, so only one 'count' prints out. Here is my program. Below it is a screenie of what happens when I issue the signals from the left terminal while the program is running in the right terminal. I increment count 5 times by signalling the first handler 5 times, then when I do the signal to the 2nd handler (just once), it prings out twice in the terminal running the program.

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

sig_atomic_t count = 0;

void handler (int signal_number) {

void handler2 (int signal_number) {
  if(count >= 10) {
	printf("You have counted too many digits.\n"
		   "You wanted %d and you can only use 9.\n"
		   "I can't just grow an extra finger or toe people. Start over.\n",count);
	count = 0;
  else {
	count = 0;

void handler3 (int signal_number) {
  printf("Liar! It is not dead (just wounded).\n") ;

int main(int argc, char ** argv) {
  struct sigaction sa1, sa2, sa3;
  int pid;
  memset (&sa1, 0, sizeof (sa1));
  sa1.sa_handler = &handler;

  memset (&sa2,0,sizeof(sa2));
  sa2.sa_handler = &handler2;

  memset (&sa3,0,sizeof(sa2));
  sa3.sa_handler = &handler3;

  pid = fork();
  if (pid != 0) {
	for(;;) {
	  //printf("I'm awake, where's the coffee\n");
  else {


Thanks in advance!
  • 0




    Tech Staff

  • Technician
  • 1,323 posts
A couple things I see here that may or may not be problematic:
  • int main(int argc, char ** argv) {
      memset (&sa3,0,sizeof(sa2));
    Maybe it doesn't matter, but should this last line be the following?
    memset (&sa3,0,sizeof(sa3));
  • pid = fork();
    This will create multiple processes with identical but separate address spaces that both execute the code directly after the fork() call. I'm guessing that you get two output values because both parent and child processes are incrementing their own version of count, as well as printing and resetting their own version of count. You could print out count after incrementing it in the first handler to confirm/reject this.

I've not had much experience with C, much less signals in C, but I hope this information gives you some assistance.

There is another thing I found that may be relevant, since I see you are suspending the parent process:

...sleep can return sooner if a signal arrives; if you want to wait for a given interval regardless of signals, use select (see section 13.8 Waiting for Input or Output) and don't specify any descriptors to wait for.

This may be the underlying issue, but you may have to debug to figure out either way.


  • 0

Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP