473,785 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Signals, threads and the use of SIGUSR1

Hi,

I am writing an application in C++ on Linux, using threads (this is
my first experience with pthreads). The application itself is fine, it
is just that I wanted to handle asynchronous signals correctly, in
particular the SIGINT, preventing the application from stopping
ungracefully.

Indeed, I'd like to catch the signal, close everything in a
consistent way and stop the application.

I ended up structuring the application with a default behaviour of
ignoring any SIGINT, SIGHUP, SIGTERM and SIGUSR1 signals both for the
main process and the 'application threads'. Then I issued a dedicated
thread waiting for one of these signals whose task is to logically stop
the other threads and terminate.

When I press CTRL-C for instance, the thread catches the signal and
stops all the other threads. But, when no event is generated, the risk
is that the thread would be waiting for ever; that's why, when the
application normally finish without an asynchronous signal, I send a
'SIGUSR1' to the thread above and the application normally ends.

I want to know if I am improperly using the SIGUSR1 signal and if -
in this case - there are other ways of accomplishing this.

Thank you very much,
-Gabriele

Jul 19 '05 #1
4 16308
> I am writing an application in C++ on Linux, using threads (this is
my first experience with pthreads).


http://www.parashift.com/c++-faq-lit...t.html#faq-5.9
Jonathan
Jul 19 '05 #2

"Gabriele Bartolini" <ba*******@info media.it> wrote in message
news:3f******** *************** @news.optusnet. com.au...
I ended up structuring the application with a default behaviour of
ignoring any SIGINT, SIGHUP, SIGTERM and SIGUSR1 signals both for the
main process and the 'application threads'. Then I issued a dedicated
thread waiting for one of these signals whose task is to logically stop
the other threads and terminate.
This is all quite reasonable.
When I press CTRL-C for instance, the thread catches the signal and
stops all the other threads. But, when no event is generated, the risk
is that the thread would be waiting for ever; that's why, when the
application normally finish without an asynchronous signal, I send a
'SIGUSR1' to the thread above and the application normally ends.


You don't necessarily need to kill the thread. You don't have to
terminate all your threads before the application terminates. Otherwise, you
can use a global variable protected by a mutex to indicate that a clean
shutdown is in progress and code the signal catcher to check that flag
before it does anything else. Then you could send that thread any signal.
However there's nothing wrong with using SIGUSR1 for that purpose.

DS
Jul 19 '05 #3
Hello Gabriele,
I am writing an application in C++ on Linux, using threads (this is
my first experience with pthreads).
<snip>
I ended up structuring the application with a default behaviour of
ignoring any SIGINT, SIGHUP, SIGTERM and SIGUSR1 signals both for the
main process and the 'application threads'. Then I issued a dedicated
thread waiting for one of these signals whose task is to logically stop
the other threads and terminate.
Looks good.
When I press CTRL-C for instance, the thread catches the signal and
stops all the other threads. But, when no event is generated, the risk
is that the thread would be waiting for ever; that's why, when the
application normally finish without an asynchronous signal, I send a
'SIGUSR1' to the thread above and the application normally ends.

I want to know if I am improperly using the SIGUSR1 signal and if -
in this case - there are other ways of accomplishing this.

Well, I don't know what kind of actions your "signal handler" thread
- the one that is waiting in sigwait() - does exactly, but I guess,
that's OK to just terminate your process as usual, without explicitely
terminate that thread. When the main thread returns, or one of your threads
terminate the process, then your signal handler thread shall be
automatically terminated.

Only when you need some "clean-up" actions (e.g. be sure that the
destructor of some objects is called etc.) in the signal handler thread
upon the normal process ending, then yes, you need to gracefully terminate
that thread.

Using SIGUSR1 is OK with the 'recent' Pthreads lib (I meant younger
than 2 years old). Indeed, older LinuxThreads library used SIGUSR1 and
SIGUSR2 for internal communications, so you weren't able to use that
signal in your application.

As noted by another poster, if you need to do some clean-up in the
signal thread, you could alternatively used pthread_cancel( ). Uses
a deferred cancelation only at sigwait() since that function is a
cancellation point. But using SIGUSR1 is also fine.

Regards,
Loic.

--
Article posté via l'accès Usenet http://www.mes-news.com
Accès par Nnrp ou Web
Jul 19 '05 #4
On Fri, 03 Oct 2003 11:54:42 +1000, Gabriele Bartolini wrote:
Hi,

