Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to handle multiple signlas from a C++ program

Posted on 2010-11-22
4
Medium Priority
?
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

722 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