北邮-操作系统-实验2-代码--进程管理-实验报告(共6页).docx
精选优质文档-倾情为你奉上实验2 进程管理(1) 进程的创建:编写一段程序,使用系统调用fork() 创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符“a”,子进程分别显示字符“b”和“c”。试观察记录屏幕上的显示结果,并分析原因。实验代码:#include <unistd.h>#include <stdio.h>main()pid_t pid1,pid2;pid1=fork();if(pid1=0)putchar('b');else if(pid1<0)return -1;elsepid2=fork();if(pid2=0)putchar('c');else if(pid2<0)return -1;else putchar('a');运行结果bca(会出现acb等任意的排列),如图分析原因进程执行并发,输出排列是随机的,fork()创建进程所需的时间一般多于输出一个字符的时间,所以输出a较晚,但各个进程的时间片的获得却不是一定是顺序的。(2)进程的控制修改已经编写的程序,将每个进程输出一个字符改为每个进程输出一句话,再观察程序执行时屏幕上出现的现象,并分析原因。实验代码#include <unistd.h>#include <stdio.h>main()pid_t pid1,pid2;pid1=fork();if(pid1=0)printf("this is process b.n");else if(pid1<0)return -1;elsepid2=fork();if(pid2=0)printf("this is process c.n");else if(pid2<0)return -1;else printf("this is father process.n");运行结果this is father process.this is process c.this is process b.分析原因由于函数printf()输出的字符串之间不会被中断,因此,每个字符串内部的字符顺序输出时不变。但是,由于进程并发执行时的调度顺序会不同,输出字符串的顺序和先后随着执行的不同而发生变化。后建的进程c可以在进程b之前完成。父进程需要创建进程,执行输出较晚,这与打印单字符的结果相同。(3)进程通信a) 编写一段程序,使其实现进程的软中断通信。要求:使用系统调用fork() 创建两个子进程,再用系统调用signal() 让父进程捕捉键盘上来的中断信号(即按DEL键);当捕捉到中断信号后,父进程用系统调用Kill() 向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:Child Process 1 is killed by Parent!Child Process 2 is killed by Parent!父进程等待两个子进程终止后,输出如下的信息后终止:Parent Process is killed!实验代码#include<stdio.h>#include<signal.h>#include<unistd.h>void waiting();void stop();void alarming();int wait_mark;main() int pid1,pid2;pid1=fork(); if(pid1>0) pid2=fork(); if(pid2>0) wait_mark=1; signal(SIGINT,stop);/*接收到c信号,转stop*/ signal(SIGALRM,alarming);/*接受SIGALRMwaiting();*/ while(wait_mark!=0); kill(pid1,16);/*向pid1发软中断信号16*/ kill(pid2,17);/*向pid2发软中断信号17*/ wait(0);/*同步*/ wait(0); printf("Parent process is killed!n"); exit(0); else if(pid2=0) wait_mark=1; signal(17,stop); signal(SIGINT,SIG_IGN);/*忽略c信号*/ while(wait_mark!=0) lockf(1,1,0); printf("Child Process 2 is killed by Parent!n"); lockf(1,0,0); exit(0); else if(pid1=0) wait_mark=1;signal(16,stop); signal(SIGINT,SIG_IGN);/*忽略c信号*/ while(wait_mark!=0) lockf(1,1,0); printf("nChild Process 1 is killed by Parent!n"); lockf(1,0,0); exit(0); void waiting() sleep(5); if(wait_mark!=0) kill(getpid(),SIGALRM);void alarming() wait_mark=0;void stop() wait_mark=0;运行结果<键盘输入Ctrl+C>Child Process 1 is killed by Parent!Child Process 2 is killed by Parent!Parent Process is killed!(4)进程的管道通信编制一段程序,实现进程的管道通信。使用系统调用pipe() 建立一条管道线;两个子进程P1和P2分别向管道各写一句话:Child 1 is sending a message!Child 2 is sending a message!而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。要求父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。实验代码#include <unistd.h>#include <signal.h>#include <stdio.h>int pid1,pid2;main()int fd2;char outpipe100,inpipe100;pipe(fd);/*创建一个管道*/while(pid1=fork()=-1);if(pid1=0)lockf(fd1,1,0);sprintf(outpipe,"Child 1 is sending a message!");/*把串放入数组outpipe中*/write(fd1,outpipe,50);/*向管道写长为50字节的串*/sleep(5);/*自我阻塞5秒*/lockf(fd1,0,0);exit(0);elsewhile(pid2=fork()=-1);if(pid2=0)lockf(fd1,1,0);/*互斥*/sprintf(outpipe,"Child 2 is sending a message!");write(fd1,outpipe,50);sleep(5);lockf(fd1,0,0);exit(0);elsewait(0);/*同步*/read(fd0,inpipe,50);/*从管道中读长为50字节的串*/printf("%sn",inpipe);wait(0);read(fd0,inpipe,50);printf("%sn",inpipe);exit(0);运行结果延迟5秒后显示:child 1 is sending a message!再延迟5秒:child 2 is sending a message!专心-专注-专业