操作系统实验1:进程的状态及其转换C语言代码

    xiaoxiao2025-01-14  15

    一、实验代码

    #include<stdio.h> #include<stdlib.h> #include<string.h> int i=0; int j=0; //运行态进程 struct Run_type{ char name; int state; int needtime; int priority; }; //就绪态进程 struct Ready_type{ char name; int needtime; int priority; }; //阻塞态进程 struct Blocked_type{ char name; int needtime; int priority; }; struct Run_type Cpu; struct Ready_type Ready[10]; struct Blocked_type Blocked[10]; //创建一个新的进程 void Creat(){ int k=0; label1: printf(" input new process name(input a letter): \n"); scanf("%s",&(Ready[i].name));getchar(); for(k=0;k<i;k++) if(Ready[i].name==Ready[k].name||Ready[i].name==Ready[k].name+32||Ready[i].name==Ready[k].name-32) { printf("the process is already exist!"); goto label1; } printf("input needtime (input a int number):\n"); label3: scanf("%d",&(Ready[i].needtime));getchar(); if(Ready[i].needtime<1||Ready[i].needtime>100) { printf("please input the true needtime(1--100)\n"); goto label3; } printf(" input the priority(1--10): \n"); label2: scanf("%d",&(Ready[i].priority));getchar(); if(Ready[i].priority<1||Ready[i].priority>10) { printf("please 1--10!\n"); goto label2; } i++; } void Dispath() { int t;char v;int k;int j; if(Cpu.state==0) if(Ready[i-1].needtime==0) { printf("* there is no process ready!\n"); exit(0); } else { for(k=0;k<i-1;k++) for(j=0;j<i-k-1;j++) if(Ready[j].priority>Ready[j+1].priority) { t=Ready[j].priority; Ready[j].priority=Ready[j+1].priority;Ready[j+1].priority=t; t=Ready[j].needtime; Ready[j].needtime=Ready[j+1].needtime;Ready[j+1].needtime=t; v=Ready[j].name; Ready[j].name=Ready[j+1].name;Ready[j+1].name=v; } --i; Cpu.name=Ready[i].name;Cpu.needtime=Ready[i].needtime;Cpu.priority=Ready[i].priority;Cpu.state=1; printf("*\ is send to cpu! \n",Cpu.name); Cpu.needtime--; if(Cpu.needtime==0) { printf("*\ is finished \n",Cpu.name); Cpu.state=0; } } else { Ready[i].name=Cpu.name; Ready[i].needtime=Cpu.needtime; Ready[i].priority=Cpu.priority; for(k=0;k<i;k++) for(j=0;j<i-k;j++) if(Ready[j].priority>Ready[j+1].priority) { t=Ready[j].priority; Ready[j].priority=Ready[j+1].priority;Ready[j+1].priority=t; t=Ready[j].needtime; Ready[j].needtime=Ready[j+1].needtime;Ready[j+1].needtime=t; v=Ready[j].name; Ready[j].name=Ready[j+1].name;Ready[j+1].name=v; } Cpu.name=Ready[i].name;Cpu.needtime=Ready[i].needtime;Cpu.priority=Ready[i].priority;Cpu.state=1; printf("*\ is send to cpu! \n",Cpu.name); Cpu.needtime--; if(Cpu.needtime==0) { printf("*\ is finished \n",Cpu.name); Cpu.state=0; } } } void Timeout() { if(Cpu.state==0) { printf("* there is no process in cpu(please select Dispath!\n"); exit(0); } else { Ready[i].name=Cpu.name; Ready[i].needtime=Cpu.needtime; Ready[i].priority=Cpu.priority; printf("%c is timeout \n",Cpu.name); Cpu.name=0; Cpu.needtime=0; Cpu.priority=0; Cpu.state=0; i++; } } void Eventwait(){ if(Cpu.state!=0) { Blocked[j].name=Cpu.name; Blocked[j].needtime=Cpu.needtime; Blocked[j].priority=Cpu.priority; printf("* %c is Blocked !\n",Cpu.name); j++; Cpu.name=Blocked[j].name; Cpu.needtime=Blocked[j].needtime; Cpu.priority=Blocked[j].priority; Cpu.state=0; } else printf("* There is no process in cpu!"); } void Eventoccurs(char a){ int k=0;int m=0;int n=0;int p=0; if(Blocked[0].needtime==0) { printf("* there is no process blocked!\n"); exit(0); } else { for(k;k<j;k++) if(Blocked[k].name==a) { Ready[i].name=Blocked[k].name; Ready[i].needtime=Blocked[k].needtime; Ready[i].priority=Blocked[k].priority; p=1; m=j-k; for(n;n<m;n++) { Blocked[k].name=Blocked[k+1].name; Blocked[k].needtime=Blocked[k+1].needtime; Blocked[k].priority=Blocked[k+1].priority; k++; } j--;i++; } if(p==1) printf("* %c is ready!\n",a); else printf("* %c is not found! \n",a); } } void main(){ Cpu.state=0; int Cputime=0; int x=6; while(x!=0) { printf("\n1:input new process 2:Dispath\n"); printf("3:Timeout 4:Eventwait\n"); printf("5:Eventoccurs(select whose eventoccur ) 0:exit\n"); label4: scanf("%d",&x);getchar(); printf("\n==============================================================================\n"); if(x==0||x==1||x==2||x==3||x==4||x==5) { switch(x) { case 1:Creat(); break; case 2:Dispath(); break; case 3:Timeout(); break; case 4:Eventwait(); break; case 5:{char a;scanf("%c",&a);getchar(); Eventoccurs(a); break;} default:printf("please select from 0 to 5\n"); } printf("----------------------------- Cputime:= ----------------------------------\n",Cputime); printf("| ProceNsname NeedTime Priority |\n"); //显示Cpu中的程序 if(Cpu.state!=0) { printf("| Cpu:c",Cpu.name);printf(" d",Cpu.needtime);printf("d |\n",Cpu.priority); } else printf("| * Cpu is free |\n"); int y=0; //显示Ready队列中的数据 if(i==0) { printf("| *There is no process ready. |\n"); } else { //显示Ready中的程序 for(y;y<i;y++) { printf("| Ready%d:",y); printf("c",Ready[y].name);printf(" d",Ready[y].needtime);printf("d |\n",Ready[y].priority); } } int z=0; //显示Blocked队列中的程序 if(j==0) { printf("| *There is no process blocked. |\n"); } else { for(z;z<j;z++) { printf("| Blocked%d:",z); printf("c",Blocked[z].name);printf(" d",Blocked[z].needtime);printf("d |\n",Blocked[z].priority); } } Cputime++; if(Cpu.state!=0) Cpu.needtime=Cpu.needtime-1; printf("-----------------------------------------------------------------------------\n"); } else { printf("please input 0--5\n"); goto label4; } } }

    二、程序说明

    在codeblocks中编译程序后若无错就执行程序,初始如下: 其中,

    1表示创建新进程,随后输入进程名,所需时间,优先级2表示系统调度进程,首先调度最高优先级3表示时间片已到,运行中的进程将编程就绪态4表示进程所需的事件尚未发生,进程将被阻塞5表示进程等待的事件已发生,进程从阻塞态变为就绪态0退出程序

    三、运行检验

    创建a、b两个进程

    进程调度(由于b优先级高于a,所以b先进入运行态)

    b时间片到,b变为就绪态

    再次调度(由于b优先级高于a,故还是b变成运行态)

    创建进程c(由于b的运行时间短,来不及说明,所以创建进程c)

    调度进程c

    正在运行的c阻塞

    希望过程能对大家有所帮助。

    最新回复(0)