操作系统实验一练习进程状态转换
操作系统实验报告进程状态转换

实验进程状态转换及其PCB的变化、程序流程图:、使用的数据结构及说明:在本实验中,主要用到的数据结构是PCB的结构,其中PCB的数据结构如下: struct PCB {int P_ld;char P_Name[10];char P_State[10];int P_Ru ntime;int P_Requiry; struct PCB * n ext ;l -//PCB 的ID 号//PCB的名称//PCB状态//PCB的所需要的运行时间//PCB所需要的资源要求//PCB块的下一个指针其中,P_Id,和P_Name 用来标示一个进程,而P_State用来标示进程的五种状态:Create_state,Ready_state,Block_state,Run_state,Exit_state 。
P_Runtime 标示要完成一个进程所需 要的时间。
P_Requiry 标示一个进程的执行所需要的其他条件, 当其他的条件满足, 则 P_Requiry 置1,否则置0。
Struct PCB * next 用来指向同一队列中的下一个PCB 块。
三、程序源代码 :#include"stdlib.h" #include"stdio.h" #include"string.h"/********** globle structure and viable ******/ struct PCB//PCB 的 ID 号//PCB 的名称//PCB 状态//PCB 的所需要的运行时间 //PCB 所需要的资源要求 //PCB 块的下一个指针{ p=*head; q=p->next;p=q;q=q->next;}// 将节点插入队列{int P_Id;char P_Name[10]; char P_State[10]; int P_Runtime; int P_Requiry; struct PCB * next ; } ;struct PCB * Create_state; struct PCB * Run_state; struct PCB * Ready_state; struct PCB * Block_state; struct PCB * Exit_state; int signal4=0; int signal5=0;// 创建状态 // 运行状态 // 就绪状态 // 阻塞状态// 退出状态 // 标示进程 // 标示进程的完成状态 的完成状态void InsertQueue(struct PCB **head,struct PCB *node){struct PCB * p,*q; node->next=NULL; if(*head==NULL){*head=node;}Else/* insert node function */// 如果队列为空// 队列不空while(q!=NULL) {// 找到最后的元素位置p->next=node;void DeleteQueue(struct PCB **head,struct PCB *node) {struct PCB *p,*q; q=*head;if(node->P_Requiry)printf("this process resource is ready \n");elseprintf("this process resource is not ready ! \n");}void DispatchToBlock(struct PCB *node) // /* dispatch to block function*/ { // 调度到阻塞状态的函数//struct PCB *p=(struct PCB *)malloc(sizeof(struct PCB));if(!node->P_Requiry) // 如果所需要的资源没有满足则,调度到阻塞状态 { strcpy(node->P_State,"block");InsertQueue(&Block_state,node); // 插入到阻塞队列 Display_Process(node);if(*head==NULL||node==NULL) return ; if(*head==node) { *head=(*head)->next; return; } Else { while(q->next!=p&&q->next!=NULL)q=q->next; q=p->next; p->next=NULL;}} void Display_Process(struct PCB * node) {printf("\n\nthis process Id is printf("this process name is printf("this process state is printf("this process Runtime is // 如果队列为空,返回 // 如果要删除的元素是队首元素// 如果不是队列的首元素// 打印进程状态的元素函数 : %d \n",node->P_Id); : %s \n",node->P_Name); : on %s \n ",node->P_State); : %d \n",node->P_Runtime);// 撤销进程,从队列中删除元素void DispatchToReady(struct PCB *node) // dispatch to ready state{ // 调度到就绪状态的函数 if(node->P_Requiry) // 如果所需的资源满足,则调度{strcpy(node->P_State,"Ready");InsertQueue(&Ready_state,node); Display_Process(node);}}void DispatchBlockToReady() //dispatch the process to readyqueueInsertQueue(&Ready_state,p);printf("process4 will be in the state of ready!\n"); Display_Process(p);} if(signal5&&p->P_Id==5){DeleteQueue(&Block_state,p); strcpy(p->P_State,"ready");InsertQueue(&Ready_state,p);printf("process5 will be in the state of ready!\n"); Display_Process(p);} } }void Create_Process(){int i;struct PCB *p; char name[10];{ struct PCB*p,*q;q=Block_state; while(q!=NULL){p=q; q=q->next;if(signal4&&p->P_Id==4){DeleteQueue(&Block_state,p); strcpy(p->P_State,"ready");// 从阻塞状态调度到就绪状态函数 // 如果阻塞状态队列不空//如果所需要的资源满足// 创建进程函数strcpy(name,"process"); // 动态创建 2 个处于阻塞状态的进程p=(struct PCB *)malloc(sizeof(struct PCB)); p->P_Id=i; name[7]=i+'0';name[8]='\0'; strcpy(p->P_Name,name); strcpy(p->P_State,"create");p->P_Runtime=1; // 所需要的时间片为 1 p->P_Requiry=0; Display_Process(p); sleep(4);printf(" \n process%d will be in the state of Block, waiting the resource ready \n\n",i); DispatchToBlock(p); // 同时调度到阻塞队列// 创建 4 个就绪状态的队列p=(struct PCB *)malloc(sizeof(struct PCB)); p->P_Id=i; name[7]=i+'0'; name[8]='\0';strcpy(p->P_Name,name); strcpy(p->P_State,"create"); p->P_Requiry=1; p->P_Runtime=2; elsep->P_Runtime=1; Display_Process(p); sleep(4);printf(" \n process%d will be in the state of Ready, waiting to run \n\n",i); DispatchToReady(p);void display(struct PCB **head) {struct PCB *p,*q; p=*head;for(i=1;i<3;i++){for(i=3;i<7;i++){if(i==6)// 在这里个进程 6while(p!=NULL) { sleep(2);//printf("\n\n///////////////////////////////////\n"); printf("\n\nthis process Id is printf("this process name is printf("this process state is printf("this process Runtime is if(p->P_Requiry)printf("this process resource is ready \n");elseprintf("this process resource is not ready ! \n"); p=p->next;} }{printf("this process is not finished,will be dispatch to the ready queue!!\n"); DeleteQueue(&Ready_state,p);strcpy(p->P_State,"ready"); InsertQueue(&Ready_state,p);Display_Process(p);}Else // 执行完成,则跳出,并发送相应的信息 {printf("\n\nProcess%d is finished and will be in the state of exit!\n\n",p->P_Id);if(p->P_Id==4) signal4=1; if(p->P_Id==5) signal5=1; } if(signal4||signal5)DispatchBlockToReady(); // 如果资源满足,则将进程调度到就绪队列 q=q->next;p=q;}if(q==NULL) printf("\nthere is no process ready!\n STOP Machine!!!\n");}int main(int argc,char * argv[]) // 主函数: %d \n",p->P_Id); : %s \n",p->P_Name); : on %s \n ",p->P_State); : %d \n",p->P_Runtime);void Process_Run(){struct PCB *p,*q; p=Ready_state; q=p;while(p!=NULL){if(p->P_Runtime<=0) break; strcpy(p->P_State,"running"); Display_Process(p);p->P_Runtime=p->P_Runtime-1; sleep(4);if(p->P_Runtime>0)// 进程运行函数// 就绪队列不空则继续执行 // 如果时间片执行完了,则跳出循环// 没有完成,则进入就绪队列{int i;char c='c'; // 界面printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n"); printf(" Ding Hai bo\n");printf(" ..... P ress s to start the process ...... \n");scanf("%c",&c);while(1){if(c=='s')break; scanf("%c",&c);}Create_Process(); // 调用创建进程函数printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); printf("\n>>>>>>> Display the Ready queue >>>>>>>>>>>>>>>\n"); sleep(5);display(&Ready_state); //////////////// 显示就绪队列里的进程printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");printf("\n>>>>>>>> Display the Block queue >>>>>>>>>>>>\n");sleep(5); // 显示阻塞队列函数display(&Block_state); /////////////////////printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");printf("\n>>>>>>>> Now the process start to run >>>>>>>>>>>\n");sleep(5);Process_Run(); // 调用进程运行函数}四、运行结果及说明:运行结果的截图:下面的运行结果是程序执行的每一步进程调度的显示,从进程的创建到进程的执行,结束,每一步进程调度都有显示。
实验一、进程调度实验报告

实验一、进程调度实验报告一、实验目的进程调度是操作系统中的核心功能之一,其目的是合理地分配 CPU 资源给各个进程,以提高系统的整体性能和资源利用率。
通过本次实验,我们旨在深入理解进程调度的原理和算法,掌握进程状态的转换,观察不同调度策略对系统性能的影响,并通过实际编程实现来提高我们的编程能力和对操作系统概念的理解。
二、实验环境本次实验使用的操作系统为 Windows 10,编程语言为 C++,开发工具为 Visual Studio 2019。
三、实验原理1、进程状态进程在其生命周期中会经历不同的状态,包括就绪态、运行态和阻塞态。
就绪态表示进程已经准备好执行,只等待 CPU 分配;运行态表示进程正在 CPU 上执行;阻塞态表示进程由于等待某个事件(如 I/O操作完成)而暂时无法执行。
2、调度算法常见的进程调度算法有先来先服务(FCFS)、短作业优先(SJF)、时间片轮转(RR)等。
先来先服务算法按照进程到达的先后顺序进行调度。
短作业优先算法优先调度执行时间短的进程。
时间片轮转算法将 CPU 时间划分成固定大小的时间片,每个进程轮流获得一个时间片执行。
四、实验内容1、设计并实现一个简单的进程调度模拟器定义进程结构体,包含进程 ID、到达时间、执行时间、剩余时间等信息。
实现进程的创建、插入、删除等操作。
实现不同的调度算法。
2、对不同调度算法进行性能测试生成一组具有不同到达时间和执行时间的进程。
分别采用先来先服务、短作业优先和时间片轮转算法进行调度。
记录每个算法下的平均周转时间、平均等待时间等性能指标。
五、实验步骤1、进程结构体的定义```c++struct Process {int pid;int arrivalTime;int executionTime;int remainingTime;int finishTime;int waitingTime;int turnaroundTime;};```2、进程创建函数```c++void createProcess(Process processes, int& numProcesses, int pid, int arrivalTime, int executionTime) {processesnumProcessespid = pid;processesnumProcessesarrivalTime = arrivalTime;processesnumProcessesexecutionTime = executionTime;processesnumProcessesremainingTime = executionTime;numProcesses++;}```3、先来先服务调度算法实现```c++void fcfsScheduling(Process processes, int numProcesses) {int currentTime = 0;for (int i = 0; i < numProcesses; i++){if (currentTime < processesiarrivalTime) {currentTime = processesiarrivalTime;}processesistartTime = currentTime;currentTime += processesiexecutionTime;processesifinishTime = currentTime;processesiwaitingTime = processesistartTime processesiarrivalTime;processesiturnaroundTime = processesifinishTime processesiarrivalTime;}}```4、短作业优先调度算法实现```c++void sjfScheduling(Process processes, int numProcesses) {int currentTime = 0;int minExecutionTime, selectedProcess;bool found;while (true) {found = false;minExecutionTime = INT_MAX;selectedProcess =-1;for (int i = 0; i < numProcesses; i++){if (processesiarrivalTime <= currentTime &&processesiremainingTime < minExecutionTime &&processesiremainingTime > 0) {found = true;minExecutionTime = processesiremainingTime;selectedProcess = i;}}if (!found) {break;}processesselectedProcessstartTime = currentTime;currentTime += processesselectedProcessremainingTime;processesselectedProcessfinishTime = currentTime;processesselectedProcesswaitingTime =processesselectedProcessstartTime processesselectedProcessarrivalTime;processesselectedProcessturnaroundTime =processesselectedProcessfinishTime processesselectedProcessarrivalTime;processesselectedProcessremainingTime = 0;}}```5、时间片轮转调度算法实现```c++void rrScheduling(Process processes, int numProcesses, int timeSlice) {int currentTime = 0;Queue<int> readyQueue;for (int i = 0; i < numProcesses; i++){readyQueueenqueue(i);}while (!readyQueueisEmpty()){int currentProcess = readyQueuedequeue();if (processescurrentProcessarrivalTime > currentTime) {currentTime = processescurrentProcessarrivalTime;}if (processescurrentProcessremainingTime <= timeSlice) {currentTime += processescurrentProcessremainingTime;processescurrentProcessfinishTime = currentTime;processescurrentProcesswaitingTime =processescurrentProcessstartTime processescurrentProcessarrivalTime;processescurrentProcessturnaroundTime =processescurrentProcessfinishTime processescurrentProcessarrivalTime;processescurrentProcessremainingTime = 0;} else {currentTime += timeSlice;processescurrentProcessremainingTime = timeSlice;readyQueueenqueue(currentProcess);}}}```6、性能指标计算函数```c++void calculatePerformanceMetrics(Process processes, int numProcesses, double& averageWaitingTime, double& averageTurnaroundTime) {double totalWaitingTime = 0, totalTurnaroundTime = 0;for (int i = 0; i < numProcesses; i++){totalWaitingTime += processesiwaitingTime;totalTurnaroundTime += processesiturnaroundTime;}averageWaitingTime = totalWaitingTime / numProcesses; averageTurnaroundTime = totalTurnaroundTime / numProcesses;}```7、主函数```c++int main(){Process processes100;int numProcesses = 0;//创建进程createProcess(processes, numProcesses, 1, 0, 5);createProcess(processes, numProcesses, 2, 1, 3);createProcess(processes, numProcesses, 3, 2, 4);createProcess(processes, numProcesses, 4, 3, 2);//先来先服务调度fcfsScheduling(processes, numProcesses);double fcfsAverageWaitingTime, fcfsAverageTurnaroundTime;calculatePerformanceMetrics(processes, numProcesses, fcfsAverageWaitingTime, fcfsAverageTurnaroundTime);cout <<"先来先服务调度的平均等待时间:"<<fcfsAverageWaitingTime << endl;cout <<"先来先服务调度的平均周转时间:"<<fcfsAverageTurnaroundTime << endl;//短作业优先调度sjfScheduling(processes, numProcesses);double sjfAverageWaitingTime, sjfAverageTurnaroundTime;calculatePerformanceMetrics(processes, numProcesses, sjfAverageWaitingTime, sjfAverageTurnaroundTime);cout <<"短作业优先调度的平均等待时间:"<<sjfAverageWaitingTime << endl;cout <<"短作业优先调度的平均周转时间:"<<sjfAverageTurnaroundTime << endl;//时间片轮转调度(时间片为 2)rrScheduling(processes, numProcesses, 2);double rrAverageWaitingTime, rrAverageTurnaroundTime;calculatePerformanceMetrics(processes, numProcesses, rrAverageWaitingTime, rrAverageTurnaroundTime);cout <<"时间片轮转调度(时间片为 2)的平均等待时间:"<< rrAverageWaitingTime << endl;cout <<"时间片轮转调度(时间片为 2)的平均周转时间:"<< rrAverageTurnaroundTime << endl;return 0;}```六、实验结果与分析1、先来先服务调度平均等待时间:40平均周转时间:85分析:先来先服务调度算法简单直观,但对于短作业可能会造成较长的等待时间,导致平均等待时间和平均周转时间较长。
计算机操作系统 练习1

1.某系统的进程状态转换图如图,请说明1)引起各种状态转换的典型事件有哪些?2)当我们观察系统中某些进程时,能够看到某一进程产生的一次状态转换能引起另一进程作一次状态转换。
在什么情况下,当一个进程发生转换3时能立即引起另一进程发生转换1。
3)试说明是否会发生下述因果转换:2→1 3→2 4→12.设有n各进程共享一个程序段,对如下两种情况:1)如果每次只允许一个进程进入该程序段2)如果每次最多允许m(m<n)个进程同时进入该程序段试问:所采用的信号量初值是否相同?信号量值的变化范围如何?3.设有一个计算进程PUT和两个取数进程GUT1和GUT2,进程PUT不断地把计算得到的结果(整数)送入变量buf,如果送入buf的数是奇数,应由GUT1取出并打印。
如果送入buf的数是偶数,应由GUT2取出并打印。
要求:即不漏打,也不重复打印。
请用P、V操作,并用类PASCAL或类C或流程图,描述对应与各进程的程序。
4.有一个仓库可以存放A、B两种物品,每次只能存入一件物品(A或B)。
存储空间充分大,只是要求:-N<A的件数-B的件数<M,其中N和M 是正整数。
试用P、V操作描述物品A和物品B的入库过程,且要求定义出所需信号量及其初始值。
5.一台计算机有8台磁带机,它们由N个进程竞争使用,每个进程可能需要3台磁带机,请问N为多少时,系统没有死锁危险,并说明理由。
6.假定某计算机系统有R1和R2两类可使用资源(其中R1有两个单位,R2有一个单位。
他们被进程P1和P2共享,且已知两个进程均以下列顺序是用两类资源:→申请R1→申请R2→申请R1→释放R1→释放R2→释放R1→试求出系统运行过程中可能到达的死锁点,并画出死锁点的资源分配图。
7.某系统由R1、R2和R3三种资源,在T0时刻P1,P2,P3,P4四个进程对资2,1,2),问题:1)将系统中各种资源总数和此刻个进程对资源的需求数目用向量或矩阵表示出来。
操作系统实验一模拟进程状态转换

