模拟PCB

2019-07-14 10:29发布

#include "iostream"    
using namespace std; typedef struct node
{
 char ID[10];
 struct node *next;
}PCB; void login(char *username)
{
 cout<<"Please enter your ID:";
 cin>>username;
 cout<<"Welcome to Little Dog [v1.0]"< cout<<" All Right Receive.   By LG."<{
 cout<<"["<}  
int check(char* statement)
{
 switch (*statement)
 {
 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;
} int main()
{
 char username[20];      //用户ID
 char statement[20];      //指令
 int enable;        //校检标示
 
 PCB *temp;  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."<   clew(username);
   continue;
  }
  else if(enable==6)
  {
   cout<<"Thank you for using Operation System Little Dog."<   cout<<"E-Mail:gldamao@gmail.com"<   exit(0);
  }
  else if(enable==0)
  {
   cout<<"h Command list for help."<   cout<<"c or C: Creat a new process."<   cout<<"b or B: Block a process."<   cout<<"t or T: Time out."<   cout<<"w or W: Wakeup the block process."<   cout<<"s or S: Show all the process."<   cout<<"q or Q: Exit Operation System LG."<   clew(username);
   continue;
  }   else if(enable==1)    //Creat
  {
   if(null_last>1)
   {
    null_last--;
    if(!execute->next)
    {
     cout<<"Set the name of this process:";
     r_null=h_null->next;
     h_null->next=r_null->next;
     cin>>r_null->ID;
     execute->next=r_null;
     r_null->next=NULL;
     cout<<"Creat new process success."<    }     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;
     
     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."<    }
    
    clew(username);
    continue;    }    else
   {
    cout<<"NO free resource."<    clew(username);
    continue;
   }
  }
  else if(enable==2)    //Block
  {
   if(!execute->next)
   {
    cout<<"No process is running."<    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."<    clew(username);
    continue;
   }
  }   else if(enable==3)    //Time out
  {
   if(!execute->next)
   {
    cout<<"No process is running."<   }
   else if(ready_last==0)
   {
    cout<<"No ready process."<   }
   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."<   }
   
   clew(username);
   continue;
  }
  
  else if(enable==4)    //wake up
  {
   if(block_last==0)
    cout<<"No block process."<   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."<   }     clew(username);
    continue;
  }
   
  else if(enable==5)    //show
  {
   if(ready_last>0)
   {
    cout<<"Waiting process:"<    temp=h_ready->next;
    for(int j=0;j    {
     cout<<"Process ID:"<ID<<"  ";
     temp=temp->next;
    }
    cout<   }    else
    cout<<"No waiting process."<next)
   {
    cout<<"Running process:"<    cout<<"Process ID:"<next->ID;
    cout<   }
   else
    cout<<"No running process."<0)
   {
    cout<<"Blocking process:"<    temp=h_block->next;     for(int j=0;j    {
     cout<<"Process ID:"<ID<<"  ";
     temp=temp->next;
    }
    cout<   }
   
   else
   {
    cout<<"No blocking process."<   }
       clew(username);
   continue;
  }  }   return 0;
}