C Get pid of process launched with execl

2019-07-31 03:32发布

问题:

I'm launching a process with the instruction

execl("./softCopia","softCopia",NULL);

softCopia is just a dummy that writes integers in a file.

I would like to know how can i get the pid of this process?

回答1:

Since all of the Unix exec functions replace the running process with the new one, the PID of the exec'd process is the same PID it was before.

So you get the PID by using the getpid() call, before calling execl.

Or, if you actually want to continue running your main program and launch a new program, you use fork() first. The fork() function returns a negative value for errors, 0 for the new, child process, and the PID of the child in the parent. So the parent can then use one of the wait functions or just continue on its business until later.



标签: c pid Execl