操作系统实验一模拟进程状态转换四、运行结果:图1 创建2个进程,因为这时cpu空闲所以内核调度,b优先级高先执行图2 超时,因为这时cpu空闲所以内核调度,b优先级还是比a高所以先执行图3 2个进程均被阻塞,其中一旦进程被阻塞就会引发调度图4 唤醒1个进程,从阻塞队列取队首放到就绪队列队尾,由于这时cpu空闲所以内核调度五、源代码:#include<cstdio>#include<algorithm>using namespace std;int Ready_len=0;int Blocked_len=0;int CPU_state=0;struct PCB{char name;int priority;int needtime;bool operator < (const PCB &b) const{return priority>b.priority;}};PCB Ready[100];PCB Blocked[100];PCB Cpu;bool dispatch();bool creat(int NUM){//创建一个新的进程while(NUM--){printf("输入进程名(一个字符)、所需时间(一个整数)、优先级(一个整数): \n");scanf("%s%d%d",&(Ready[Ready_len].name),&(Ready[Ready_len ].needtime),&(Ready[Ready_len].priority));getchar();Ready_len++;}if(CPU_state==0)//如果CPU空闲,则调度dispatch();}bool dispatch(){if(CPU_state==0){if(Ready_len!=0){sort(Ready,Ready+Ready_len);=Ready[0].name;Cpu.needtime=Ready[0].needtime;Cpu .priority=Ready[0].priority;if(Ready_len!=1)//就绪队列剔除队首元素for(int indx=1;indx<Ready_len;indx++){Ready[indx-1].name=Ready[indx].name;Ready[indx-1].needtime= Ready[indx].needtime;Ready[indx-1].priority=Ready[indx].priority;}Ready_len--;CPU_state=1;printf("***%c进程送往CPU执行\n",);Cpu.needtime--;Cpu.priority--;}else{printf("***就绪队列为空,无法调度\n");return false;}}else{printf("***CPU忙,无法调度\n");}}bool time_out(){if(CPU_state==1){if(Cpu.needtime==0)printf("***%c时间片用完,并且执行完毕,被释放\n",);else{Ready[Ready_len].name=;Ready[Ready_len].needtime= Cpu.needtime;Ready[Ready_len].priority=Cpu.priority;Ready_len++;printf("***%c时间片用完\n",);}CPU_state=0;=0;Cpu.needtime=0;Cpu.priority=0;if(Ready_len!=0)//时间片用完,如果就绪队列不为空,则调度dispatch();}else{printf("***没有进程在CPU中,无法超时\n");}}bool event_wait(){if(CPU_state==1){Blocked[Blocked_len].name=;Blocked[Blocked_len].n eedtime=Cpu.needtime;Blocked[Blocked_len].priority=Cpu.priority;Blocked_len++;printf("***%c被阻塞\n",);CPU_state=0;if(Ready_len!=0)//进程被阻塞,如果就绪队列不为空,则调度dispatch();}elseprintf("***没有进程在CPU中,无法阻塞\n");}bool event_occur(){if(Blocked_len!=0){//sort(Blocked,Blocked+Blocked_len);Ready[Ready_len].name=Blocked[0].name;Ready[Ready_len].nee dtime=Blocked[0].needtime;Ready[Ready_len].priority=Blocked[0]. priority;Ready_len++;if(Blocked_len!=1)//阻塞队列剔除队首元素for(int indx=1;indx<Blocked_len;indx++){Blocked[indx-1].name=Blocked[indx].name;Blocked[indx-1].needti me=Blocked[indx].needtime;Blocked[indx-1].priority=Blocked[indx]. priority;}Blocked_len--;//printf("%d %d",Blocked_len,Ready_len);printf("***%c被唤醒\n",Ready[Ready_len-1].name);if(CPU_state==0)//如果CPU空闲,则调度dispatch();//printf("%d %d",Blocked_len,Ready_len);}elseprintf("***阻塞队列为空,无法唤醒\n");}int main(){int Cputime=1;while(1){printf("\n1:New\t\t\t2:Dispatch\n");printf("3:Timeout\t\t4:Event wait\n");printf("5:Event occur\t\t0:exit\n");printf("输入1--5实现相应的功能:\n");int select;scanf("%d",&select);getchar();switch(select){case 1:int num;printf("输入要创建的进程数:\n");scanf("%d",&num);getchar();creat(num);break;case 2:dispatch();break;case 3:time_out();break;case 4:event_wait();break;case 5:event_occur();break;case 0:exit(0);break;}printf("****************************Cputime:%3d********************* *******\n",Cputime);printf("状态\t\t进程名\t\t需要时间\t\t优先级\n");if(CPU_state){//显示CPU中的进程printf("Running:\t%c\t\t",);printf("%d\t\t\t",Cpu.needtime);printf("%d\n",Cpu.priority);}if(Ready_len){//显示Ready队列中的进程for(int a=0;a<Ready_len;a++){printf("Ready%d:\t\t",a);printf("%c\t\t",Ready[a].name);printf("%d\t\t\t",Ready[a].needtime);printf("%d\n",Ready[a].priority);}}if(Blocked_len){//显示Blocked队列中的程序for(int b=0;b<Blocked_len;b++){printf("Blocked%d:\t",b);printf("%c\t\t",Blocked[b].name);printf("%d\t\t\t",Blocked[b].needtime);printf("%d\n",Blocked[b].priority);}}printf("**************************************************************** ***\n");Cputime++;} }。
操作系统进程的三种状态的转换实验报告

