为什么孩子的PID及其过程不同
why pid of child and its process are different?
这是我的代码:
extern int errno;
pid_t system1(const char * command)
{
pid_t pid;
pid = fork();
cout<<"PID in child "<<(int)pid<<endl;
if (pid < 0) {
return pid;
} else if (pid == 0) {
execl("/bin/sh", "sh", "-c", command, (char*)NULL);
_exit(1);
}
int stat_val;
pid_t child_pid;
cout << "Hello1" << endl;
child_pid = wait(&stat_val);
cout << "child_pid = " <<(int)child_pid<< endl;//LINE 1
if(WIFEXITED(stat_val))
printf("Child has terminated with exit code %dn", WIFEXITED(stat_val));
else
printf("Child has existed abnormallyn");
return child_pid;
}
int main( )
{
int pid_1 = system1("setup.csh &");;
struct stat status;
sleep(10);
cout<<"errno = "<<errno<<endl;
int i = kill(pid_1,0);
cout<<"Pid id = "<<pid_1<<endl;
cout<<"i === "<<i<<endl;
cout<<"errno = "<<errno<<endl;
if(errno == ESRCH)
{
cout<<"process does not exist";
}
else
{
cout<<"process exist"<<endl;
}
return 0;
}
在上面的代码中,我在LINE 1
和process
setup.csh
PID
得到了不同的孩子PID
。谁能帮帮我。我想得到我的process
setup.csh
PID
。
我正在使用控制台中的ps -u user | grep setup.csh
寻找其他PID
值。
当您运行时:
sh -c 'setup.csh &'
原始 shell 进程派生另一个子进程以运行csh
。流程层次结构为:
YourProgram
sh -c 'setup.csh &'
csh setup.csh
没有办法直接在原始程序中获取此PID。为什么不直接从程序中运行setup.csh
,而不是通过 shell?
实际上,有一种方法可以做到这一点。如果使用 exec
shell 命令,它将在自己的进程中运行指定的命令,而不是分叉子命令:
system1('exec setup.csh &');
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 是否删除在对象构造过程中创建的对象
- 如何在鼠标挂钩过程中检测拖动
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 更改"child's parent's style sheet"时如何将孩子的样式表重置为 Qt 默认样式表?
- clang 插件:在编译过程中修改 AST
- 如何使变量从 x 到 y,然后从 y 返回到 x 并始终重复该过程
- 在C++中调用 MS SQL 存储过程
- 阻塞信号会导致升压过程不起作用
- 为什么 isVisible 在 QWidget 孩子中不起作用
- OOP使用不同的结构与孩子一起构建建议
- Adafruit 羽毛RFM69HCW在使用过程中会冻结,需要硬重置
- 使用提升过程获取 shell 命令的标准输出
- 附加到 gdb 会中断并且不会继续该过程
- 如何创建始终在线的过程?
- 使用 Qt 测量过程的实际运行时间
- 如何在窗口过程方法中检测击键?
- 开始一个过程,而不是作为一个孩子
- MPI,通过其中一个过程生成一个孩子
- 为什么孩子的PID及其过程不同