Solved

how to handle multiple signlas from a C++ program

Posted on 2010-11-22
4
598 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

758 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

20 Experts available now in Live!

Get 1:1 Help Now