Thread: I need a program to recognize a pattern of signals(linux environment)

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    3

    I need a program to recognize a pattern of signals(linux environment)

    I need to write a c program that can receive signals and recognize a certain pattern (SIGUSR1, SIGUSR2, SIGUSR1, SIGUSR2). once the pattern is caught the program terminates itself.

    I am able to send the signals to the program from a different process, but I am unsure on how to get the signal handler to recognize the pattern.

    here is what i have so far:

    Code:
    #include <signal.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    sig_atomic_t sigusr1_count = 0;
    sig_atomic_t sigusr2_count = 0;
    
    void do_sleep(int i)
    {
     while(i)
     {
      i=sleep(i);
     }
    }
    
    void handler(int signal_number)
    {
     if(signal_number == SIGUSR1)
     {
      ++sigusr1_count;
     }
     if(signal_number == SIGUSR2)
     {
      ++sigusr2_count;
     }
    }
    
    int main()
    {
     struct sigaction sa;
     memset (&sa, 0, sizeof (sa));
     sa.sa_handler = &handler;
     sigaction (SIGUSR1, &sa, NULL);
     sigaction (SIGUSR2, &sa, NULL);
     printf("going to sleep, ima %i\n", getpid());
     do_sleep(30);
    
    
     printf("SIGUSR1 was raised %d times\n", sigusr1_count);
     printf("SIGUSR2 was raised %d times\n", sigusr2_count);
     return 0;
    }
    I just need help on getting the program to be able to catch that specific pattern.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I would keep a list of signals that your program receives, and every time you receive one, check the last n signals to see if they match the pattern. Is there a specific time frame during which the signals should arrive? You could store a unix timestamp along with the signal, so that you can only do it if all of the signals have been received within the last x seconds.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    3
    Quote Originally Posted by Elkvis View Post
    I would keep a list of signals that your program receives, and every time you receive one, check the last n signals to see if they match the pattern. Is there a specific time frame during which the signals should arrive? You could store a unix timestamp along with the signal, so that you can only do it if all of the signals have been received within the last x seconds.
    there isn't a specific time frame, the goal of the exercise is to get familiar with signals and handling them. I'm just struggling with the logic behind making the program recognize the pattern of signals.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Perhaps you should consider using a state machine.

    It could be structured something like this:

    Code:
    case STATE_0
      wait for signal
      if signal is what you expect, move to STATE_1
    
    case STATE_1
      wait for signal
      if signal is what you expect, move to STATE_2
      else, move to STATE_0
    
    // ...etc

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    3
    Quote Originally Posted by Matticus View Post
    Perhaps you should consider using a state machine.

    It could be structured something like this:

    Code:
    case STATE_0
      wait for signal
      if signal is what you expect, move to STATE_1
    
    case STATE_1
      wait for signal
      if signal is what you expect, move to STATE_2
      else, move to STATE_0
    
    // ...etc
    Awesome! I took your advice and this is what I came up with (seems to be working perfectly!!!!):

    Code:
    #include <signal.h> 
    #include <stdio.h> 
    #include <string.h> 
    #include <sys/types.h> 
    #include <unistd.h> 
    #include <stdlib.h> 
    sig_atomic_t sigusr1_count = 0; 
    sig_atomic_t sigusr2_count = 0; 
     
    typedef enum 
    { 
     state1=0, 
     state2, 
     state3, 
     state4 
    } my_state_t;  
     
    my_state_t state = state1; 
     
    void do_sleep(int i) 
    { 
     while(i) 
     { 
      i=sleep(i); 
     } 
    } 
     
    void handler(int signal_number) 
    { 
     
     if(signal_number == SIGUSR1) 
     { 
      ++sigusr1_count; 
     } 
     if(signal_number == SIGUSR2) 
     { 
      ++sigusr2_count; 
     } 
     switch(state) 
     { 
      case state1: 
      if(signal_number == SIGUSR1) 
      state = state2; 
      break; 
     
      case state2: 
      if(signal_number == SIGUSR2) 
      state = state3; 
      else 
      state = state1; 
      break; 
    case state3:
      if(signal_number == SIGUSR1)
      state = state4;
      else
      state = state1;
      break;
    
      case state4:
      if(signal_number == SIGUSR2)
      exit(1);
      else
      state = state1;
      break;
      
      default:
      printf("Invalid signal");
     }
     
    }
    
    
    int main()
    {
     struct sigaction sa;
     memset (&sa, 0, sizeof (sa));
     sa.sa_handler = &handler;
     sigaction (SIGUSR1, &sa, NULL);
     sigaction (SIGUSR2, &sa, NULL);
     printf("going to sleep, ima %i\n", getpid());
     do_sleep(30);
    
    
     
     printf("SIGUSR1 was raised %d times\n", sigusr1_count);
     printf("SIGUSR2 was raised %d times\n", sigusr2_count);
      
     
     return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling signals in Linux
    By tkks in forum Linux Programming
    Replies: 0
    Last Post: 04-18-2013, 01:37 PM
  2. How do I use signals sent from the kill() Linux call?
    By jsrig88 in forum C Programming
    Replies: 3
    Last Post: 10-10-2009, 10:20 PM
  3. Using signals between unrelated processes (C/Linux)
    By sauronnikko in forum C Programming
    Replies: 3
    Last Post: 06-29-2009, 04:20 AM
  4. MS compiler in Linux environment
    By Fyodorox in forum Linux Programming
    Replies: 4
    Last Post: 06-29-2002, 05:23 PM
  5. which linux environment should I use?
    By Dissata in forum Linux Programming
    Replies: 19
    Last Post: 10-02-2001, 06:45 AM