模拟PCB的plus版
来源:互联网 发布:淘宝网下载ipad版 编辑:程序博客网 时间:2024/05/01 23:36
#include "iostream"
using namespace std;
#define M_SIZE 200
typedef struct node
{
char ID[10];
struct node *next;
int beg;
int len;
}PCB;
typedef struct
{
int beg;
int len;
}memory;
void sorce(memory *m,int n)
{
int i=0,j=0;
memory temp;
for(i=0;i<n-1;i++)
{
for(j=n-1;j>i;j--)
{
if(m[j].len<m[j-1].len)
{
temp=m[j];
m[j]=m[j-1];
m[j-1]=temp;
}
}
}
if(m[0].len==0)
{
for(i=1;i<n;i++)
{
m[i-1]=m[i];
}
}
}
void free(memory *m,int& m_last,PCB *the)
{
bool pp=false;
int i=0,j=0,n=m_last;
for(i=0;i<n;i++)
{
sorce(m,m_last);
if(m[i].beg+m[i].len == the->beg) //上空
{
for(j=0;j<n;j++)
{
if(the->beg+the->len == m[j].beg) //上空下空
{
m[i].len=the->len+m[j].len+m[i].len;
m_last--;
m[j].beg=0;
pp=true;
break;
}
}
sorce(m,m_last);
if(pp)
break;
//上空下占
for(j=0;j<n;j++)
{
if(m[j].beg+m[j].len == the->beg)
{
m[i].len=m[i].len+the->len;
break;
}
}
break;
}
else //上占
{
for(j=0;j<n;j++)
{
if(the->beg+the->len == m[j].beg) //上占下空
{
m[j].beg=the->beg;
m[j].len=m[j].len+the->len;
pp=true;
break;
}
}
if(pp)
break;
//上占下占
m_last++;
m[m_last-1].beg=the->beg;
m[m_last-1].len=the->len;
break;
}
}
}
void login(char *username)
{
cout<<"Please enter your ID:";
cin>>username;
cout<<"Welcome to Little Dog [v1.0]"<<endl;
cout<<"<C> All Right Receive. By LG."<<endl;
}
void clew(char *username)
{
cout<<"["<<username<<" @ Little Dog]#";
}
int check(char* statement)
{
switch (*statement)
{
case 'k':
return 7;
case 'K':
return 7;
case 'C':
return 1;
case 'B':
return 2;
case 'T':
return 3;
case 'W':
return 4;
case 'S':
return 5;
case 'c':
return 1;
case 'b':
return 2;
case 't':
return 3;
case 'w':
return 4;
case 's':
return 5;
case 'q':
return 6;
case 'Q':
return 6;
case 'h':
return 0;
case 'H':
return 0;
default:
return -1;
}
}
PCB *pro(void)
{
PCB *s,*r,*head;
head=new PCB;
r=head;
for(int i=0;i<10;i++)
{
s=new PCB;
strcpy(s->ID,"null");
if(i==0)
head->next=s;
else
{
r->next=s;
r=s;
}
}
r->next=NULL;
return head;
}
void init(memory* m)
{
for(int i=0;i<20;i++)
{
m[i].beg=0;
m[i].len=0;
}
m[0].len=M_SIZE;
}
int main()
{
char username[20]; //用户ID
char statement[20]; //指令
int enable; //校检标示
memory m[20];
int m_last=1;
bool find=false;
init(m);
m_last=1;
PCB *temp,*last,*t;
last=new PCB;
temp=new PCB;
int null_last=10;
int ready_last=0;
int block_last=0;
PCB *execute=new PCB; //执行块
execute->next=NULL;
PCB *h_block=new PCB; //阻塞队列
h_block->next=NULL;
PCB *h_ready=new PCB; //就绪队列
h_ready->next=NULL;
PCB *h_null=new PCB; //空白PCB
h_null->next=NULL;
PCB *r_ready=new PCB;
r_ready->next=NULL;
PCB *r_block=new PCB;
r_block->next=NULL;
PCB *r_null=new PCB;
r_null->next=NULL;
login(username);
clew(username);
h_null=pro();
while(true)
{
cin>>statement;
enable=check(statement);
if(enable==-1)
{
cout<<"Bad command. ";
cout<<"Please input h for help."<<endl;
clew(username);
continue;
}
else if(enable==6)
{
cout<<"Thank you for using Operation System Little Dog."<<endl;
cout<<"E-Mail:gldamao@gmail.com"<<endl;
exit(0);
}
else if(enable==0)
{
cout<<"h Command list for help."<<endl;
cout<<"c or C: Creat a new process."<<endl;
cout<<"b or B: Block a process."<<endl;
cout<<"t or T: Time out."<<endl;
cout<<"w or W: Wakeup the block process."<<endl;
cout<<"s or S: Show all the process."<<endl;
cout<<"k or K: Kill a process."<<endl;
cout<<"q or Q: Exit Operation System LG."<<endl;
clew(username);
continue;
}
else if(enable==1) //Creat
{
if(null_last>1)
{
null_last--;
if(!execute->next)
{
// init(m);
cout<<"Set the name of this process:";
r_null=h_null->next;
h_null->next=r_null->next;
cin>>r_null->ID;
cout<<"The size of memory:";
cin>>r_null->len;
if(m_last==1) //第一个进入的进程
{
r_null->beg=0;
m[0].beg=r_null->len+m[0].beg;
m[0].len=m[0].len-r_null->len;
}
else //前面有其他空余空间
{
for(int i=0;i<m_last;i++)
{
if(r_null->len<=m[i].len)
{
r_null->beg=m[i].beg;
m[i].beg=m[i].beg+r_null->len;
m[i].len=m[i].len-r_null->len;
break;
}
}
}
execute->next=r_null;
r_null->next=NULL;
cout<<"Creat new process success."<<endl;
}
else
{
ready_last++;
cout<<"Set the name of this process:";
r_null=h_null->next;
h_null->next=r_null->next;
cin>>r_null->ID;
cout<<"The size of memory:";
cin>>r_null->len;
if(m_last==1)
{
r_null->beg=m[0].beg;
m[0].beg=m[0].beg+r_null->len;
m[0].len=m[0].len-r_null->len;
}
else
{
for(int i=0;i<m_last;i++)
{
if(m[i].len>=r_null->len)
{
r_null->beg=m[i].beg;
m[i].beg=m[i].beg+r_null->len;
m[i].len=m[i].len-r_null->len;
break;
}
}
}
if(ready_last==1)
{
h_ready->next=r_null;
r_ready=r_null;
r_ready->next=NULL;
}
else
{
r_ready->next=r_null;
r_ready=r_null;
r_ready->next=NULL;
}
cout<<"Creat new process success."<<endl;
}
clew(username);
continue;
}
else
{
cout<<"NO free resource."<<endl;
clew(username);
continue;
}
}
else if(enable==2) //Block
{
if(!execute->next)
{
cout<<"No process is running."<<endl;
clew(username);
continue;
}
else
{
block_last++;
if(block_last==1)
{
h_block->next=execute->next;
r_block=execute->next;
r_block->next=NULL;
if(h_ready->next)
{
ready_last--;
execute->next=h_ready->next;
h_ready->next=h_ready->next->next;
execute->next->next=NULL;
}
else
{
execute->next=NULL;
}
}
else
{
r_block->next=execute->next;
r_block=execute->next;
r_block->next=NULL;
if(h_ready->next)
{
ready_last--;
execute->next=h_ready->next;
h_ready->next=h_ready->next->next;
}
else
execute->next=NULL;
}
cout<<"Block process success."<<endl;
clew(username);
continue;
}
}
else if(enable==3) //Time out
{
if(!execute->next)
{
cout<<"No process is running."<<endl;
}
else if(ready_last==0)
{
if(!h_ready->next)
cout<<"command success."<<endl;
else
cout<<"No ready process."<<endl;
}
else
{
r_ready->next=execute->next;
execute->next=h_ready->next;
r_ready=r_ready->next;
r_ready->next=NULL;
h_ready->next=h_ready->next->next;
execute->next->next=NULL;
cout<<"command success."<<endl;
}
clew(username);
continue;
}
else if(enable==4) //wake up
{
if(block_last==0)
cout<<"No block process."<<endl;
else
{
block_last--;
if(!execute->next)
{
execute->next=h_block->next;
h_block->next=h_block->next->next;
execute->next->next=NULL;
}
else
{
ready_last++;
r_ready->next=h_block->next;
r_ready=r_ready->next;
if(!h_ready->next)
{
h_ready->next=r_ready;
}
if(h_block->next->next)
{
h_block->next=h_block->next->next;
r_ready->next=NULL;
}
else
{
h_block->next=NULL;
}
}
cout<<"One process has been wake up."<<endl;
}
clew(username);
continue;
}
else if(enable==5) //show
{
if(ready_last>0)
{
cout<<"Waiting process:"<<endl;
temp=h_ready->next;
for(int j=0;j<ready_last;j++)
{
cout<<"Process ID:"<<temp->ID<<" memory:"<<temp->len<<" ";
temp=temp->next;
}
cout<<endl;
}
else
cout<<"No waiting process."<<endl;
if(execute->next)
{
cout<<"Running process:"<<endl;
cout<<"Process ID:"<<execute->next->ID<<" memory:"<<execute->next->len;
cout<<endl;
}
else
cout<<"No running process."<<endl;
if(block_last>0)
{
cout<<"Blocking process:"<<endl;
temp=h_block->next;
for(int j=0;j<block_last;j++)
{
cout<<"Process ID:"<<temp->ID<<" memory:"<<temp->len<<" ";
temp=temp->next;
}
cout<<endl;
}
else
{
cout<<"No blocking process."<<endl;
}
cout<<"The free memory(Total:0k-200k):"<<endl;
for(int i=0;i<m_last;i++)
{
// if(m[i].len!=0)
cout<<m[i].beg<<" "<<m[i].len<<endl;
}
clew(username);
continue;
}
else if(enable==7) //Kill
{
cout<<"Which process do you want to kill:";
cin>>statement;
temp=h_ready->next;
last=h_ready;
find=false;
while(temp)
{
//if(strcmp(temp->ID,statement))
if(temp->ID[0]==*statement)
{
null_last++;
ready_last--;
free(m,m_last,temp);
t=h_null->next;
h_null->next=last->next;
last->next=temp->next;
temp->next=t;
cout<<"command successed."<<endl;
find=true;
break;
}
last=temp;
temp=temp->next;
}
if(find)
{
clew(username);
sorce(m,m_last);
continue;
}
if(h_block->next)
{
temp=h_block->next;
last=h_block;
while(temp)
{
//if(strcmp(temp->ID,statement)
if(temp->ID[0]==*statement)
{
block_last--;
null_last++;
free(m,m_last,temp);
t=h_null->next;
h_null->next=last->next;
last->next=temp->next;
temp->next=t;
cout<<"command successed."<<endl;
find=true;
break;
}
last=temp;
temp=temp->next;
}
if(find)
{
clew(username);
sorce(m,m_last);
continue;
}
}
temp=execute->next;
if(execute->next)
// if(strcmp(execute->next->ID,statement))
{
if(temp->ID[0]==*statement)
{
free(m,m_last,temp);
null_last++;
t=h_null->next;
h_null->next=execute->next;
h_null->next->next=t;
if(h_ready->next)
{
execute->next=h_ready->next;
if(h_ready->next->next)
h_ready->next=h_ready->next->next;
else
h_ready->next=NULL;
ready_last--;
execute->next->next=NULL;
}
else
{
execute->next=NULL;
}
cout<<"command successed."<<endl;
clew(username);
sorce(m,m_last);
continue;
}
}
cout<<"The process not be found."<<endl;
clew(username);
}
}
return 0;
}
- 模拟PCB的plus版
- 模拟PCB
- plus-one:数组模拟的大数+1
- 模拟电路和数字电路PCB设计的区别
- PCB设计中数字地、模拟地、数字电源、模拟电源的处理方式
- C++ PRIMER PLUS 第12章 队列模拟的问题
- C.Primer.Plus(第六版)第12章 队列模拟
- PCB Design - 如何连接模数转换器的模拟和数字地
- SQL*Plus 模拟 EBS环境
- PCB板在线模拟测试技术简介
- pcb加工对pcb设计的要求
- 平衡PCB层叠PCB设计的方法
- 平衡PCB层叠PCB设计的方法
- PCB 的寄生效应(PCB Layout Parasitics)
- C plus plus 的多态性
- 疯狂的图形(利用C# + GDI plus模拟杂乱无章的现实场景)
- 关于PCB板的
- PCB测试的策略
- ASP.NET常用代码
- 模拟PCB
- 家
- 男人的承诺、男人的感情、男人的理由
- Managing Configuration Information
- 模拟PCB的plus版
- HQL中的时间比较
- 读写模版的操作
- 天道筹勤,但我不知道结果会怎么样
- 玩转IDT(PHRACK59-0x04)
- oracle连接串
- ASP.NET中怎样截取文章标题或者内容的一部分进行显示ASP.NET中怎样截取文章标题或者内容的一部分进行显示
- word-wrap同break-word的区别
- 程序设计与人体条件反射