【问题标题】:C: Parent and Child ProcessesC:父子进程
【发布时间】:2023-03-05 21:26:01
【问题描述】:

我正在尝试执行以下操作...

Create a new process
obtain the PID of a process
put a process to sleep for a defined period of time
check process ID on the terminal 

我的程序运行,但输出不是我期望的那样,我不太确定哪里出错了。感谢您的宝贵时间,我真的很感激!

代码:

int main() {
    int i;
    pid_t pid=0;

    /** use fork() system call to create a new process */
    /** if the pid returned by fork() is negative, it indicates an error */

    if(fork()<0) {
        perror("fork");
        exit(1);
    }

    if(pid==0) {
        printf("child PID= %d\n", (int) getpid());
        for(i=0; i<10; i++) {
            printf("child: %d\n", i);
            /** put process to sleep for 1 sec */
            sleep(1);
        }
    } else {
        /* parent */
        /** print the parent PID */
        printf("parent PID= %d\n", (int) getpid());
        for(i=0; i<10; i++) {
            printf("parent: %d\n", i);
            sleep(1);
        }
    }

    exit(0);
}

输出应该看起来像......

parent PID=8900
child PID=4320
parent:0
child:0
child:1
parent:1
child:2
parent:2
child:3
parent:3
parent:4
etc.

但我越来越……

child PID= 97704
child: 0
child PID= 106388
child: 0
child: 1
child: 1
child: 2
child: 2
child: 3
child: 3
child: 4
child: 4
child: 5
child: 5
child: 6
child: 6
child: 7
child: 7

【问题讨论】:

  • if ( fork() &lt; 0 ) --> if ( (pid=fork()) &lt; 0 )

标签: c process fork parent child-process


【解决方案1】:

您并没有真正将 fork() 的输出分配给 pid,因此 pid 保持为零。

【讨论】:

    【解决方案2】:

    如上所述,您没有将pid 分配给任何东西,因此它始终为零。您还应该将您的条件更改为pid,而不是调用另一个fork()

    int main() {
    int i;
    pid_t pid=0;
    
    pid = fork(); /* Add this */
    
    /** use fork() system call to create a new process */
    /** if the pid returned by fork() is negative, it indicates an error */
    
    if(pid<0) { /* Change this */
        perror("fork");
        exit(1);
    }
    

    此外,如果您的预期输出仍然与您的预期不同,请不要感到惊讶。无法确定何时会打电话给孩子或父母(尤其是如果您睡着了)。这取决于很多事情。

    编辑:我明白你在说什么。您想通过终端检查进程 ID?您可以在程序末尾添加getchar(); 以暂停程序退出,然后您可以打开另一个终端并运行ps。您需要确保添加 #include &lt;stdio.h&gt; 才能使用它。

    【讨论】:

    • Awww 好吧好吧,我现在明白了。我将如何检查进程 ID?有人告诉我,在执行时键入 ps 会显示它,但到目前为止 ps 直到程序完成运行后才会执行
    • 我有点困惑。 ps 是一个 shell 命令,用于报告有关当前进程的信息。要检查进程 id,只需检查 pid 的值,就像你做的那样。
    • @Bob 看我上面的编辑。这是您可以从终端获取程序的进程 ID 的一种方式。
    【解决方案3】:

    使用pid 进行比较,而不是调用另一个fork()。将pid 设置为等于fork(),以便您可以比较它以检查pid 中的错误。

    int main() {
        int i;
        pid_t pid=0;
        pid = fork();
    
        /** use fork() system call to create a new process */
        /** if the pid returned by fork() is negative, it indicates an error */
    
        if(pid<0) {
            perror("fork");
            exit(1);
        }
    
        if(pid==0) {
            printf("child PID= %d\n", (int) getpid());
            for(i=0; i<10; i++) {
                printf("child: %d\n", i);
               /** put process to sleep for 1 sec */
                sleep(1);
            }
        } else {
           /* parent */
            /** print the parent PID */
            printf("parent PID= %d\n", (int) getpid());
            for(i=0; i<10; i++) {
                printf("parent: %d\n", i);
                sleep(1);
           }
        }
    
        exit(0);
    }
    

    【讨论】:

    • 请查看此页面stackoverflow.com/help 并尝试重新提出您的问题,以便其他人可以更好地帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    相关资源
    最近更新 更多