0

I am using the open signals SIGUSR1 and SIGUSR2 to call a user-defined function. I have tried two function prototype for my signal handling function. Both of which runs without any compilation error. What exactly happens when open signals call a function? How is the function supposed to be implemented?

prototype1:

/***********************************************************/
/*** Sample program demonstrating the sending of signals ***/
/*** Written by Abhijit Das, 17-Jan-2014                 ***/
/***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

/* The signal handler for the child process */
void childSigHandler (int sig)
{
    //int sig;
   if (sig == SIGUSR1) {
      printf("+++ Child : Received signal SIGUSR1 from parent...\n");
      sleep(1);
   } else if (sig == SIGUSR2) {
      printf("+++ Child : Received signal SIGUSR2 from parent...\n");
      sleep(5);
   }
   exit(0);
}

int main ()
{
   int pid;

   pid = fork();                                   /* Spawn the child process */
   if (pid) {
                                                            /* Parent process */
      int t;
      srand((unsigned int)time(NULL));
      t = 2 + rand() % 4;
      printf("+++ Parent: Going to sleep for %d seconds\n", t);
      sleep(t);       /* Sleep for some time before sending a signal to child */
      t = 1 + rand() % 2;
      printf("+++ Parent: Going to send signal SIGUSR%d to child\n", t);
      kill(pid, (t == 1) ? SIGUSR1 : SIGUSR2);        /* Send signal to child */
      wait(NULL);                                   /* Wait for child to exit */
      printf("+++ Parent: Child exited\n");

   } else {
                                                             /* Child process */
      signal(SIGUSR1, childSigHandler);           /* Register SIGUSR1 handler */
      signal(SIGUSR2, childSigHandler);           /* Register SIGUSR2 handler */
      while (1) sleep(1);     /* Sleep until a signal is received from parent */

   }

   exit(0);
}

prototype2:

/***********************************************************/
/*** Sample program demonstrating the sending of signals ***/
/*** Written by Abhijit Das, 17-Jan-2014                 ***/
/***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

/* The signal handler for the child process */
void childSigHandler ()
{
int sig;
   if (sig == SIGUSR1) {
      printf("+++ Child : Received signal SIGUSR1 from parent...\n");
      sleep(1);
   } else if (sig == SIGUSR2) {
      printf("+++ Child : Received signal SIGUSR2 from parent...\n");
      sleep(5);
   }
   exit(0);
}

int main ()
{
   int pid;

   pid = fork();                                   /* Spawn the child process */
   if (pid) {
                                                            /* Parent process */
      int t;
      srand((unsigned int)time(NULL));
      t = 2 + rand() % 4;
      printf("+++ Parent: Going to sleep for %d seconds\n", t);
      sleep(t);       /* Sleep for some time before sending a signal to child */
      t = 1 + rand() % 2;
      printf("+++ Parent: Going to send signal SIGUSR%d to child\n", t);
      kill(pid, (t == 1) ? SIGUSR1 : SIGUSR2);        /* Send signal to child */
      wait(NULL);                                   /* Wait for child to exit */
      printf("+++ Parent: Child exited\n");

   } else {
                                                             /* Child process */
      signal(SIGUSR1, childSigHandler);           /* Register SIGUSR1 handler */
      signal(SIGUSR2, childSigHandler);           /* Register SIGUSR2 handler */
      while (1) sleep(1);     /* Sleep until a signal is received from parent */

   }

   exit(0);
}
sudeepdino008
  • 3,194
  • 5
  • 39
  • 73

2 Answers2

1

See the documentation.

The signature for a signal handler is:

typedef void (*sighandler_t)(int);

Your first example uses an empty parameter list, which basically doesn't declare any expected arguments. The compiler is probably generating boiler-plate to handle "any" arguments being passed, in that case.

If you enable all warnings, you might get something from your compiler. Also note that you can make the handler static since you're passing the pointer to the library anyway, it doesn't have to be visible from the outside.

unwind
  • 391,730
  • 64
  • 469
  • 606
-3

Read this:

http://www.gnu.org/software/libc/manual/html_node/Basic-Signal-Handling.html

In the first 10 lines of this page, you have your answer.
Since our good frien alk here thinks I'm not being explicit enough, I'll copy-paste them for you :

The signal function provides a simple interface for establishing an action for a particular signal. The function and associated macros are declared in the header file signal.h.

— Data Type: sighandler_t

This is the type of signal handler functions. Signal handlers take one integer argument specifying the signal number, and have return type void. So, you should define handler functions like this:

void handler (int signum) { ... }

The name sighandler_t for this data type is a GNU extension.

 — Function: sighandler_t signal (int signum, sighandler_t action)

The signal function establishes action as the action for the signal signum. For more information about defining signal handler functions, see Defining Handlers.

Or even better, google this:

signal handler function

Or even better yet, read a textbook about signal handling first.

If you are not familiar with C basic mechanisms, it is a bit early to tackle signal handling, in my opinion.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • This would have suited better as a comment. – alk Jan 22 '14 at 16:06
  • @alk Well I could have copy-pasted the GNU documentation too, but it would have added little value. – kuroi neko Jan 22 '14 at 16:10
  • 1
    What about a brief summary referring to the specific issue the OP is struggeling with? Answers here at SO are expected to **directly** refer to a question. – alk Jan 22 '14 at 16:12
  • @alk that is exactly what the GNU page does. In the first 3 lines there is a definition of the sig handler prototype. – kuroi neko Jan 22 '14 at 16:13
  • So why not just quote those three lines, adding an introduction that link them to the OPs issue. And then finsh with the link you got the quote form. – alk Jan 22 '14 at 16:14
  • Hadn't you been talking about thress lines? – alk Jan 22 '14 at 16:17
  • @alk frankly this question makes no sense. If the OP does not grasp the basics of C yet, trying to explain what a sig handler does is futile. Anyway... – kuroi neko Jan 22 '14 at 16:17
  • That's why I did not answer. ;-) – alk Jan 22 '14 at 16:18
  • Warning the OP he is doing something futile has some value in my book. Better spend some time doing scales than trying to play Mozart right away, IMHO. – kuroi neko Jan 22 '14 at 16:38