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.