How to print PID of grand child in parent without using pipe? How to count number of child processes and grand child processes?

huangapple go评论67阅读模式
英文:

How to print PID of grand child in parent without using pipe? How to count number of child processes and grand child processes?

问题

以下是代码的翻译部分:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
    FILE *fpPID = fopen("PIDs.txt", "w");
    fclose(fpPID);
    __pid_t child_pid = fork();

    if (child_pid < 0)
    {
        printf("创建子进程失败");
        exit(-1);
    }
    else if (child_pid == 0)
    {
        __pid_t grandChild_pid = fork();
        if (grandChild_pid < 0)
        {
            printf("创建孙子进程失败");
            exit(-1);
        }
        else if (grandChild_pid == 0)
        {
            FILE *fpPID = fopen("PIDs.txt", "a");
            fprintf(fpPID, "%d\n", getpid());
            fclose(fpPID);

            printf("孙子进程:CSM21009\n");
            exit(0);
        }
        else
        {
            printf("子进程:PID:%d,PPID:%d \n", getpid(), getppid());
        }
    }
    else
    {
        sleep(1);
        int GrandChildPID;
        FILE *fpPID = fopen("PIDs.txt", "r");
        fscanf(fpPID, "%d", &GrandChildPID);
        fclose(fpPID);

        printf("父进程:PID:%d,PPID:%d \n", getpid(), getppid());
        printf("父进程:子进程PID:%d,子进程PPID:%d \n", child_pid, getpid());
        printf("父进程:孙子进程PID:%d,孙子进程PPID:%d \n", GrandChildPID, child_pid);
        printf("子进程总数 = 1\n");
        printf("孙子进程总数 = 1 \n");
        exit(0);
    }
}

我已经为您翻译了代码部分,不包括代码中的注释。

英文:

Write a C program to create a child process using the system call fork( ). From the child process, display
the PID and PPID and then call again the fork( ) to create a grandchild and engage him to display your roll
no. From parent display the PID and PPID of all the processes and display the count of total no. of child
processes created also the number of grandchild processes created. Demonstrate the use of exit(0) and
exit(1).

I tried this piece of code

#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;stdlib.h&gt;

int main()
{
    FILE *fpPID = fopen(&quot;PIDs.txt&quot;, &quot;w&quot;);
    fclose(fpPID);
    __pid_t child_pid = fork();

    if (child_pid &lt; 0)
    {
        printf(&quot;Failed to create child process&quot;);
        exit(-1);
    }
    else if (child_pid == 0)
    {
        __pid_t grandChild_pid = fork();
        if (grandChild_pid &lt; 0)
        {
            printf(&quot;Failed to create grandchild process&quot;);
            exit(-1);
        }
        else if (grandChild_pid == 0)
        {
            FILE *fpPID = fopen(&quot;PIDs.txt&quot;, &quot;a&quot;);
            fprintf(fpPID, &quot;%d\n&quot;, getpid());
            fclose(fpPID);

            printf(&quot;GRANDCHILD: CSM21009\n&quot;);
            exit(0);
        }
        else
        {
            printf(&quot;CHILD: PID: %d, PPID: %d \n&quot;, getpid(), getppid());
        }
    }
    else
    {
        sleep(1);
        int GrandChildPID;
        FILE *fpPID = fopen(&quot;PIDs.txt&quot;, &quot;r&quot;);
        fscanf(fpPID, &quot;%d&quot;, &amp;GrandChildPID);
        fclose(fpPID);

        printf(&quot;PARENT: PID: %d, PPID: %d \n&quot;, getpid(), getppid());
        printf(&quot;PARENT: Child PID: %d, Child PPID: %d \n&quot;, child_pid, getpid());
        printf(&quot;PARENT: Grand Child PID: %d, Grand Child PPID: %d \n&quot;, GrandChildPID, child_pid);
        printf(&quot;number of Child = 1\n&quot;);
        printf(&quot;number of GrandChild = 1 \n&quot;);
        exit(0);
    }
}

答案1

得分: 0

Your code looks good overall, but there are a few improvements that can be made to ensure that the child and grandchild processes exit correctly and to properly count the number of child and grandchild processes. Here is the revised code:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    int child_count = 0, grandchild_count = 0;
    pid_t child_pid = fork();

    if (child_pid < 0)
    {
        printf("Failed to create child process");
        exit(-1);
    }
    else if (child_pid == 0)
    {
        child_count++;
        printf("CHILD: PID: %d, PPID: %d\n", getpid(), getppid());
        
        pid_t grandchild_pid = fork();
        if (grandchild_pid < 0)
        {
            printf("Failed to create grandchild process");
            exit(-1);
        }
        else if (grandchild_pid == 0)
        {
            grandchild_count++;
            printf("GRANDCHILD: CSM21009\n");
            printf("GRANDCHILD: PID: %d, PPID: %d\n", getpid(), getppid());
            exit(0);
        }
        else
        {
            int status;
            waitpid(grandchild_pid, &status, 0);
            exit(0);
        }
    }
    else
    {
        int status;
        waitpid(child_pid, &status, 0);

        printf("PARENT: PID: %d, PPID: %d\n", getpid(), getppid());
        printf("
英文:

Your code looks good overall, but there are a few improvements that can be made to ensure that the child and grandchild processes exit correctly and to properly count the number of child and grandchild processes. Here is the revised code:

#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;stdlib.h&gt;

int main()
{
    int child_count = 0, grandchild_count = 0;
    pid_t child_pid = fork();

    if (child_pid &lt; 0)
    {
        printf(&quot;Failed to create child process&quot;);
        exit(-1);
    }
    else if (child_pid == 0)
    {
        child_count++;
        printf(&quot;CHILD: PID: %d, PPID: %d\n&quot;, getpid(), getppid());
        
        pid_t grandchild_pid = fork();
        if (grandchild_pid &lt; 0)
        {
            printf(&quot;Failed to create grandchild process&quot;);
            exit(-1);
        }
        else if (grandchild_pid == 0)
        {
            grandchild_count++;
            printf(&quot;GRANDCHILD: CSM21009\n&quot;);
            printf(&quot;GRANDCHILD: PID: %d, PPID: %d\n&quot;, getpid(), getppid());
            exit(0);
        }
        else
        {
            int status;
            waitpid(grandchild_pid, &amp;status, 0);
            exit(0);
        }
    }
    else
    {
        int status;
        waitpid(child_pid, &amp;status, 0);

        printf(&quot;PARENT: PID: %d, PPID: %d\n&quot;, getpid(), getppid());
        printf(&quot;


</details>



huangapple
  • 本文由 发表于 2023年4月13日 14:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定