I am writing an application in C++ on Linux, using threads (this is
my first experience with pthreads). The application itself is fine, it
is just that I wanted to handle asynchronous signals correctly, in
particular the SIGINT, preventing the application from stopping
ungracefully.

Indeed, I'd like to catch the signal, close everything in a
consistent way and stop the application.

I ended up structuring the application with a default behaviour of
ignoring any SIGINT, SIGHUP, SIGTERM and SIGUSR1 signals both for the
main process and the 'application threads'. Then I issued a dedicated
thread waiting for one of these signals whose task is to logically stop
the other threads and terminate.

When I press CTRL-C for instance, the thread catches the signal and
stops all the other threads. But, when no event is generated, the risk
is that the thread would be waiting for ever; that's why, when the
application normally finish without an asynchronous signal, I send a
'SIGUSR1' to the thread above and the application normally ends.

I want to know if I am improperly using the SIGUSR1 signal and if -
in this case - there are other ways of accomplishing this.

Thank you very much,
-Gabriele


Hi,

unless you are using the very new nptl thread implementation on a 2.6
kernel (or a heavily patched 2.4) signals are not handled according to the
posix std.; ie. having a signal handling thread does not work because
signals are delivered to specific threads. There is no de-coupling between
the Thread-ID and the Process-ID; hence new threads have a different
Process-ID.

just my 2 ct.

Have fun coding.

Peter Zijlstra
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2447
by: nowhere | last post by:
Hi, Java 1.4.1: I have a problem using the JNI interface to call some library methods written in java from some C code. The C code uses SIGUSR1 for asynchronous events. However JNI_CreateJVM unblocks SIGUSR1 even if I supply the "-XX:+UseAltSigs" option. The C code relies on the fact that SIGUSR1 is normally blocked and
3
1756
by: Sebastian Meyer | last post by:
Hi Newsgroup, i have some problems with using threads and signals in one program. In my program i have three threads running, one for checking a directory at a specified interval to see if new data arrived, one waiting for work and the main thread. My problem is the following: I want to shutdown the program via a signal, so i used the signal module. I know, that signal handling mus be done by the main thread, the signal
2
2056
by: Holger Joukl | last post by:
Hi, migrating from good old python 1.5.2 to python 2.3, I have a problem running a program that features some threads which execute calls to an extension module. Problem is that all of a sudden, I cannot stop the program with a keyboard interrupt any more; the installed signal handler does not seem to receive the signal at all. This happens both if I rebuild this extension using python 2.3 headers/library and if I simply use the old...
1
5976
by: Frank Bossy | last post by:
Dear group :) I don't quite understand the meaning of this paragraph in the qt docu (http://doc.trolltech.com/3.1/threads.html): ***SNIP The Signals and Slots mechanism can be used in separate threads, as long as the rules for QObject based classes are followed. The Signals and Slots mechanism is synchronous: when a signal is emitted, all slots are called immediately. The slots are executed in the thread
1
1860
by: Sori Schwimmer | last post by:
Hi, I am working on an application which involves interprocess communication. More to the point, processes should be able to notify other processes about certain situations, so the "notifyees" would be able to act in certain ways. As processes are distributed on several machines, in different physical locations, my thinking was:
0
1008
by: Mitko Haralanov | last post by:
Hi everyone, First off, I know that this has been discussed before and I did a search but could not find anything that helped my situation. Here is the problem: I have a Python program that uses threads, forked processes, and signals and I can't seem to understand where the signals go. When the program starts, it creates a thread, which spins in select
2
2157
by: shibu-kundara | last post by:
Hi folks, I have a problem using sigaction function in pthread library. I have 2 thread each one registering in threadA act1.sa_handler=func1; if(sigaction(SIGUSR1,&act1,NULL)) printf("\nSigaction failed! in th1 !!\n"); in threadB
0
1230
by: Lloyd Zusman | last post by:
I have a python-2.5 program running under linux in which I spawn a number of threads. The main thread does nothing while these subsidiary threads are running, and after they all complete, the main thread will then exit. I know that I can manage this through the use of Thread.join(), but when I do it as follows, the main thread doesn't respond to signals: import sys, time, signal, threading
6
2383
by: geoffbache | last post by:
Hi all, I have a Python program (on UNIX) whose main job is to listen on a socket, for which I use the SocketServer module. However, I would also like it to be sensitive to signals received, which it isn't if it's listening on the socket. ("signals can only be received between atomic actions of the python interpreter", presumably - and control will not return to Python unless something appears on the socket). Does anyone have a tip of a...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.