操作系统作业调度实验
发布时间:2024-11-28
发布时间:2024-11-28
实验一 进程
2011210549 崔晶
1.本实验实现的功能有:创建一个进程,撤销一个进程,挂起一个进程
2.本实验初始化的进程块状态如图所示:(自定义)
3.程序如下:
# include<stdio.h>
# include<string.h>
//进程块的数据结构
struct PCB_Struct{
char pb[6];
char status;//进程块所处的状态,p表示正在运行,r表示就绪,w表示等待 int next;
int prior;
}pcb[255];
int srun = 2;//运行队列头指针
int erun;//运行队列尾指针
int sready = 3;//就绪队列头指针
int eready;//就绪队列尾指针
int swait = 0;//等待队列头指针
int ewait;//等待队列尾指针
//初始化静态链表,运行队列2,7,9;等待队列0,1,4,6;就绪队列3,5,10,17; void init(){
strcpy(pcb[0].pb,"PCB0");
pcb[0].next=1;
pcb[0].status='p';
pcb[0].prior=-1;
strcpy(pcb[1].pb,"PCB1");
pcb[1].next=4;
pcb[1].status='w';
pcb[1].prior=0;
strcpy(pcb[2].pb,"PCB2");
pcb[2].next=7;
pcb[2].status='r';
pcb[2].prior=-1;
strcpy(pcb[3].pb,"PCB3");
pcb[3].next=5;
pcb[3].status='r';
pcb[3].prior=-1;
strcpy(pcb[4].pb,"PCB4");
pcb[4].next=6;
pcb[4].status='w';
pcb[4].prior=1;
strcpy(pcb[5].pb,"PCB5");
pcb[5].next=10;
pcb[5].status='r';
pcb[5].prior=3;
strcpy(pcb[6].pb,"PCB6");
pcb[6].next=-1;
pcb[6].status='w';
pcb[6].prior=4;
ewait = 6;
strcpy(pcb[7].pb,"PCB7");
pcb[7].next=9;
pcb[7].status='p';
pcb[7].prior=2;
strcpy(pcb[9].pb,"PCB9");
pcb[9].next=-1;
pcb[9].status='p';
pcb[9].prior=7;
erun = 9;
strcpy(pcb[10].pb,"PCB10");
pcb[10].next=17;
pcb[10].status='r';
pcb[10].prior=5;
strcpy(pcb[17].pb,"PCB17");
pcb[17].next=-1;
pcb[17].status='r';
pcb[17].prior=10;
eready = 17;
}
//插入就绪队列
void insert1(char* name){
for(int j = 0; j < 255; j++){
if(strcmp(pcb[j].pb,"-1") == 0){
strcpy(pcb[j].pb,name);
pcb[j].next = -1;
pcb[j].prior = eready;
pcb[eready].next = j;
eready = j;
return;
}
}
}
//插入等待队列
void insert2(char* name){
for(int j = 0; j < 255; j++){
if(strcmp(pcb[j].pb,"-1") == 0){
strcpy(pcb[j].pb,name);
pcb[j].next = -1;
pcb[j].prior = ewait;
pcb[ewait].next = j;
ewait = j;
return;
}
}
}
//创建一个进程并插入就绪队列
void creatpcb(char* name){
for(int i = 0; i < 255; i++){
if(strcmp(pcb[i].pb,name) == 0){
printf("要创建的进程已存在!");
return;
}
}
insert1(name);
}
//输出目前正在运行或等待或就绪的队列
void Print(char ch){
int p ;//当前进程块的序号
//输出等待队列
if(ch == 'w'){
p = swait;
while(pcb[p].next != -1){
printf("PCB%d ",p);
p = pcb[p].next;
}
printf("%s\n",pcb[ewait].pb);
}
//输出就绪队列
else if(ch == 'r'){
p = sready;
while(pcb[p].next != -1){
printf("PCB%d ",p);
p = pcb[p].next;
}
printf("%s\n",pcb[eready].pb);
}
//输出运行队列
else{
p = srun;
while(pcb[p].next != -1){
printf("PCB%d ",p);
p = pcb[p].next;
}
printf("%s\n",pcb[erun].pb);
}
}
void Givestatus(int s,int i){
strcpy(pcb[i].pb,"-1");
pcb[s].prior = -1;
pcb[i].next = -1;
pcb[i].prior = -1;
pcb[i].status = 'N';
}
//撤销一个进程
void Deletepcb(char* name){
if(strcmp("-1",name) != 0)
for(int i = 0; i < 255; i++){
if(strcmp(pcb[i].pb,name) == 0){
if(i == srun ){
srun = pcb[i].next;
Givestatus(srun,i);
return ;
}
if(i == erun ){
erun = pcb[i].prior;
pcb[erun].next = -1;
Givestatus(srun,i);
return ;
}
if(i == sready ){
sready = pcb[i].next;
pcb[sready].prior = -1;
Givestatus(erun,i);
return;
}
if(i == eready ){
eready = pcb[i].prior;
pcb[eready].next = -1;
Givestatus(eready,i);
return;
}
if(i == swait ){
swait = pcb[i].next;
pcb[swait].prior = -1;
Givestatus(swait,i);
return;
}
if(i == ewait ){
ewait = pcb[i].prior;
pcb[ewait].next = -1;
Givestatus(ewait,i);
return;
}
pcb[pcb[i].next].prior =pcb[i].prior;
pcb[pcb[i].prior].next =pcb[i].next;
pcb[i].next = -1;
pcb[i].prior = -1;
pcb[i].status = 'N';
return;
}
}
}
//挂起一个进程并插入等待队列
void Letwait(char* name){
for(int i = 0; i < 255; i++){
if(strcmp(pcb[i].pb,name) == 0 && pcb[i].status == 'p' ){ Deletepcb(name);//从运行队列中删除的进程 insert2(name);//插入等待队列
return;
}
}
printf("您要挂起的进程不存在!");
}
void main(){
int choice;//选项
int tag = 1;
for(int i = 0; i < 255; i++){
strcpy(pcb[i].pb,"-1");
pcb[i].next = -1;
pcb[i].prior = -1;
pcb[i].status = 'N';
}
init();
printf("目前就绪的进程有:");
Print('r');
printf("目前等待的进程有:");
Print('w');
printf("目前运行的进程有:");
Print('p');
while(choice != 4){
printf("choice:\n");
printf("1:创建一个进程\n");
printf("2:撤销一个进程\n");
printf("3:挂起一个进程\n");
printf("4:退出\n");
printf("enter your choice!\n");
char name[6];
scanf("%d",&choice);
if(choice != 1 && choice != 2 && choice != 3 && choice != 4) printf("\ndon't have this choice!Enter your choice again!");
if(choice == 1){
printf("输入要创建的进程名:\n");
scanf("%s",name);
creatpcb(name);
printf("目前就绪的进程有:");
Print('r');
printf("目前等待的进程有:");
Print('w');
printf("目前运行的进程有:");
Print('p');
}
else if(choice == 2){
printf("输入要撤销的进程名:\n");
scanf("%s",name);
Deletepcb(name);
printf("目前就绪的进程有:");
Print('r');
printf("目前等待的进程有:");
Print('w');
printf("目前运行的进程有:");
Print('p');
}
else if(choice == 3){
printf("输入要挂起的进程名:\n");
scanf("%s",name);
Letwait(name);
printf("目前就绪的进程有:");
Print('r');
} } printf("目前等待的进程有:"); Print('w'); printf("目前运行的进程有:"); Print('p'); } printf("\----------------------------------------------------\n");
4.结果截屏如图所示:
5.实验分析:程序中仍含有大量冗余代码,可进一步改进。