操作系统进程管理实验

实验题目:

(1) 进程的创建编写一段程序,使用系统调用fork( )创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符“a”;子进程分别显示字符“b”和字符“c”。试观察记录屏幕上的显示结果,并分析原因。(2) 进程的控制修改已编写的程序,将每个进程输出一个字符改为每个进程输出一句话,在观察程序执行时屏幕上出现的现象,并分析原因。(3) 编制一段程序,使其实现进程的软中断通信。要求:使用系统调用fork( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上来的中断信号(即按Del键);当捕捉到中断信号后,父进程调用系统调用kill( )向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:   Child process 1 is killed by parent!   Child process 2 is killed by parent! 父进程等待两个子进程终止后,输出如下的信息后终止:   Parent process is killed! 在上面的程序中增加语句signal(SIGINT, SIG_IGN)和 signal(SIGQUIT, SIG_IGN),观察执行结果,并分析原因。(4) 进程的管道通信编制一段程序,实现进程的管道通信。使用系统调用pipe( )建立一条管道线;两个进程P1和P2分别向管道各写一句话: Child 1 is sending a message! Child 2 is sending a message! 而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。要求父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。

实验源程序及报告:

(1)、进程的创建

 #include <stdio.h>

int main(int argc, char *argv[])

{    

        int pid1,pid2;     /*fork first child process*/    

        if ( ( pid1=fork() ) < 0 )    

        {     

                printf( "ProcessCreate Failed!");       

                 exit(-1);    

          }    

         if ( ( pid1=fork() ) == 0 )   

         {

                  printf( "b/n" );    

          }    

            /*fork second child process*/   

           if ( ( pid2=fork() ) < 0 )    

           {  

                   printf( "ProcessCreate Failed!");        

                  exit(-1);  

             }    

             if ( ( pid2=fork() ) == 0 )    

              {  

                     printf( "c/n" );    

                }   

                /*parent process*/    

               else   

               {

                         wait(NULL);

                        printf( "a/n" );  

                        exit(0);

                 }  

               return 0;

}

 

(2)、进程的控制

#include <stdio.h>

 int main(int argc, char *argv[])

 {      int pid1,pid2;

        /*fork first child process*/   

        if ( ( pid1=fork() ) < 0 )    

        {       

                printf( "ProcessCreate Failed!");     

                exit(-1);    

          }

          if ( ( pid1=fork() ) == 0 )

          {

                 printf( "This is my Unix OS program!/n" );    

          }    

       /*fork second child process*/  

         if ( ( pid2=fork() ) < 0 )

        {

               printf( "ProcessCreate Failed!");

              exit(-1);

        }

        if ( ( pid2=fork() ) == 0 )  

        {  

               printf( "This is the second Child process!/n" );

         }  

         /*parent process*/   

        else   

        {

                wait(NULL);

                printf( "This is the Parent process/n" );

                exit(0);

         }  

         return 0;

}

(3) 编制一段程序,使其实现进程的软中断通信。

#include <stdio.h>

#include <signal.h>  

 #include <unistd.h> 

#include <sys/types.h> 

int wait_flag; void stop( );

main( )

{  

         int pid1, pid2;   signal(3, stop);

         while ((pid1 = fork( )) == -1);

         if ( (pid1 = fork() ) > 0)  

         {  

                  while ((pid2 = fork( )) == -1);

                 if (( pid2 = fork()) > 0 )  

                  {      

                            wait_flag = 1;  signal(SIGINT, stop);   

                             sleep(5);  

                             kill(pid1, 16);  

                             kill(pid2,17);         

                             wait(0);    

                             wait(0);  

                             printf("/n Parent process is killed./n");

                            exit(0);    

                   }      

                  else     

                   {    

                              wait_flag = 1;   

                              signal(17, stop);    

                               printf("/n Child process 2 is killed by parent./n");       

                               exit(0);     

                       }   

            }        

           else

            {           

                       wait_flag = 1;                                                

                      signal(16, stop);   

                      printf("/n Child process 1 is killed by parent./n");   

                      exit(0);   

                }        

void stop( )

 {     

           wait_flag = 0;  

  }

(4) 进程的管道通信

#include <unistd.h>

#include <signal.h>

#include <stdio.h>

int pid1,pid2;

int main()

 {   

            int fd[2];  

           char OutPipe[100], InPipe[100];   

           pipe(fd);   

            while((pid1 = fork()) == -1);

            if( pid1 == 0)   

            {   

                     lockf( fd[1], 1, 0 );

                    sprintf(OutPipe, "/n Child process 1 is sending message!/n");    

                    write( fd[1], OutPipe, 50);

                    sleep(5);   

                    lockf(fd[1], 0 , 0);   

                   exit(0);   

              }

              else     

              {      

                       while((pid2 = fork()) == -1);  

                       if( pid2 == 0)    

                       {        

                                lockf( fd[1], 1, 0 );  

                                sprintf(OutPipe, "/n Child process 2 is sending message!/n");       

                                 write( fd[1], OutPipe, 50);

                                sleep(3);   

                                 lockf(fd[1], 0 , 0);  

                                exit(0);

                          }       

                         else

                         {   

                                wait(0);     

                                read(fd[0], InPipe, 50);        

                                printf("%s/n", InPipe);    

                                wait(0);   

                                 read(fd[0], InPipe, 50);

                                 printf("%s/n", InPipe);  

                                  exit(0);  

                             }

           }

         return 0;

}

三、程序注释:

1)进程的创建

  源代码:

 #include <stdio.h>

int main(int argc, char *argv[])

{

    int pid1,pid2;

    /*调用fork函数创建进程1*/

    if ( ( pid1=fork() ) < 0 )   

    {  /*fork()返回值为负数,则创建进程失败*/

       printf( "ProcessCreate Failed!");

       exit(-1);

}

/**

/*fork()返回值为0,则创建子进程成功且当前进程为子进程

**/

    if ( ( pid1=fork() ) == 0 )

    {

       printf( "b/n" );    //子进程1输出’b’;

    }

/**

/*创建子进程2

***/

    if ( ( pid2=fork() ) < 0 ) /*fork()返回值为负数,则创建进程失败*/

    {

       printf( "ProcessCreate Failed!");

        exit(-1);

}

/**

/*fork()返回值为0,则创建子进程成功且当前进程为子进程

**/

    if ( ( pid2=fork() ) == 0 )

    {

       printf( "c/n" );  //子进程2输出’c’;

    }

   /*fork()返回值大于0,则当前进程为父进程*/

    else           

        {

           wait(NULL);

           printf( "a/n" );  //父进程输出’a’;

           exit(0);

        }

    return 0;

}

     

2)进程的控制

