Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the child PID after system()

Tags:

linux

fork

exec

As far as I understand, the system() call uses internally fork() and exec() but encapsulates them for a easier handling.

Is it possible to get the PID from the child process created with the system() call?

Aim: I want to be able to SIGINT any child process after a certain timeout. I could rebuild the system() function by using fork() and exec(). But all I need is the child's PID and perhaps there is shortcut by using system()?

like image 392
Maus Avatar asked Dec 23 '22 08:12

Maus


2 Answers

Typically, system() is a synchronous operation. This means it won't return until the child has exited, i.e. there is no valid PID for the child process when system() returns, since the child process no longer exists.

like image 77
unwind Avatar answered Dec 24 '22 21:12

unwind


There's no way (that I know of) when using system(). Besides, with system() there's the additional step of launching a shell which will execute your command, making this a tad more difficult. You're probably better off replacing it with fork() and exec().

like image 27
Michael Foukarakis Avatar answered Dec 24 '22 21:12

Michael Foukarakis