⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 操作系统的4个程序.txt

📁 操作系统原理的4个进程程序。 进程的软中断通信等。
💻 TXT
字号:
#include <stdio.h>
	main( )
	{
	int p1,p2;
	while((p1=fork( ))= = -1);        /*创建子进程p1*/
	if (p1==0)  putchar('b');        
	else 
	{ 
	while((p2=fork( ))= = -1);   /*创建子进程p2*/
	if(p2==0)  putchar('c');   
	else  putchar('a');     
	}
	}


#include <stdio.h>
	#include <unistd.h>
	main( )
	{
	int p1,p2,i;
            while((p1=fork( ))= = -1);       /*创建子进程p1*/
	if (p1= =0)
	{
	lockf(1,1,0);          /*加锁,这里第一个参数为stdout(标准输出设备的描述符)*/
	for(i=0;i<10;i++)
	   printf("daughter %d\n",i);
	    lockf(1,0,0);                     /*解锁*/
	}
	else
	{
                  while((p2=fork( ))= =-1);  /*创建子进程p2*/
	if (p2= =0)
	{
	lockf(1,1,0);        /*加锁*/
	for(i=0;i<10;i++)
	printf("son %d\n",i);
	lockf(1,0,0);            /*解锁*/
	}
	else
	                   {
	                        lockf(1,1,0);         /*加锁*/
	                        for(i=0;i<10;i++)
	                            printf(" parent %d\n",i);
	                        lockf(1,0,0);         /*解锁*/
	}
	}

#include <stdio.h>
	#include <signal.h>
	#include <unistd.h>

	void waiting( ),stop( );
	int wait_mark;
	main( )
	{
	int p1,p2,stdout;
	while((p1=fork( ))= =-1);       /*创建子进程p1*/
	if (p1>0)
            {
	while((p2=fork( ))= =-1);     /*创建子进程p2*/
	if(p2>0)
	{
                    wait_mark=1;
	signal(SIGINT,stop);   /*接收到^c信号,转stop*/
	waiting( );
	kill(p1,16);        /*向p1发软中断信号16*/
	kill(p2,17);        /*向p2发软中断信号17*/
	wait(0);           /*同步*/
	wait(0);
	printf("Parent process is killed!\n");
	exit(0);
	}
	           else
	             {
                    wait_mark=1;
	signal(17,stop);   /*接收到软中断信号17,转stop*/
	waiting( );
	lockf(stdout,1,0);
	printf("Child process 2 is killed by parent!\n");
	lockf(stdout,0,0);
	exit(0);
	}
	}
	else
	{
	wait_mark=1;
	signal(16,stop);        /*接收到软中断信号16,转stop*/
	waiting( );
	lockf(stdout,1,0);
	printf("Child process 1 is killed by parent!\n");
	lockf(stdout,0,0);
	exit(0);
	}   }
	
	void waiting( )
	{
	 while(wait_mark!=0);
	}
	
	void stop( )
	{
	wait_mark=0;
	}



#include <unistd.h>
	#include <signal.h>
	#include <stdio.h>
	int pid1,pid2;
 
	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,"child 1 process is sending message!"); 
	/*把串放入数组outpipe中*/
	    write(fd[1],outpipe,50);     /*向管道写长为50字节的串*/
	    sleep(5);                 /*自我阻塞5秒*/
	    lockf(fd[1],0,0);
	    exit(0);
            }
	else
           {
	while((pid2=fork( ))= =-1);
	    if(pid2= =0)
	{    lockf(fd[1],1,0);           /*互斥*/
	   sprintf(outpipe,"child 2 process is sending message!");
                 write(fd[1],outpipe,50);
                 sleep(5);
	           lockf(fd[1],0,0);
                 exit(0);
            }
               else
                {  wait(0);              /*同步*/
	         read(fd[0],inpipe,50);   /*从管道中读长为50字节的串*/
                   printf("%s\n",inpipe);
	         wait(0);
	         read(fd[0],inpipe,50);
	         printf("%s\n",inpipe);
                   exit(0);
               }
        }
	}

1)进程的软中断通信 

   <任务1> 

   编制一段程序,使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按Del键),当捕捉到中断信号后,父进程用系统调用kill() 向两个子进程发出信号,子进程捕捉到信号后,分别输出下列信息后终止: 

   child process l is killed by parent! 

   child process 2 is killed by parent!   

    父进程等待两个子进程终止后,输出以下信息后终止: 

   parent process is killed! 

   (程序) 

   