3.设计出可视性较好的界面,应能反映出进程状态的变化引起的对应内容。
4.代码书写要规范,要适当地加入注释。
5.编译并调试自己编写的程序,是程序编译成功。
6.检查运行的结果是否正确。
四、实验结果(含算法说明、程序、数据记录及分析等,可附页)
五、实验思考题
利用C语言编写了一个模拟Windows进程三种状态之间的转换的程序,虽然肥城精简,但是我从中学到的很多知识点,学会了编写程序时的一些技巧,为自己以后更加深造打下了坚实的基础和使自己更好的理解Windows如何更好的管理进程。
六、实验总结(含实验心得体会,收获与不足等)
通过这次实验让我受益匪浅,不仅明白了自己以前不知道如何定义和应用结构体,还提高了自己的编程能力,调试程序的能力,使自己的编程能力有了大幅度的提高。使自己更好的理解进程的概念和进程的三种状态之间转换的过程和他们之间的关系。为自己以后复习打下了夯实的基础。
1)根据课本第三章的内容设计并实现一个模拟进程状态转换。
2)独立编写和调试程序。进程的数目,进程的状态模型自行选择。
3)合理设计进程相对应的数据结构,内容要包括进程的基本信息。
4)是进程的三种状态之间的转化清晰的显示。
三、实验过程及步骤(包含使用软件或实验设备等情况)
1.打开DEV-C++,新建一个源代码文件
实验报告
实验名称
进程三种状态的转换
专业
计算机
课程名称
操作系统
指导老师
张海燕
班级
二表一班
姓名
刘广法
学号
11100140109
评分
实验地点
1cபைடு நூலகம்6217
操作系统实验一练习进程状态转换

