?? cpp1.cpp
字號:
#include <iostream.h>
#include <malloc.h>
#include "stdio.h"
#define null 0
#define running 1 /*用running 表示進程處于運行態*/
#define ready 2 /*用aready表示進程處于就緒態*/
#define blocking 3 /*用blocking表示進程處于等待態*/
//#define n 10 /*假定系統允許進程個數為10*/
struct pcb {
int name;//進程的名字(程序的入口地址)
int state;//進程狀態
int pr; //
struct pcb *next;
};
int m,n,k;//分別記錄就緒隊列,阻塞隊列的進程數,正在運行的進程的個數
struct pcb *ready_head,*r,*block_head,*b,*run_head,*run;
//----------------------------------------------------------
void enReadyqueue(struct pcb *p)//進入就緒隊列
{
if( ready_head==null){
ready_head=p;
r=ready_head;
r->next=p->next;
}
else { r->next=p;
r=p;
}
m++;
}
//----------------------------------------------------------
void create( )//創建進程,并把該進程壓進就緒隊列
{ struct pcb *p;
p=(struct pcb *)malloc(sizeof(struct pcb));
p->next=null;
p->state=2;
p->pr=1;
cout<<"please enter the program's name"<<endl;
cin>>p->name;
enReadyqueue(p);
cout<<"creat complete"<<endl;
}
//----------------------------------------------------------
void enBlockqueue( )//從運行隊列中取頭節點進入就阻塞隊列
{ struct pcb *p,*q;
p=run_head;
if(run_head==null)cout<<"run queue is empty,error"<<endl;
else{ if( block_head==null){
block_head=p;
b=block_head;
b->next=null;
cout<<"block finish"<<endl;
}
else {
b->next=p;
b=p;
b->next=null;
}
q=run_head;
run_head=run_head->next;
free(q);
k--;
}
n++;
}
//------------------------------------------------------------
void enRunqueue(struct pcb *p)//正在運行的進程進入運行隊列
{ if( run_head==null){
run_head=p;
run=run_head;
run->next=p->next;
}
else
{ run->next=p;
run=p;
}
k++;
}
//------------------------------------------------------------
void wakeup()//喚醒進程(把阻塞隊列的頭節點插入就緒隊列尾)
{ struct pcb *p,*q;
if(block_head==null) cout<<"blocked queue is empty,error"<<endl;
else { p=(struct pcb*)malloc(sizeof(struct pcb));
p->state=ready;
p->pr=1;
p->name=block_head->name;
p->next=null;
enReadyqueue(p);
q=block_head;
block_head=block_head->next;
free(q);
n--;
cout<<"wake up one program"<<endl;
}
}
//------------------------------------------------------------
void show()//顯示當前正在運行的進程
{ struct pcb *p;
p=run_head;
if(p==null) cout<<"run queue is empty,error"<<endl;
else cout<<"the number of running program is: "<<k<<endl;
while(p)
{ cout<<"the running program's name is: "<<p->name<<endl;
cout<<"the running program's state is: "<<p->state<<endl;
p=p->next;
}
}
//------------------------------------------------------------
void Run()//從就緒隊列中去頭節點運行
{ struct pcb *p;
p=(struct pcb*)malloc(sizeof(struct pcb));
if(ready_head==null) cout<<"ready queue is empty,error"<<endl;
else { p->state=running;
p->pr=1;
p->name=ready_head->name;
p->next=null;
enRunqueue(p);
ready_head=ready_head->next;
m--;
cout<<"a program is pushed into run_queue"<<endl;
}
}
//------------------------------------------------------------------
void delect(int e )//結束一個進程(從運行狀態隊列中刪掉一個進程)
{
struct pcb *p,*q;
int find;//用來判斷是否找到名字為e的進程
find=0;
q=p=run_head;
if(run_head==null)cout<<"the running queue is empty,error"<<endl;
else if(run_head->name==e)
{
run_head=run_head->next;
free(q);
find=1;
}
else{ p=run_head->next;
while(p)
{
if(p->name==e)
{
find=1;
q->next=p->next;
free(p);
k--;
}
else
{
q=p;
p=p->next;
}
}
}
if(find) cout<<"delect finish"<<endl;
else cout<<"error,can't find the program"<<endl;
}
//-----------------------------------------------------------------
void main()
{
b=block_head;
run=run_head;
int i; char j;
i=1;
while(i)
{ cout<<"--------------------------------------"<<endl;
cout<<"creat a program please enter c."<<endl;
cout<<"delect a program please enter d."<<endl;
cout<<"run a program please enter r."<<endl;
cout<<"block a program please enter b."<<endl;
cout<<"wake up a program please enter w."<<endl;
cout<<"watch the running programs please enter s."<<endl;
cout<<"--------------------------------------"<<endl;
cin>>j;
switch(j)
{
case 'c':
create();
break;
case 's':
show();
break;
case 'r':
Run();
break;
case 'b':
enBlockqueue();
break;
case 'w':
wakeup();
break;
case'd':
int e;
cout<<"enter the program's name that you want to delect"<<endl;
cin>>e;
delect(e);
break;
default:
break;
}
cout<<"if you want continnue please enter 1"<<endl;
cin>>i;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -