Linux进程编程练习

1、分析理解多个进程的创建“

1)若一个程序中有这样的代码,则有几个进程,父子关系如何?

#include

#include

#include

#include

int main(void){

         pid_t pid,pid2;

         char *message;

         int x;

         pid = fork();

pid2 = fork();

         if (pid < 0)

         {        perror("fork failed");

                   exit(1);     }

 

    补充完整后面的代码,使多个进程输出各自身份,并符合正确的亲缘关系

         if (  )

         {        message = "This is the child\n";

                   x = 0;       }

 

    printf("%s I'm %d, x=%dmy father is:%d\n",message,getpid(),getppid());

         return 0;

}//main

给出代码及执行效果抓图,并要有说明分析!!!!谁先谁后,为什么。

#include

#include

#include

#include

int main(void){

         pid_t pid,pid2;

         char *message;

         int x;

         pid = fork();

         pid2 = fork();

         if (pid < 0)

         {        perror("fork failed");

                   exit(1);     }

         if (pid == 0)

         {       

                   if (pid2>0)

                   {

                            message = "This is the son1\n";

                        x = 0;

                   }else {

                            message = "This is the pid's son\n";

                        x = 0;

                   }

                  

         }

         else if (pid>0&&pid2==0)

         {       

                   message = "This is the son2\n";

                   x = 1;

         }

         else{

                   message = "This is the parent\n";

                       x = 10;

         }

     printf("%s I'm %d, x=%dmy father is:%d\n",message,getpid(),x,getppid());

         return 0;

}

Linux进程编程练习_第1张图片

主进程通过pid=fork()创建出子进程son1,主进程和子进程继续运行,通过pid2=fork()分别创建各自子进程,

主进程创建出son2子进程,son1子进程创建出自己的pid’s son子进程另一个进程延续son1.

 

2)若有如下的代码

for(i = 0; i < 5; i++)

    {

        pid = fork();

……

请分析将产生多少个进程?

 

第一次创建产生一个子进程。加上主进程共2个。

第二次子进程产生两个子进程,主进程产生一个子进程,共4

……

第四次的时候共有32个进程

2、若有下面的功能代码,执行会有怎样的输出?不只有抓图,要有说明分析!!!!谁先谁后,哪里停顿过。

int main(void){

         pid_t a;

         a = fork();

         if (a == 0) {

                   sleep(2);

                   execlp ("ps" ,"ps",NULL);       

    printf("%s I'm %d, x=%dmy father is:%d\n",message,getpid(),getppid());

         }

         else 

    printf("%s I'm %d, x=%dmy father is:%d\n",message,getpid(),getppid());

return 0;

}

Linux进程编程练习_第2张图片

先是a>0时父进程输出,然后a==0,运行子进程内的代码,停顿两秒,然后运行exec,没有报错,因此跳过exec下面的输出语句

3. 给出观察到僵死态进程的代码和执行效果。

 

#include

#include

#include

#include

int main(void){

         pid_t pid;

         char *message;

         int x;

         pid = fork();

         if (pid < 0)

         {        perror("fork failed");

                   exit(1);     }

         if (pid == 0)

         {        message = "This is the child\n";

                   x = 0;       }

         else

         {        message = "This is the parent\n";

                   x = 10;

                   sleep(10);

         }

        printf("%s I'm %d, x=%dmy father is:%d\n",message,getpid(),getppid());

         return 0;

}

Linux进程编程练习_第3张图片

 

4.写代码,使其符合如下的执行逻辑,并给出执行效果及分析。

Linux进程编程练习_第4张图片

#include

#include

#include

#include

#include

int main(){

         pid_t pc1,pc2,pw1,pw2;

         pc1=fork();

         pc2=fork();

         if (pc1>0&&pc2>0)

         {    

                   pw1=wait(NULL);

                   pw2=wait(NULL);

                   printf("***Catch a dead child process with pid: %d\n",pw1);

                   printf("***Catch a dead child process with pid: %d\n",pw2);

                   printf("***I'M %d,THE MAIN PROCESS LEAVE!\n",getpid()); 

         }

         if (pc1==0&&pc2>0)

         {

              printf("===I'M the first child PID:%d,my father is:%d\n",getpid(),getppid());

              sleep(10);                                  

         }

         if (pc1>0&&pc2==0) 

             printf("===I'M the 2nd child PID:%d,my father is:%d,i don't sleep.\n",getpid(),getppid());

         if (pc1==0&&pc2==0)

             printf("I'M grandson PID:%d,my father is:%d,no one is waiting for me.\n",getpid(),getppid());

         exit(0);              

}

Linux进程编程练习_第5张图片

创建子进程之后,四个进程并发执行,主进程设置wait等待子进程的死亡,然后回收子进程,只回收一个子进程

你可能感兴趣的:(Linux)