/*e-1-31*/ 

#include <stdio.h> 

#include <signal.h> 

#include <unistd.h> 

#include <stdio.h> 

#include <sys/types.h> 

  

int wait_flag; 

void stop(); 

  

main() { 

    int pid1,pid2; 

  

    signal(3,stop);  //or signal(14,stop); 

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

    if(pid1>0) { 

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

       if(pid2>0) { 

           // wait_flag=1; 

           sleep(5); 

           kill(pid1,16); 

           kill(pid2,17); 

           wait(0); 

           wait(0); 

           printf("Parent process is killed !!\n"); 

           exit(0); 

       } 

       else { 

           // wait_flag=1; 

           signal(17,stop); 

           printf("Child process 2 is killed by parent !!\n"); 

           exit(0); 

       } 

    } 

    else { 

       // wait_flag=1; 

       signal(16,stop); 

       printf("Child process 1 is killed by parent !!\n"); 

       exit(0); 

    } 

} 

  

void stop() { 

    wait_flag=0; 

} 

  

  

   (运行结果) 

   child process 1 is killed by parent! 

   child process 2 is killed by parent! 

   Parent process is killed! 

   分析 

   (1)上述程序中,实用函数signal()都放在一段程序的前面部位,而不是在其他接收信号处。这是因为signal()的执行只是为进程指定信号量16或17的作用,以及分配相应的与 stop()过程链接的指针。从而signal()函数必须在程序前面部分执行。 

   (2)该程序段前面部分用了两个wait(0),为什么?请读者思考。 

   (3)该程序段中每个进程退出时都用了语句exit(0),为什么?请读者思考。 

   <任务2> 

   在上面任务1中,增加语句signal(SIGINT,SIG_IGN)和语句signal(SIGQUIT, SIG_IGN),观察执行结果,并分析原因。这里signal(SIGINT,SIG_IGN)和signal(SIGQUIT,SIG_IGN)分别为忽略'Del'键信号以及忽略中断信号。 

   (程序) 

   

/*e-1-32*/

#include<unistd.h>

#include<signal.h>

#include<stdio.h>

int pid1,pid2;  

int EndFlag=0;

   //pf1=0;

   //pf2=0;

  void IntDelete()

   {

   kill(pid1,10);

   kill(pid2,12);

   EndFlag=1;

   }

   void Int1()

   {

   printf("child process 1 is killed ! by parent\n");

   exit(0);

   }

   void Int2()

   {

   printf("child process 2 is killed ! by parent\n");

   exit(0);

   }

   waiting()

   { while(EndFlag==0);}

  

   main()

   {

   //int exitpid;

   signal(SIGINT,SIG_IGN);

   signal(SIGQUIT,SIG_IGN);

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

   if(pid1==0)

   {

   //signal(SIGINT,SIG_IGN);

   signal(10,Int1);

   signal(SIGINT,SIG_IGN);

   pause();

   exit(0);

   }

   else

   {

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

   if(pid2==0)

   {

   //signal(SIGINT,SIG_IGN);

   signal(12,Int2);

   signal(SIGINT,SIG_IGN);

   pause();

   exit(0);

   }  

   else

   {

   signal(SIGINT,IntDelete);

   //waitpid(-1,&exitcode,0);

   waiting();

   printf("parent process is killed\n");

   exit(0);

   }

   }

   }

   <运行结果> 

   请读者将上述程序输入计算机后,执行并观察。 

   <分析>  由于忽略了中断与退出信号,程序会一直保持阻塞状态而无法退出。 

   2)进程的管道通信 

   (任务) 

   编制一段程序,实现进程的管道通信。使用系统调用pipe()建立一条管道线。两个子进程p1和p2分别向管道各写一句话: 

   child 1 is sending message! 

   child 2 is sending message! 

   而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。 

   (程序) 

  /*e-1-4*/  

  #include<unistd.h>

   #include<signal.h>

   #include<stdio.h>

   int pid1,pid2;

   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,"child 1 process is sending message!");

   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,"child 2 proeess is sending message!");

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

   sleep(5);

   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);

   }

   }

   }

   (运行结果) 

   child 1 is sending message! 

   child 2 is sending message!    

   (分析) 

   请读者自行完成。 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -