shafi
shafi

Reputation: 145

How to get the pid of a process started using system call?

In a C program, in main I am calling a system function using system(). Now I want the pid of that process started by system(). Is there any way to get that pid ?

Upvotes: 0

Views: 483

Answers (1)

Toby Speight
Toby Speight

Reputation: 30841

No, and in general it's not useful. When the call of system() returns to your program, the child process has terminated and been reaped, so there's no process (not even a zombie process) for it to reference.

If you need to start a process and retain its PID, you'll need to fork() a child yourself (noting the returned value in the parent), and in the child, exec() the command. In the parent, you now have the PID, and can use it (e.g. in waitpid()).

Upvotes: 4

Related Questions