实验一模拟进程状态转换及其PCB的变化一、实验目的:自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容、组织的变化,理解进程与其PCB间的一一对应关系。
二、实验内容及要求:(1)、设计并实现一个模拟进程状态转换及其相应PCB内容、组织结构变化的程序。
(2)、独立编写、调试程序。
进程的数目、进程的状态模型(三状态、五状态、七状态或其它)以及PCB的组织形式可自行选择。
(3)、合理设计与进程PCB相对应的数据结构。
PCB的内容要涵盖进程的基本信息、控制信息、资源需求及现场信息。
(4)、设计出可视性较好的界面,应能反映出进程状态的变化引起的对应PCB内容、组织结构的变化。
(5)、代码书写要规范,要适当地加入注释。
(6)、鼓励在实验中加入新的观点或想法,并加以实现。
(7)、认真进行预习,完成预习报告。
(8)、实验完成后,要认真总结,完成实验报告。
三、实现:数据结构struct PCB{char name。
int priority。
int needtime。
bool operator < (const PCB &b) const{ return priority>b.priority。
}}。
五状态进程模型最高优先数优先调度算法流程图四、运行结果:图1创建2个进程,因为这时cpu空闲所以内核调度,b优先级高先执行图2超时,因为这时cpu空闲所以内核调度,b优先级还是比a高所以先执行图32个进程均被阻塞,其中一旦进程被阻塞就会引发调度图4唤醒1个进程,从阻塞队列取队首放到就绪队列队尾,由于这时cpu空闲所以内核调度五、源代码:#include<cstdio>#include<algorithm>using namespace std。
int Ready_len=0。
int Blocked_len=0。
int CPU_state=0。
国家开放大学《操作系统》形考任务(应用题)参考答案

