0

I have main program from which I create two threads using pthread_create(). In one thread, I call

Thread I { ... system ("binary application");

}

System() internally forks a child process. How can I kill that " binary application" from main program??

Hanu
  • 57
  • 6

1 Answers1

0

That's not directly supported.

You need the PID to kill a process, and system() is designed for the synchronous execution of some command — it doesn't expose the PID of the invoked command. Indeed, system() might spawn several PIDs, several generations of descendants, probably /bin/sh and then your binary-application.

How would you kill the binary-application from an external process (not a thread, a completely external process)? However you'd do that might be how your killing thread can get the PID.

It's probably easier to set an alarm on the command, or instead call fork() (which gives you the PID) and exec() in your own code. In any case, system() in a multithreaded program can be tricky, so take care.

pilcrow
  • 56,591
  • 13
  • 94
  • 135
  • that "binary application" can be killed when SIGINT can be received by it. But system() blocks the SIGINT signal.how can we make that SIGINT be received by it? It would be of immense timely help if you could leave some ideas on this. – Hanu Jul 15 '20 at 05:29
  • @Hanu You are not looking at the other side of the problem. How are you planning to send ``SIGINT``? You need process PID for that. There is no standard way to obtain it when calling ``system``. – m0hithreddy Jul 16 '20 at 16:43
  • @MohithReddy : SIGINT is being passed to the child process. I confirmed it on the system() man page which says SIGINT will be paseed to child process. its the thread from which this child process is launched, will block SIGINT on the thread. – Hanu Jul 18 '20 at 04:35