【问题标题】:How can you make a parent process display PID of child instead of value variable?如何让父进程显示子进程的 PID 而不是值变量?
【发布时间】:2013-12-15 20:59:15
【问题描述】:

在linux中如何让父进程显示子进程的PID而不是value变量?

【问题讨论】:

  • 使用哪种语言? bashC ?
  • @BasileStarynkevitch C 请

标签: linux process pid


【解决方案1】:

你获得子 PID 的机会是在你 fork 的时候。

Python:

import os

child_pid = os.fork()
if child_pid == 0:
    print "This is the child, my pid is", os.getpid()
else:
    print "This is the parent, my child pid is", child_pid

C:

pid_t child_pid = fork();
if (child_pid == 0) {
    printf("This is the child, my pid is %d\n", getpid());
}
else {
    printf("This is the parent, my child pid is %d\n", child_pid);
}

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 2015-12-23
    • 2021-11-07
    • 2010-09-05
    • 1970-01-01
    • 2017-04-15
    • 2014-06-25
    • 2022-12-02
    相关资源
    最近更新 更多