Solved

how to handle multiple signlas from a C++ program

Posted on 2010-11-22
4
599 Views
Last Modified: 2013-11-17
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
Comment
Question by:upendra_swami
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:cup
ID: 34194575
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
 

Author Comment

by:upendra_swami
ID: 34195071
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
 
LVL 11

Accepted Solution

by:
cup earned 500 total points
ID: 34202915
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
 

Author Comment

by:upendra_swami
ID: 34224023
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about a problem I had getting the GPS LocationListener working.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now