C Get pid of process launched with execl

2019-07-31 03:08发布

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?

标签: c pid Execl
1条回答
我只想做你的唯一
2楼-- · 2019-07-31 03:45

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.

查看更多
登录 后发表回答