国家开放大学《操作系统》形考任务(应用题)参考答案1. 用如图所示的进程状态转换图能够说明有关处理机管理的大量内容。
图进程状态转换图试回答:①什么事件引起每次显著的状态变迁?②下述状态变迁因果关系能否发生?为什么?参考答案:①就绪→运行:CPU 空闲,就绪态进程被调度程序选中。
运行→就绪:正在运行的进程用完了本次分配给它的CPU 时间片。
运行 → 阻塞:运行态进程因某种条件未满足而放弃对CPU 的占用,如等待读文件。
阻塞→就绪:阻塞态进程所等待的事件发生了,例如读数据的操作完成。
②下述状态变迁:(A)2 → 1:可以。
运行进程用完了本次分配给它的时间片,让出CPU,从 就绪队列中选一个进程投入运行。
(B)3 →2:不可以。
任何时候一个进程只能处于一种状态,它既然由运行 态变为阻塞态,就不能再由运行态变为就绪态。
(C)4 → 1:可以。
某一阻塞态进程等待的事件出现了,而且此时就绪队列 运行状态1/24 阻塞状态就绪 状态 3为空,该进程进入就绪队列后马上又被调度运行。
2.系统中只有一台打印机,有三个用户的程序在执行过程中都要使用打印机输出计算结果。
设每个用户程序对应一个进程。
问:这三个进程间有什么样的制约关系?试用P、v操作写出这些进程使用打印机的算法。
参考答案:因为打印机是一种临界资源,所以这三个进程只能互斥使用这台打印机,即一个用户的计算结果打印完之后,另一个用户再打印。
设三个进程分别为A、B和C。
设一个互斥信号量mutex,其初值为1。
进程A 进程B 进程CP(mutex) 使用打印机V(mutex)P(mutex)使用打印机V(mutex)P(mutex)使用打印机V(mutex)3. 判断下列同步问题的算法是否正确?若有错,请指出错误原因并予以改正。
①设A,B两个进程共用一个缓冲区Q,A向Q写入信息,B从Q读出信息,算法框图如图左侧所示。
②设A,B为两个并发进程,它们共享一个临界资源。
操作系统--进程状态切换以及cpu调度(转)

