fork() child and parent processes

后端 未结 5 502
遥遥无期
遥遥无期 2021-02-01 10:37

I am trying to create a program that uses fork() to create a new process. The sample output should look like so:

This is the child process. My pid is 733 and my parent\'

5条回答
  •  梦如初夏
    2021-02-01 11:01

    We control fork() process call by if, else statement. See my code below:

    int main()
    {
       int forkresult, parent_ID;
    
       forkresult=fork();
       if(forkresult !=0 )
       {
            printf(" I am the parent my ID is = %d" , getpid());
            printf(" and my child ID is = %d\n" , forkresult);
       }
       parent_ID = getpid();
    
       if(forkresult ==0)
          printf(" I am the  child ID is = %d",getpid());
       else
          printf(" and my parent ID is = %d", parent_ID);
    }
    

提交回复
热议问题