1

I want to show a PID at the beginning of my C++ program and send a wait signal, so, that I can run gdb - PID in another terminal. Then by pressing run in the gdb terminal, the program should continue.

If I use

   pid_t pid = getpid();
   cout << "PID = " << pid << endl;
   raise(SIGSTOP);

I see this in the output

$ ./run
PID = 30261

[1]+  Stopped                 ./run
$

So, the program goes in the background and stops. On the other hand if I use SIGINT, I see

$ ./run
PID = 8790
Caught signal: 2
$ 

How can I fix that?

mahmood
  • 23,197
  • 49
  • 147
  • 242

2 Answers2

2

So, the program goes in the background and stops

What stops you from attaching to a stopped process?

You can now attach to a process with pid number 30261 and continue program execution. See also related question wait for gdb to attach.

gdb ./run 30261
ks1322
  • 33,961
  • 14
  • 109
  • 164
1

You don't need to use a signal to pause your application, you could just wait for a certain signal you are interested in.

int main(){
    std::cout << "PID = "  << getpid() << std::endl;

    int sig;
    sigset_t signal_set;

    sigemptyset(&signal_set);
    sigaddset(&signal_set, SIGCONT); 
    sigwait( &signal_set, &sig  );
 
    std::cout << "continued"  << std::endl;
}
t.niese
  • 39,256
  • 9
  • 74
  • 101