操作系统--进程状态切换以及cpu调度(转)进程的状态转换 进程在运⾏中不断地改变其运⾏状态。
通常,⼀个运⾏进程必须具有以下三种基本状态。
进程状态执⾏态run:进程正在使⽤CPU等待态wait:进程正在等待I/O完成,不在使⽤也不能使⽤CPU就绪态ready:进程不在使⽤CPU,但已经纯备好⽤使⽤CPU 在特定的情况下,这三种状态可以相互转换。
状态转换 就绪->执⾏,当前运⾏进程阻塞,调度程序选⼀个优先权最⾼的进程占有处理机; 执⾏->就绪,当前运⾏进程时间⽚⽤完; 执⾏->等待,当前运⾏进程等待键盘输⼊,进⼊了睡眠状态。
等待->就绪,I/O操作完成,被中断处理程序唤醒。
刚从其他状态进⼊就绪态的进程需要置⼊调度队列,该队列不⼀定按进⼊队列的时间先后顺序排列。
从等待态中出来的进程通常不直接进⼊运⾏态,⽽要进⼊就绪态。
如果需要直接进⼊运⾏态,这属于抢先式调度,通过抢先式中断完成。
从执⾏态到就绪态的转换发⽣在抢先式终端处理中,例如I/O或分时下的时间⽚。
分时是在多个⽤户同时以交互⽅式使⽤计算机时采⽤的⼀种技术。
分时技术:每当时钟中断发⽣时且发现当前运⾏进程已连续在CPU上运⾏了⼀定的时间(称为时间⽚,⼀般为20ms~250ms)时,就强制地发⽣进程切换,使当前进程退出CPU,重新调度,选出另⼀个进程在CPU上运⾏。
此时,退出的进程不能变为等待状态,因为它不是因为等待I/O⽽退出的,也不能变为终⽌,因为它尚未结束,因此,它需要转换为就绪态,等待属于它的时间⽚的到来。
当正在建⽴⼀个新进程时,计算机上的当前运⾏进程是哪⼀个? 该新进程的⽗进程。
CPU调度算法 同时处于就绪态的进程经常有多个,因此需要⼀个CPU调度算法来君顶那⼀个就绪进程先运⾏。
衡量CPU调度算法的标准有:CPU利⽤率、⽤户程序响应时间、系统吞吐量、公平合理性、设备利⽤率等。
常见的调度算法有先来先服务FIFO、轮转调度法RR(时间⽚法)、优先级调度法、短作业优先SJF、最短剩余事件优先、最⾼相应⽐优先、多级反馈法、策略驱动法、最晚时间限调度、⼆级调度法。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验一模拟进程状态转换及其PCB的变化一、实验目的:自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容、组织的变化,理解进程与其PCB间的一一对应关系。
二、实验内容及要求:(1)、设计并实现一个模拟进程状态转换及其相应PCB内容、组织结构变化的程序。
(2)、独立编写、调试程序。
进程的数目、进程的状态模型(三状态、五状态、七状态或其它)以及PCB的组织形式可自行选择。
(3)、合理设计与进程PCB相对应的数据结构。
PCB的内容要涵盖进程的基本信息、控制信息、资源需求及现场信息。
(4)、设计出可视性较好的界面,应能反映出进程状态的变化引起的对应PCB内容、组织结构的变化。
(5)、代码书写要规范,要适当地加入注释。
(6)、鼓励在实验中加入新的观点或想法,并加以实现。
(7)、认真进行预习,完成预习报告。
(8)、实验完成后,要认真总结,完成实验报告。
三、实现:数据结构struct PCB{char name。
int priority。
int needtime。
bool operator < (const PCB &b) const{ return priority>b.priority。
}}。
五状态进程模型最高优先数优先调度算法流程图四、运行结果:图1创建2个进程,因为这时cpu空闲所以内核调度,b优先级高先执行图2超时,因为这时cpu空闲所以内核调度,b优先级还是比a高所以先执行图32个进程均被阻塞,其中一旦进程被阻塞就会引发调度图4唤醒1个进程,从阻塞队列取队首放到就绪队列队尾,由于这时cpu空闲所以内核调度五、源代码:#include<cstdio>#include<algorithm>using namespace std。
int Ready_len=0。
int Blocked_len=0。
int CPU_state=0。
struct PCB{char name。
int priority。
int needtime。
bool operator < (const PCB &b) const{ return priority>b.priority。
}}。
PCB Ready[100]。
PCB Blocked[100]。
PCB Cpu。
bool dispatch()。
bool creat(int NUM){//创建一个新的进程while(NUM--){printf("输入进程名(一个字符)、所需时间(一个整数)、优先级(一个整数): \n")。
scanf("%s%d%d",&(Ready[Ready_len].name),&(Ready[Ready_len ].needtime),&(Ready[Ready_len].priority))。
getchar()。
Ready_len++。
}if(CPU_state==0)//如果CPU空闲,则调度dispatch()。
}bool dispatch(){if(CPU_state==0){if(Ready_len!=0){sort(Ready,Ready+Ready_len)。
=Ready[0].name。
Cpu.needtime=Ready[0].needtime。
Cpu.priority=Ready[0].priority。
if(Ready_len!=1)//就绪队列剔除队首元素for(int indx=1。
indx<Ready_len。
indx++){Ready[indx-1].name=Ready[indx].name。
Ready[indx-1].needtime=Ready[indx].needtime。
Ready[indx-1].priority=Ready[indx].priority。
}Ready_len--。
CPU_state=1。
printf("***%c进程送往CPU执行\n",)。
Cpu.needtime--。
Cpu.priority--。
}else{printf("***就绪队列为空,无法调度\n")。
return false。
}}else{printf("***CPU忙,无法调度\n")。
}}bool time_out(){if(CPU_state==1){if(Cpu.needtime==0)printf("***%c时间片用完,并且执行完毕,被释放\n",)。
else{Ready[Ready_len].name=。
Ready[Ready_len].needtime=Cpu.needtime。
Ready[Ready_len].priority=Cpu.priority。
Ready_len++。
printf("***%c时间片用完\n",)。
}CPU_state=0。
=0。
Cpu.needtime=0。
Cpu.priority=0。
if(Ready_len!=0)//时间片用完,如果就绪队列不为空,则调度dispatch()。
}else{printf("***没有进程在CPU中,无法超时\n")。
}}bool event_wait(){if(CPU_state==1){Blocked[Blocked_len].name=。
Blocked[Blocked_len].needtime=Cpu.needtime。
Blocked[Blocked_len].priority=Cpu.priority。
Blocked_len++。
printf("***%c被阻塞\n",)。
CPU_state=0。
if(Ready_len!=0)//进程被阻塞,如果就绪队列不为空,则调度dispatch()。
}elseprintf("***没有进程在CPU中,无法阻塞\n")。
}bool event_occur(){if(Blocked_len!=0){//sort(Blocked,Blocked+Blocked_len)。
Ready[Ready_len].name=Blocked[0].name。
Ready[Ready_len].needtime=Blocked[0].needtime。
Ready[Ready_len].priority=Blocked[0].priority。
Ready_len++。
if(Blocked_len!=1)//阻塞队列剔除队首元素for(int indx=1。
indx<Blocked_len。
indx++){Blocked[indx-1].name=Blocked[indx].name。
Blocked[indx-1].needtime=Blocked[indx].needtime。
Blocked[indx-1].priority=Blocked[indx].priority。
}Blocked_len--。
//printf("%d %d",Blocked_len,Ready_len)。
printf("***%c被唤醒\n",Ready[Ready_len-1].name)。
if(CPU_state==0)//如果CPU空闲,则调度dispatch()。
//printf("%d %d",Blocked_len,Ready_len)。
}elseprintf("***阻塞队列为空,无法唤醒\n")。
}int main(){int Cputime=1。
while(1){printf("\n1:New\t\t\t2:Dispatch\n")。
printf("3:Timeout\t\t4:Event wait\n")。
printf("5:Event occur\t\t0:exit\n")。
printf("输入1--5实现相应的功能:\n")。
int select。
scanf("%d",&select)。
getchar()。
switch(select){case 1:int num。
printf("输入要创建的进程数:\n")。
scanf("%d",&num)。
getchar()。
creat(num)。
break。
case 2:dispatch()。
break。
case 3:time_out()。
break。
case 4:event_wait()。
break。
case 5:event_occur()。
break。
case 0:exit(0)。
break。
}printf("****************************Cputime:%3d********************* *******\n",Cputime)。
printf("状态\t\t进程名\t\t需要时间\t\t优先级\n")。
if(CPU_state){//显示CPU中的进程printf("Running:\t%c\t\t",)。
printf("%d\t\t\t",Cpu.needtime)。
printf("%d\n",Cpu.priority)。
}if(Ready_len){//显示Ready队列中的进程for(int a=0。
a<Ready_len。
a++){printf("Ready%d:\t\t",a)。
printf("%c\t\t",Ready[a].name)。
printf("%d\t\t\t",Ready[a].needtime)。
printf("%d\n",Ready[a].priority)。
}}if(Blocked_len){//显示Blocked队列中的程序for(int b=0。
b<Blocked_len。
b++){printf("Blocked%d:\t",b)。
printf("%c\t\t",Blocked[b].name)。
printf("%d\t\t\t",Blocked[b].needtime)。