0

I wrote a network packet listener program and I have 2 threads. Both runs forever but one of them sleeps 30 sec other sleeps 90 sec. In main function, I use sigaction function and after installed signal handler, I created these 2 threads. After creation of threads, main function calls pcaploop function, which is infinite loop. Basic structure of my program: (I use pseudo syntax)

signalHandler()  
   only sets a flag (exitState = true)

thread1()   
{  
    while 1  
    {  
       sleep 30 sec  
       check exit state, if so exit(0);  
       do smth;   
     }  
}

thread2()
{
    while 1
    {
       sleep 90 sec
       check exit state, if so exit(0);
       do smth;

    }
}

main()
{
    necassary snytax for sigaction ;
    sigaction( SIGINT, &act, NULL ); 
    sigaction( SIGUSR1, &act, NULL ); 

    create thread1;
    create thread2;

    pcaploop(..., processPacket,...); // infinite loop, calls callback function (processPacket) everytime a packet comes.

    join threads;
    return 0; 

}

processPacket()
{
   check exitState, if true exit(0);
   do smth;
}

And here is my question. When I press CTRL-C program does not terminate. If the program run less than 6-7 hours, when I press CTRL-C, program terminates. If the program run 1 night, at least 10 hours or more, I cannot terminate the program. Actually, signal handler is not called.

What could be the problem? Which thread does catch the signal?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Creed
  • 103
  • 7

2 Answers2

0

Basically it would be better to remove all pseudo code you put in your example, and leave the minimum working code, what exactly you have.

From what I can see so far from your example, is that the error handling of sigaction's is missing.

Try to perform checks against errors in your code.

deimus
  • 9,565
  • 12
  • 63
  • 107
  • i wouldn't use pseudo codes, i used for better understanding of my work. For your suggestion, unfortunately it does not solve my problem, since signal handler does not called at all. – Creed Sep 29 '14 at 07:40
0

I am writing this for those who had faced with this problem. My problem was about synchronization of threads. After i got handle synchronization problem, the program now, can handle the signals. My advice is check the synchronization again and make sure that it works correctly.

I am sorry for late answer.

Edited : I have also used sigaction for signal handling and I have change my global bool variable whit this definition :

static volatile sig_atomic_t exitFlag = 0;

This flag has been used for checking whether the signal received or not.

Creed
  • 103
  • 7