Signal Handler Question

Signal Handler Question

Post by Jon Fra » Wed, 25 Jun 2003 00:38:30



Hello,

I'm new to linux development and I am currently going through the
"Advanced Linux Programming" book.  I've taken the following program
from their pages and the signal handler is never being called.  The
for loops were added to simply delay before ending the program.  Can
someone tell me what my problem may be?  I'm assuming the OS is
choosing not to call the handler for some reason:

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

sig_atomic_t sigusr1_count =0;

void handler (int signal_number)
{
  ++sigusr1_count;

Quote:}

int main()
{
  struct sigaction sa;
  int i=0;
  int j=0;

  memset (&sa, 0, sizeof(sa));
  sa.sa_handler = &handler;
  sigaction (SIGUSR1, &sa, NULL);

  /* DO BUSY WORK */
  for (j=0; j<100; j++)
    for (i =0; i<100000000; i++)
      ;

  printf("SIGUSR was raised %d times\n", sigusr1_count);
  return 0;

Quote:}

 
 
 

Signal Handler Question

Post by Jens.Toerr.. » Wed, 25 Jun 2003 02:19:52



> I'm new to linux development and I am currently going through the
> "Advanced Linux Programming" book.  I've taken the following program
> from their pages and the signal handler is never being called.  The
> for loops were added to simply delay before ending the program.  Can
> someone tell me what my problem may be?  I'm assuming the OS is
> choosing not to call the handler for some reason:
> #include <signal.h>
> #include <stdio.h>
> #include <string.h>
> #include <sys/types.h>
> #include <unistd.h>
> sig_atomic_t sigusr1_count =0;
> void handler (int signal_number)
> {
>   ++sigusr1_count;
> }
> int main()
> {
>   struct sigaction sa;
>   int i=0;
>   int j=0;
>   memset (&sa, 0, sizeof(sa));
>   sa.sa_handler = &handler;
>   sigaction (SIGUSR1, &sa, NULL);
>   /* DO BUSY WORK */
>   for (j=0; j<100; j++)
>     for (i =0; i<100000000; i++)
>       ;

Make this

    for ( i = 0; i < 100; i++ )
                pause();

then the program will sleep until it gets a signal (and stop after
100 signals).

Quote:>   printf("SIGUSR was raised %d times\n", sigusr1_count);
>   return 0;
> }

Did you send a signal to the program manually? Otherwise there's
nothing in this program that would send itself a signal and thus
there's no reason for the signal handler to get called.

You can easily send the program a signal by typing on the command
line

KILL -USR1 PID

where PID is the PID of the program to receive the signal.

BTW., these kinds of questions would be more on-topic on
comp.os.linux.development.apps...
                                      Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

1. correction - signal handler question

Hi All,
        Sorry about the previous posting. My bad!!

main()
{
        func_a();

func_a()
{
        func_b();

static my_sighandler(int sig)
{
        How can I find if I received the signal in main or during
        execution of func_a() or if the signal was received during
        func_b().

        Thank you all in advance.

Regards,
Harish Chandraiah


2. Linux crashes!!!

3. Help! - signal handler question

4. Hard Drive Running like a bat out of hell

5. Signal handlers inside signal handlers

6. why the shell script need #!/bin/sh ??

7. Threads performance - allow signal handler to not call handler

8. Adaptech 2940 Drivers Where?

9. Signal handlers are not reset after signal delivery

10. blocking signals in signal handler -- True or False quiz

11. Registering signal inside signal handler

12. Registering client data on signal manager side andreceiving it in signal handler

13. --- called from signal handler with signal -24242176 (SIG Unknown)