Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

how to handle multiple signlas from a C++ program

I am implementing a program which has to handle following three signals for different purpose as described briefly below.
.
SIGTERM (for clean up/resource release and termination of the program)
SIGUSR1 (selecting updating database 1)
SIGUSR2 (selecting updating database 2)

I am wrting the program in C++. The program is multithreaded. I have defined the handler in the main thread. But i am not very sure how masking of the signals should be done in such scenario.
 I would be greatful, if you could suggest me some appropriate approach in such scenario.
Like how shoud we perform the mask of the signals when a particular signal arrives and how should we clear the mask later and so on.
0
upendra_swami
Asked:
upendra_swami
  • 2
  • 2
1 Solution
 
cupCommented:
Is this on Windows or Linux?

Do you have the same signal handler or different signal handlers for each signal or are you asking how to set up a signal handler?
0
 
upendra_swamiAuthor Commented:
Sorry for incomplete question.

This is on solaris.
This application was multifile and multithread existing application.
someone had already implemented handler for TERM signal and USR1 signal in the application. (The handlers were present on different files but seems to be on same thread.

For handling TERM, it is using signal (....) and for handling USR1 it is using sa_action (....) like stuff.
Following these existing things i tried to handler USR2 in the similar mannar as USR1. But it is not working properly.

Very first time i wrote different signal handler for USR2.
 In normal scenario, it was working properly. However , when two signals (USR1 and USR2) generated at almost same time, program stopped responding one of the signals. Sometimes, it only responded USR1 only or sometime USR2 only.

Then I wrote common signal handler for USR1 and USR2.
Inside the handler, i checked the signal type and perform the task accordingly.
Here also similar things are happening..like when signals are generated countinously
at a very short interval, it stops processing one of the signal.

Thanks in advance.

0
 
cupCommented:
Try an experiment - I'm just wondering if your signal handler is taking too long to process the signal.  It is possible that if it is in the middle of handling a signal, it won't accept another signal.

Try commenting out the code and just printing that you got the signal.  Does it catch both signals every time?

If it does, then use the signal handler to set a volatile flag/mutex/semaphore and for another thread to sit on that flag/mutex/semaphore and then process it when set.
0
 
upendra_swamiAuthor Commented:
Thanks cup.

I tried as you sugessted. I commented the bulky operation of the hander and justed printed the message.
It seemed that it blocks the same signal at the middle of the execution of handler.
However when signal of different types comes at the middle of handling a pariticular signal, it seems that other signal is caught.

So, i used signal mask to block the other signal when signal handler is running. Now it seems it is working fine...

Thanks for you comment. It really helped me.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now