操作系统实验(2).docx
操作系统实验操作系统实验 模拟进程管理 专业:信息管理与信息系统 班级:信管082 姓名:温静 实验一 进程管理 1目的和要求 通过实验理解进程的概念,进程的组成,进程的并发执行和操作系统进行进程管理的相关原语。 2实验内容 用C语言编程模拟进程管理,至少要有:创建新的进程;查看运行进程;换出某个进程;杀死运行进程以及进程之间通信等功能。 3主体程序 #include <conio.h> #include <stdio.h> #include <stdlib.h> struct PCB_type int pid; int priority; int cputime; int state; int shumu=0,pid_l; struct PCB_type neicun20; struct PCB_type hc10; int max=0;int number=0; void create; void run; void huanchu; void kill; /* 创建新进程 */ void create if(shumu>=20) printf("n内存已满,请先结束或换出进程n"); else /* 查看当前运行进程 */ void run /* 换出 */ void huanchu int k; printf("请输入要换出程序的程序名:"); scanf("%d",&k); for(int j=0;j<shumu;j+) if(neicunj.state=1) hcnumber.pid=neicunj.pid; hcnumber.state=neicunj.state; int max=0; for(int i=0;i<shumu;i+) neicunmax.state=3; printf("当前运行进程程序名:n%d",neicunmax.pid); printf("n该进程的优先级:n%d",neicunmax.priority); printf("n该进程的运行时间:n%d",neicunmax.cputime); printf("n该进程的状态:n%d",neicunmax.state); if(neicuni.state=1)&&(neicuni.priority>=neicunmax.priority) max=i; shumu+; printf("n请输入新进程的程序名n"); scanf("%d",&neicunshumu-1.pid); printf("n请输入新进程的优先级n"); scanf("%d",&neicunshumu-1.priority); printf("n请输入新进程的运行时间n"); scanf("%d",&neicunshumu-1.cputime); printf("n创建进程成功!n"); printf("n创建进程时令其状态为就绪n"); neicunshumu-1.state=2; hcnumber.priority=neicunj.priority; hcnumber.cputime=neicunj.cputime; number+; neicunj.pid=0; neicunj.state=0; neicunj.priority=0; neicunj.cputime=0; pid_1+; else printf("进程%d无法换出的pid:%dn",j.neicunj.pid); if(number!=0) for(int i=0;i<number;i+) printf("当前运行进程程序名:n%d",hci.pid); printf("n该进程的运行时间:n%d",hci.cputime); printf("n该进程的优先级:n%d",hci.priority); printf("n该进程的状态:n%d",hci.state); PCB_type temp=neicun0; for(k=0;k<=shumu;k+) neicunk.state=1; if(neicunk.priority>temp.priority) tmpe=neicunk; /* 杀死进程 */ void kill neicunmax.pid=0; neicunmax.priority=0; neicunmax.cputime=0; neicunmax.state=0; if(max=(shumu-1) else for(int j=max+1;j<shumu;j+) neicunj-1.pid=neicunj.pid; neicunj-1.priority=neicunj.priority; neicunj-1.cputime=neicunj.cputime; neicunj-1.state=neicunj.state; shumu-; shumu-; max=0; run; /* int k=0; */ int main int n,a; n=1; while(n=1) system("cls"); printf("n*"); printf("n* 进程演示系统 *"); printf("n*"); printf("n 1.创建新的进程 2.查看运行进程"); printf("n 3.换出某个进程 4.杀死运行进程"); printf("n 5.退出系统 "); printf("n*"); printf("请输入要杀死程序的进程名:"); scanf("%d",&k); if(neicunk.state=1) neicunk.state=2; neicunk.cputime=0; neicunk.pid=0; neicunk.priority=0; neicunk.state=0; shumu-; else printf("进程%d已被杀死!,k"); for(int j=k+1;j<shumu;j+) neicunj-1.pid=neicunj.pid; neicunj-1.priority=neicunj.priority; neicunj-1.cputime=neicunj.cputime; neicunj-1.state=neicunj.state; if(k=(shumu-1) shumu-; printf("n请选择(15):"); scanf("%d",&a); switch(a) case 1: create( ); printf("npress anykey to go on"); getch; break; case 2 : run; printf("npress anykey to go on"); getch; break; case 3 : huanchu; printf("npress anykey to go on"); getch; break; case 4 : kill; printf("npress anykey to go on"); getch; break; case 5 : exit(0); default: n=0; break; 5感想与心得体会 做了两周的实验,问了很多同学,可程序还是有很多问题。通过这次实验,我发现了自己在模拟进程调度和模拟程序换出这两块地方有很多东西都没有学好,自己还有很多的不足。以后还应多看看书,加深自己的知识储备。希望下次试验会比这次熟练多些。 最后,真的要感谢徐老师耐心、细心的讲解。