?? pcb.cpp
字號:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h" //控制臺輸入輸出頭文件
#include "iostream.h"
#include "windows.h" //包含Sleep()函數的頭文件!
#define SEC 3
#define NULL 0
typedef struct PCB
{
int PID;
int UID;
struct PCB * next;
}PCB;
PCB *ready , *excute , *wait;
int enqueue(PCB *head , PCB *node)
{
PCB *p;
p = head;
if(p -> next == NULL)
{
head -> next = node;
return 1;
}
while(p)
{
if(p -> next == NULL)
{
p -> next = node;
return 1;
}
else
p = p -> next;
}
}
/*dequeue 出隊列 */
PCB * dequeue(PCB *head)
{
PCB *p;
p = head;
if(p -> next == NULL)
{
return NULL;
}
else
{
p = p -> next;
head -> next = p -> next;
p -> next = NULL;
return p;
}
}/*dequeue*/
/*新建進程,創建->就緒*/
int create()
{
PCB *p;
p = new PCB;
p -> next = NULL;
cout<<"input PID and UID to a new process"<<endl;
cin>>p->PID>>p->UID;
if(enqueue(ready , p))
cout<<"create a process:PID="<<p->PID<<" UID="<<p->UID<<endl;
else
cout<<"create failed"<<endl;
return 1;
}/*create*/
/*就緒->執行*/
int fexcute()
{
PCB *p = dequeue(ready);
if(p == NULL)
{
cout<<"NO process in queue "<<endl;
return 0;
}
else
{
enqueue(excute , p);
cout<<"add a process into excute queue process: PID ="<<p->PID<<" UID="<<p->UID<<endl;
return 1;
}
}
/*執行->就緒:時間片完(延緩)*/
int suspend()
{
PCB *p = dequeue(excute);
if(p == NULL)
{
cout<<"NO process in queue"<<endl;
return 0;
}
else
{
enqueue(ready , p);
cout<<"add a process into ready queue process: PID ="<<p->PID<<" UID="<<p->UID<<endl;
return 1;
}
}
/*阻塞->就緒*/
int wake()
{
PCB *p = dequeue(wait);
if(p == NULL)
{
cout<<"NO process in queue"<<endl;
return 0;
}
else
{
enqueue(ready , p);
cout<<"add a process into ready queue process: PID ="<<p->PID<<" UID="<<p->UID<<endl;
return 1;
}
}
/*執行->阻塞*/
int block()
{
PCB *p = dequeue(excute);
if(p == NULL)
{
cout<<"NO process in queue"<<endl;
return 0;
}
else
{
enqueue(wait , p);
cout<<"add a process into wait queue process: PID ="<<p->PID<<" UID="<<p->UID<<endl;
return 1;
}
}/*block*/
/*輸出隊列 outputqueue*/
int outputqueue(PCB *head)
{
PCB *p;
if(head -> next == NULL)
{
cout<<"queue is null"<<endl;
return 1;
} /*隊列為空*/
p = head -> next;
while(p)
{
cout<<" "<<"PID = "<<p->PID<<" UID = "<<p->UID<<endl;
p = p -> next;
} /*打印process id UID*/
return 0;
}
/*output輸出*/
void output()
{
cout<<"READY QUEUE:";
outputqueue(ready);
cout<<endl;
cout<<"EXCUTE QUEUE:";
outputqueue(excute);
cout<<endl;
cout<<"WAIT QUEUE: ";
outputqueue(wait);
cout<<endl;
}
/*init 初始化*/
int init()
{
PCB *p;
system("CLS"); //用于清屏的C庫函數
ready=new PCB;
ready -> next=NULL;
excute=new PCB;
excute -> next=NULL;
wait=new PCB;
wait -> next = NULL;
cout<<"--------------PROCESS MENU---------------"<<endl;
cout<<"now initing....................."<<endl;
cout<<"input PID and UID as integer , 0 0 as over"<<endl;
while(1)
{
p=new PCB;
p -> next = NULL;
cin>>p->PID>>p->UID;
if(p -> PID == 0 && p -> UID == 0)
break;
else
{
if(enqueue(ready , p))
{
cout<<"new process PID = "<<p->PID<<" UID = "<<p->UID<<" added!"<<endl;
}
else
return 0;
}
}
return 1;
} /*init*/
/*運行一個process*/
int run()
{
PCB *p = excute;
int s = SEC;
if(excute -> next == NULL)
{
cout<<"no process in excute queue "<<endl;
return 0;
}
else
{
p = excute -> next;
cout<<"system will sleep "<<s<<"s as process running"<<endl;
Sleep(3);
cout<<"process: PID = "<<p->PID<<" UID = "<<p->UID<<" excute successed.."<<endl;
excute -> next = p -> next;
free(p);
return 1;
}
}
int leave()
{
PCB *p,*t;
while(ready->next || excute->next || wait->next)
{
p = ready -> next;
while(p)
{
t = p -> next;
free(p);
p = t;
}
ready -> next = NULL;
p = wait -> next;
while(p)
{
t = p -> next;
free(p);
p = t;
}
wait -> next = NULL;
p = excute -> next;
while(p)
{
t = p -> next;
free(p);
p = t;
}
excute -> next = NULL;
}
exit(0);
}/*leace*/
void help()
{
cout<<"_____________________HELP MENU_____________________"<<endl;
cout<<"\t-h HELP show help option"<<endl;
cout<<"\t-c CREATE create a new process , and put to ready queue"<<endl;
cout<<"\t-b BLOCK block a process in excute queue"<<endl;
cout<<"\t-w WAKE wake a process in wait queue"<<endl;
cout<<"\t-e EXCUTE excute a process in ready queue"<<endl;
cout<<"\t-s SUSPEND suspend a process in excute queue"<<endl;
cout<<"\t-o OUTPUT output all processes in queues"<<endl;
cout<<"\t-r RUN excute a process in excute queue"<<endl;
cout<<"\t-x EXIT exit this program"<<endl;
cout<<"___________________________________________________"<<endl;
cout<<"\t type 'H' will show this menu"<<endl;
}/*help*/
int main()
{
char COMMAND = NULL;
if( init() != 1)
{
cout<<"init falied ! "<<endl;
getch();
exit(0);
}
else
{
cout<<"init...OK"<<endl;
output();
help();
}
while(1)
{
/*當三隊列都不空 執行調度 */
cout<<">";
cin>>COMMAND;
switch(COMMAND)
{
case '\n': break;
case 'H':
case 'h': help(); break;
case 'C':
case 'c': create(); break;
case 'B':
case 'b': block(); break;
case 'W':
case 'w': wake(); break;
case 'S':
case 's': suspend(); break;
case 'E':
case 'e': fexcute(); break;
case 'O':
case 'o': output(); break;
case 'X':
case 'x': leave(); break;
case 'R':
case 'r': run(); break;
}
}
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -