zoukankan      html  css  js  c++  java
  • 进程控制编程——Linux编程

    1、进程的创建

        编写一段程序,使用系统调用fork( )创建两个子进程,在系统中有一个父进程和两个子进程活动。让每个进程在屏幕上显示一个字符;父进程显示字符“a”,子进程分别显示字符“b” 和“c”。试观察记录屏幕上的显示结果,并分析原因。

       

     1  # include<stdio.h>
     2 
     3     main()
     4 
     5     { int  p1, p2;
     6 
     7     while((p1=fork())= = -1);
     8 
     9     if(p1= =0)
    10 
    11     putchar(‘b’);
    12 
    13     else
    14 
    15       { while((p2=fork())= =-1);
    16 
    17         if(p2= =0)
    18 
    19         putchar(‘c’);
    20 
    21         else
    22 
    23         putchar( ‘a’);
    24 
    25        }
    26 
    27      } 

    2、 修改已编写的程序,将每个进程的输出由单个字符改为一句话,再观察程序执行时屏幕上出现的现象,并分析其原因。

     1 # include<stdio.h>
     2     main()
     3     { int  p1, p2, i;
     4       while((p1=fork())= = -1);
     5       if(p1= =0)
     6         for(i=0;i<500;i++) 
     7         printf(“child%d
    ”,i);
     8       else
     9        { while((p2=fork())= =-1);
    10          If(p2= =0)
    11             for(i=0;i<500;i++) 
    12             printf(“son%d
    ”,i);
    13          else 
    14             for(i=0;i<500;i++) 
    15             printf(“daughter%d
    ”,i);
    16         }
    17     }

    3、编写程序创建进程树如图1和图2所示,在每个进程中显示当前进程识别码和父进程识别码。

     

    图一

    #include<stdio.h>
    //#include<sys/types.h> /* 提供类型pid_t的定义 */
    #include<unistd.h> /* 提供函数的定义 */
    int main()
    { 
    int  p1,p2,p3;
    while((p1=fork())== -1);
    if(p1==0)
    {
      while((p2=fork())==-1);        
           if(p2==0)
       {
              while((p3=fork())==-1);        
                 if(p3==0)
          {
     //      putchar('d');
           printf("I am D,My pid is %d, my parent's pid is %d
    ", getpid(), getppid());
          }
                  else
          {
     //      putchar('c');
           printf("I am C,My pid is %d, my parent's pid is %d
    ", getpid(), getppid());
          }
       }
          else
       {
     //    putchar('b');
          printf("I am B,My pid is %d, my parent's pid is %d
    ", getpid(), getppid());
       }
    }
    else
    {
    //putchar('a');
    printf("I am A,My pid is %d
    ", getpid());
    }
    printf("
    ");
    getchar();
    } 

    图二

    #include<stdio.h>
    #include<sys/types.h> /* 提供类型pid_t的定义 */
    
    
    #include<unistd.h> /* 提供函数的定义 */
    int main()
    { 
           int p1_B,p1_C,p2_D,p2_E;
           while((p1_B=fork())== -1);
           if(p1_B==0)
       {
         printf("I am B,My pid is %d, my parent's pid is %d
    ", getpid(), getppid());
                  while((p1_C=fork())== -1);
                  if(p1_C==0)
            printf("I am C,My pid is %d, my parent's pid is %d
    ", getpid(), getppid());
        }
          else 
         {
          printf("I am A,My pid is %d
    ",getpid());
                    while((p2_D=fork())==-1);
                   if(p2_D==0)
           {
              printf("I am D,My pid is %d, my parent's pid is %d
    ", getpid(), getppid());
                          while((p2_E=fork())== -1);
                         if(p2_E==0)
             printf("I am E,My pid is %d, my parent's pid is %d
    ", getpid(), getppid());
           }
    }
    getchar();
    }

     

  • 相关阅读:
    竞态与死锁
    Java-核心技术-面试整理
    接口(工厂模式&代理模式)
    switch case实现两个数的算术运算
    继承和多态举例
    字符串的逆序输出
    引用传递&值传递
    递归的使用
    构造方法的重载
    给定数组,去掉0元素后将剩下的元素赋给新的数组
  • 原文地址:https://www.cnblogs.com/WangYiqiang/p/9561552.html
Copyright © 2011-2022 走看看