📄 lp.cpp
字号:
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define maxsize 1024
int Memory[maxsize],flag=0,flag_number=0,count=0;
int spare_memory=maxsize;
struct pcb //pcb结构体的定义.
{ char name[10];
int id;
int state; //规定0:空白,1:就绪,2:运行,3阻塞.
int address;
int length;
};
typedef struct node //链表的描述.
{ pcb pcb_data;
struct node *next;
}linklist;
linklist *null_head=NULL,*ready_head=NULL,*run_head=NULL,*block_head=NULL,*variable=NULL;//空,就绪,运行,阻塞三个队列的队头.
//******************************************************************
linklist *nullpcb_list() //空白PCB队列的建立.
{
linklist *head,*s;
head=NULL;
int i;
for(i=9;i>=0;i--)
{
s=(linklist *)malloc(sizeof(linklist));
s->pcb_data.id=i+1;
s->pcb_data.length=-1;
s->next=head;
head=s;
}
return head;
}
//********************************************
void Memory_()
{
for(int i=0;i<=(maxsize-1);i++)
Memory[i]=0;
}
//********************************************
void out_put()
{
cout<<"Thanks for using my program !"<<endl
<<"CopyRight(c) 2004-2018 LIPENG "<<endl
<<"This is a program for OS "<<endl
<<"Input your command:(please enter 'h' for help)"<<endl
<<">";
}
//***************************************************
void list()
{
cout<<"help: Commmand list:"<<endl
<<"C: Creat a process.You must input the name of process."<<endl
<<"D: Destroy a process.You must input the name of process."<<endl
<<"P: Call a process.You must input the name of process."<<endl
<<"E: End the process"<<endl
<<"B: Block the process.You must input the name of process."<<endl
<<"W: Wakeup the process.You must input the name of process."<<endl
<<"T: Time over"<<endl
<<"S: Show All the process name."<<endl
<<"H: Help."<<endl
<<"Enter: Execute a comamd ramdom."<<endl
<<"#: Exit the program."<<endl;
}//*****************************************************************
int not_number(char *number1)//???????????????????????????????????
{
int i=0;
int tokenval=0;
while(*(number1+i)!='\0')
{
if((*(number1+i)<'0')||(*(number1+i)>'9'))
{
*(number1+i)='\0';
flag_number=1;
tokenval=0;
}
else
{
tokenval=tokenval*10+*(number1+i)-'0';
i++;
}
}
return tokenval;
}
//*******************************************************
void memory(int pcbmemory,int sparememory) ////////////////////////
{ int w;
w=sparememory-pcbmemory;
if(w>=0)
{
cout<<"该进程占据内存 :"<<pcbmemory<<"K"<<endl;
cout<<"当前剩余空间 :"<<sparememory<<"K"<<endl;
cout<<"创建后内存将剩余:"<<sparememory-pcbmemory<<"K"<<endl;
}
else
{
cout<<"您的内存不够,不能创建该进程!"<<endl;
cout<<"该进程占据内存为:"<<pcbmemory<<"K"<<endl;
cout<<"当前剩余空间仅为:"<<sparememory<<"K"<<endl;
}
}
//****************************************************************
linklist *headout(linklist *outhead) //null pcb出队列函数.
{ linklist *p;
variable=outhead;
p=outhead->next;
variable->next=NULL;
outhead=p;
return outhead;
}
//******************************************************
linklist *else_headout(linklist *outhead,char p_name[10])//非空pcb出队函数.
{
linklist *p,*q;
int i=0;
if(outhead->next==NULL)
{
if(strcmp(outhead->pcb_data.name,p_name)==0)
{
variable=outhead;
variable->next=NULL;
outhead=NULL;
flag=1;
return outhead;
}
else
{ cout<<"系统无法找到或操作 '"<<p_name<<"' 进程!"<<endl;
flag=0;
return outhead;
}
}
else if(strcmp(outhead->pcb_data.name,p_name)==0)
{
q=outhead->next;
variable=outhead;
variable->next=NULL;
outhead=q;
flag=1;
return outhead;
}
else
{ p=outhead;
while((outhead->next!=NULL)&&(i==0))
{
if(strcmp(outhead->next->pcb_data.name,p_name)==0)
{ q=outhead->next->next;
variable=outhead->next;
variable->next=NULL;
outhead->next=q;
flag=1;
i=1;
}
else
{
outhead=outhead->next;
}
}
if(i==0)
{
flag=0;
cout<<"系统无法找到或操作 '"<<p_name<<"' 进程!"<<endl;
}
outhead=p;
return outhead;
}
}
//*****************************************************************
linklist *headin(linklist *inhead,linklist *q) //pcb入队列函数.
{
linklist *w;
w=inhead;
if(inhead==NULL)
{
q->next=NULL;
inhead=q;
return inhead;
}
else
{
for(int i=0;i<=9;i++)
{ if(inhead->next==NULL)
{
i=10;
}
else
{
inhead=inhead->next;
}
}
inhead->next=q;
q->next=NULL;
inhead=w;
return inhead;
}
}
//**************************************************************************
int deal_memory(int *memory,int length_) //分配内存函数并返回address.
{
int address_,count=0,i=0,j;
for(int k=0;k<maxsize;k++)
{
while(i<=(maxsize-1))
{
if(*(memory+i)==0)
{ address_=i;
i=maxsize;
}
else
{
i++;
}
}
for(j=address_;j<=(address_+length_-1);j++)
{ if(*(memory+j)==0)
{
*(memory+j)=1;
count++;
if(count==length_)
{
k=maxsize;
}
}
else
{
i=j;
j=maxsize;
k++;
}
}
}
return address_;
}
//***********************************************************
void callBackMemory(int address_1,int length_) //回收内存函数
{
int j;
for(j=address_1;j<=(address_1+length_-1);j++)
{
Memory[j]=0;
}
}
//***************************************************************************8
void create()
{
if(count<=10)
{
char name1[10],input;
int memory_pcb1;
char memory_pcb2[10];
cout<<"please enter the name of pcb:"<<endl;
cin>>name1;
strcpy(null_head->pcb_data.name,name1);
cout<<"请输入创建该进程所需要的内存(单位:K):"<<endl;
cin>>memory_pcb2;
memory_pcb1=not_number(memory_pcb2);
if(flag_number!=1)
{
cout<<"确认是否创建该进程:Y/N?"<<endl;
cin>>input;
if(input=='y')
{
null_head->pcb_data.length=memory_pcb1;
memory(memory_pcb1,spare_memory);
null_head=headout(null_head);
variable->pcb_data.address=deal_memory(Memory,variable->pcb_data.length);
ready_head=headin(ready_head,variable);
spare_memory=spare_memory-variable->pcb_data.length;
ready_head->pcb_data.state=1;
count++;
cout<<endl;
cout<<"进程 '"<<name1<<"' 已创建成功!"<<endl;
cout<<"NAME :"<<name1<<endl
<<"ID :"<<variable->pcb_data.id<<endl;
if(ready_head->pcb_data.state==1)
{
cout<<"STATE :"<<"READY"<<endl;
}
cout<<"ADDRESS:"<<variable->pcb_data.address<<endl
<<"LENGTH :"<<variable->pcb_data.length<<endl;
}
else if(input=='n')
{
cout<<"您刚才创建的名为 '"<<name1<<"' 的进程已被系统成功删除!"<<endl;
}
else
{
cout<<"该命令非法!"<<endl;
}
}
else
{
cout<<"该数字非法!!!"<<endl;
flag_number=0;
}
}
else
{
cout<<"PCB已无剩余,不可再申请!"<<endl;
}
}
//*********************************************************************
void call()
{
char name_call[10];
if(ready_head==NULL)
{
cout<<"系统中无READY进程!"<<endl;
}
else if(run_head!=NULL)
{
cout<<"系统中存在正在运行的进程,该进程无法运行,请等待..."<<endl;
}
else
{
cout<<"Please enter the name you want to call:"<<endl;
cin>>name_call;
ready_head=else_headout(ready_head,name_call);
if(flag==1)
{
variable->pcb_data.state=2;
run_head=headin(run_head,variable);
cout<<"进程 '"<<run_head->pcb_data.name<<"' 开始运行!"<<endl;
flag=0;
}
else
{
cout<<"系统中无 '"<<name_call<<"' 进程!"<<endl;
}
}
}
//***************************************************************************
void destroy()
{ char name_destroy[10];
if(run_head==NULL)
{
cout<<"系统中无READY进程!"<<endl;
}
else
{
cout<<"Please enter the name you want to destroy:"<<endl;
cin>>name_destroy;
run_head=else_headout(run_head,name_destroy);
if(flag==1)
{
null_head=headin(null_head,variable);
callBackMemory(variable->pcb_data.address,variable->pcb_data.length);
variable->pcb_data.state=0;
spare_memory=spare_memory+variable->pcb_data.length;
cout<<"进程 '"<<variable->pcb_data.name<<"' 已被系统删除!"<<endl
<<"PCB '"<<variable->pcb_data.name<<"' 原占用系统内存 "<<variable->pcb_data.length<<" K已释放!"<<endl;
flag=0;
count--;
}
cout<<"系统剩余空间: "<<spare_memory<<" K"<<endl;
}
}
//*********************************************************************************
void end()
{
char name_end[10];
if(run_head==NULL)
{
cout<<"系统中无RUN进程!"<<endl;
}
else
{
cout<<"Please enter the name you want to block:"<<endl;
cin>>name_end;
run_head=else_headout(run_head,name_end);
if(flag==1)
{
ready_head=headin(ready_head,variable);
variable->pcb_data.state=1;
cout<<"进程 '"<<variable->pcb_data.name<<"' 已进入就绪状态!"<<endl;
flag=0;
}
}
}
//***********************************************************************************
void block()
{
char name_block[10];
if(run_head==NULL)
{
cout<<"系统中无RUN进程!"<<endl;
}
else
{
cout<<"Please enter the name you want to block:"<<endl;
cin>>name_block;
run_head=else_headout(run_head,name_block);
if(flag==1)
{
block_head=headin(block_head,variable);
variable->pcb_data.state=3;
cout<<"进程 '"<<variable->pcb_data.name<<"' 已进入阻塞状态!"<<endl;
flag=0;
}
}
}
//**************************************************************************************
void wakeup()
{
char name_wakeup[10];
if(block_head==NULL)
{
cout<<"系统中无阻塞进程!"<<endl;
}
else
{
cout<<"Please enter the name you want to wakeup"<<endl;
cin>>name_wakeup;
block_head=else_headout(block_head,name_wakeup);
if(flag==1)
{
ready_head=headin(ready_head,variable);
variable->pcb_data.state=1;
cout<<"进程 '"<<variable->pcb_data.name<<"' 已进入就绪状态!"<<endl;
flag=0;
}
}
}
//******************************************************************************
void time()
{
cout<<"time over"<<endl;
}
//******************************************************************************
void show()
{ linklist *run,*ready,*block,*null_;
cout<<" READY : ";
ready=ready_head;
while(ready_head!=NULL)
{
cout<<ready_head->pcb_data.name<<" --> ";
ready_head=ready_head->next;
}
ready_head=ready;
cout<<"NULL"<<endl;
cout<<" RUN : ";
run=run_head;
while(run_head!=NULL)
{
cout<<run_head->pcb_data.name<<" --> ";
run_head=run_head->next;
}
run_head=run;
cout<<"NULL"<<endl;
cout<<" BLOCK : ";
block=block_head;
while(block_head!=NULL)
{
cout<<block_head->pcb_data.name<<" --> ";
block_head=block_head->next;
}
block_head=block;
cout<<"NULL"<<endl;
int i=0;
null_=null_head;
while(null_head!=NULL)
{
i++;
null_head=null_head->next;
}
null_head=null_;
cout<<" 空白PCB个数: "<<i<<" 个."<<endl;
cout<<" 进程共占用内存 "<<maxsize-spare_memory<<" K,剩余空间: "<<spare_memory<<" K"<<endl;
}
//********************************************************************************
void enter()
{
cout<<"The system is executing a command random..."<<endl;
}
void exit()
{
char c;
cout<<"Will you exit this system?Y/N..."<<endl;
cin>>c;
if(c=='y')
{
exit(0);
}
}
void main()
{
Memory_();
null_head=nullpcb_list();
out_put();
char in_put;
cin>>in_put;
while(in_put)
{
switch(in_put)
{
case('h'): list();
break;
case('c'): create();
break;
case('d'): destroy();
break;
case('p'): call();
break;
case('e'): end();
break;
case('b'): block();
break;
case('w'): wakeup();
break;
case('t'): time();
break;
case('s'): show();
break;
case('\n'): enter();
break;
case('#'): exit();
break;
default: cout<<"The word you enter is wrong~!"<<endl;
break;
}
cin>>in_put;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -