views:

33

answers:

2

Hi,

I am new to linux and writing a service in C++ which spawns multiple threads and I am starting the service by calling it from init.d, but how should I send the terminate signal to my application from the script , so that my service terminates all the threads and exits.

And also where can I find the source code for any linux services. e.g. /etc.init.d/rc5.d/S14cron . It will be helpful in understanding how to implement a service.

+1  A: 

The classic reference for this kind of question is Steven's "Advanced Programming in the UNIX Environment". You can find the source code to this text book here.

Gene Vincent
+1  A: 

Depends what your application does.

Personally I'd keep a thread just for handling signals and call sigprocmask in the other threads to stop signals being delivered to them.

The main thread / signal handling thread (it is usually a good idea to make this the main thread) can then send a message to its threads to tell them to finish what they're doing and quit.

Alternatively, if you like the principle of crash-only, you could just call exit_group and be done with it :)

MarkR
@MarkR What is the best way a main thread can send a message to other worker threads to terminate
siri
It really depends what the other threads are doing. In many cases it may be sufficient to set a global variable. If the other threads are waiting for something, then it's necessary to wake them up. There are a lot of cheats such as having a named pipe which you send a single byte on.
MarkR