C 获取使用 execl 启动的进程的 pid [英] C Get pid of process launched with execl

查看:102
本文介绍了C 获取使用 execl 启动的进程的 pid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用指令启动一个进程

I'm launching a process with the instruction

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

softCopia 只是一个在文件中写入整数的虚拟程序.

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

我想知道如何获得这个进程的pid?

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

推荐答案

由于所有 Unix exec 函数替换正在运行的进程为新的,因此 exec 进程的 PID 是与之前相同的 PID.

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.

所以你通过使用 getpid() 调用获得 PID,before 调用 execl.

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

或者,如果您确实想继续运行主程序并启动新程序,请先使用 fork().fork() 函数为错误返回负值,为新的子进程返回 0,以及父进程中子进程的 PID.因此,父级可以使用 wait 功能之一,或者只是继续其业务,直到稍后.

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 获取使用 execl 启动的进程的 pid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