views:

511

answers:

3

i'm working with a multi-threaded program (using pthreads) that currently create a background thread (PTHREAD_DETACHED) and then invokes pthread_exit(0). My problem is that the process is then listed as "defunct" and curiously do not seems to "really exists" in /proc (which defeats my debugging strategies)

I would like the following requirements to be met:

  • the program should run function A in a loop and function B once
  • given the PID of the program /proc/$pid/exe, /proc/$pid/maps and /proc/$pid/fd must be accessible (when the process is defunct, they are all empty or invalid links)
  • it must be possible to suspend/interrupt the program with CTRL+C and CTRL+Z as usual

edit: I hesitate changing the program's interface for having A in the "main" thread and B in a spawned thread (they are currently in the other way). Would it solve the problem ?

+1  A: 

Is there a reason you can't do things the other way round: have the main thread run the loop, and do the one-off task in the background thread?

Anthony Williams
If that would fix the problem, i'd face the challenge of reorganising the software so that it can be done. It's my first real exposure to pthreads, actually.
sylvainulg
A: 

Not the most elegant design but maybe you could block the main thread before exiting with:

 while(1) {
       pause();
 }

Then you can install a signal handler for SIGINT and SIGTERM that breaks the loop. The easiest way for that is: exit(0) :-).

tsg
+2  A: 

You can either suspend the execution of the main process waiting for a signal, or don't detach the thread (using the default PHTREAD_CRATE_JOINABLE) waiting for its termination with a pthread_join().

Nicola Bonelli