源代码:

 #include <stdio.h>

int main(int argc, char *argv[])

{

    int pid1,pid2;

    /*调用fork函数创建进程1*/

    if ( ( pid1=fork() ) < 0 )   

    {  /*fork()返回值为负数,则创建进程失败*/

       printf( "ProcessCreate Failed!");

       exit(-1);

}

/**

/*fork()返回值为0,则创建子进程成功且当前进程为子进程

**/

    if ( ( pid1=fork() ) == 0 )

    {

       printf( "This is my Unix OS program!/n" );    //子进程1输出句子;

    }

/**

/*创建子进程2

***/

    if ( ( pid2=fork() ) < 0 ) /*fork()返回值为负数,则创建进程失败*/

    {

       printf( "ProcessCreate Failed!");

        exit(-1);

}

/**

/*fork()返回值为0,则创建子进程成功且当前进程为子进程

**/

    if ( ( pid2=fork() ) == 0 )

    {

       printf( "This is the second Child process!/n" );  //子进程2输出句子;

    }

   /*fork()返回值大于0,则当前进程为父进程*/

    else           

        {

           wait(NULL);

           printf( "This is the Parent process/n" ); //父进程输出句子;

           exit(0);

        }

    return 0;

}

 

 

3)编制一段程序,使其实现进程的软中断通信。

源代码:(软中断用Ctrl+ | 实现)

#include <stdio.h>

#include <signal.h>    

#include <unistd.h> 

#include <sys/types.h> 

     

int wait_flag;

void stop( ); //stop()是自己定义的Signal()触发的自定义函数

main( )

{

   int pid1, pid2; //定义两个进程号参数

   signal(3, stop); //Signal()触发软中断

   while ((pid1 = fork( )) == -1); //程序等待成功创建子进程事件的发生

   if ( (pid1 = fork() ) > 0)

  {        

     while ((pid2 = fork( )) == -1);

     if (( pid2 = fork()) > 0 )

     {  /*当前进程为父进程,父进程发出两个软中断信号Kill子进程*/

        wait_flag = 1;

       signal(SIGINT, stop);

        sleep(5);

        kill(pid1, 16);

        kill(pid2,17);

        wait(0);     //等待子进程死信号

        wait(0);  

        printf("/n Parent process is killed./n"); //接收到子进程死信号后,杀死父进程

        exit(0);  

      }

      else

      {  /**

         /*当前进程为子进程,则发送子进程Kill信号,杀死该子进程2

         **/

         wait_flag = 1;

         signal(17, stop);          

         printf("/n Child process 2 is killed by parent./n");

         exit(0);                                    

       }

    }                               

    else {  /**

         /*当前进程为子进程,则发送子进程Kill信号,杀死该子进程1

         **/                         

            wait_flag = 1; 

            signal(16, stop); 

            printf("/n Child process 1 is killed by parent./n");               

            exit(0);

   }                             

} 

void stop( )

{    /**

     /*自定义函数,供signal()调用

    **/

   wait_flag = 0;                 

}  

 

 

4)进程的管道通信

 

源代码:

#include <unistd.h>

#include <signal.h>

#include <stdio.h>

 

int pid1,pid2;  //定义两个进程号参数

 

int main()

{

   int fd[2];  /*定义一个数组作为sys_pipe()的参数*/

   char OutPipe[100], InPipe[100];

   pipe(fd);  /*调用sys_pipe()创建管道线*/

   while((pid1 = fork()) == -1);

   if( pid1 == 0)

   {   /**

       /**根据sys_pipe()函数的定义fd[1]用于向管道写数据

         */

       lockf( fd[1], 1, 0 );  /*锁定管道写入端fd[1]*/

       /*进程1向管道写入要输出的句子*/

       sprintf(OutPipe, "/n Child process 1 is sending message!/n");

       write( fd[1], OutPipe, 50);

       sleep(5);

       lockf(fd[1], 0 , 0);

       exit(0);

    }

    else

    {

       while((pid2 = fork()) == -1);

       if( pid2 == 0)

       {

          lockf( fd[1], 1, 0 ); /*锁定管道写入端fd[1]*/

         /*进程2向管道写入要输出的句子*/

          sprintf(OutPipe, "/n Child process 2 is sending message!/n");

          write( fd[1], OutPipe, 50);  /*调用pipe_write()函数写管道*/

          sleep(3);

          lockf(fd[1], 0 , 0);

          exit(0);

       }

       else

       {

          wait(0);

          read(fd[0], InPipe, 50); /*调用pipe_read()函数读管道*/

          printf("%s/n", InPipe);

          wait(0);

          read(fd[0], InPipe, 50); /*调用pipe_read()函数读管道*/

          printf("%s/n", InPipe);

          exit(0);

       }

    }

   

   return 0;

}

 

  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值