Link to home
Start Free TrialLog in
Avatar of upendra_swami
upendra_swami

asked on

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.
Avatar of cup
cup

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?
Avatar of upendra_swami

ASKER

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.

ASKER CERTIFIED SOLUTION
Avatar of cup
cup

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.