?? pro1.cpp
字號:
// pro1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define BUFFSIZE 2
struct PCB
{
int flag;//1:生產者;0:消費者
int state;//1:就緒;0:等待
char name[10];//名字
};
typedef struct QNode
{//定義隊列的結點的結構
PCB data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{//定義隊列的結構
QueuePtr front; //隊頭指針
QueuePtr rear; //隊尾指針
}LinkQueue;
typedef struct
{//定義鏈表的結構
QueuePtr head; //鏈表指針
int length; //隊尾指針
}Linklist;
int Initlist (Linklist &L)
{//初始化鏈表
L.head=(QueuePtr) malloc(sizeof(QNode));
if(!L.head) exit(0);
L.head->next=NULL;
L.length=0;
return OK;
}
int EnList(Linklist &L,QueuePtr p)
{//入隊
QueuePtr q;
q=(QueuePtr)malloc(sizeof(QNode));
if(!q)
exit(0);
q=L.head->next;
L.head->next=p;
p->next=q;
return OK;
}
int InitQueue (LinkQueue &Q)
{//初始化隊列
Q.front=Q.rear=(QueuePtr) malloc(sizeof(QNode));
if(!Q.front) exit(0);
Q.front->next=NULL;
return OK;
}
int EnQueue(LinkQueue &Q,PCB e)
{//入隊
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(0);
p->data=e; p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
int DeQueue (LinkQueue &Q,PCB &e)
{//出隊
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return OK;
}
int create(LinkQueue &Q)
{
int a,i;
system("cls");
cout<<endl<<endl<<endl<<endl;
cout<<" **************************************************"<<endl;
cout<<" * ^-^ 歡迎使用此系統 ^-^ * "<<endl;
cout<<" * * "<<endl;
cout<<" **************************************************"<<endl;
cout<<" 請輸入您需要的進程數(請不要輸入過大):";
cin>>i;
for(a=0;a<i;a++)
{
PCB e;
cout<<"請輸入第"<<a+1<<"個進程的名字:"<<endl;
cin>>e.name;
cout<<endl<<"請輸入這個進程是生產者還是消費者(1為生產者,0為消費者):"<<endl;
cin>>e.flag;
e.state=1;//新產生的是就緒隊列,所以開始狀態是1(就緒)
EnQueue(Q,e);
}
cout<<"就緒隊列創建成功,它是:"<<endl;
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
p=Q.front->next;
while(p!=NULL)
{
cout<<p->data.name<<" ";
if(p->data.flag==1)
cout<<"生產者;"<<endl;
else
cout<<"消費者;"<<endl;
p=p->next;
}
return OK;
}
void display(LinkQueue &Q)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
p=Q.front->next;
while(p!=NULL)
{
cout<<p->data.name<<" ";
p=p->next;
}
}
int main(int argc, char* argv[])
{
char c;
do
{
int productnum=0;//已經生產的產品的數目
int full=0;//緩存是否滿的信號,若為0,則未滿,若為1,則已滿
int empty=0;//緩存是否為空的信號,若為0,則空,若為BUFFSIZE,則已滿.開始時無資源,所以將其置為空
char buff[BUFFSIZE];
int buffpoint=0;//緩存區的指針
PCB e;
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
LinkQueue ready,waitcustomer,waitproducer;//產生就緒隊列,生產者隊列,消費者隊列
InitQueue (ready);
InitQueue (waitcustomer);
InitQueue (waitproducer);
Linklist over;//產生執行鏈表
Initlist (over);
create(ready);//生成就緒隊列
p=ready.front->next;
while(p!=NULL)
{
cout<<endl<<p->data.name<<"申請運行,它是一個";
if(p->data.flag==1)
{
cout<<"生產者";
if(full==0)
{//若緩存不為滿,則讓生產者生產物品放入緩存
cout<<endl<<"批準申請";
buff[buffpoint]='b';//置為b,意為放入了資源
buffpoint++;
empty++;//生產了一個資源,所以 empty+1
if(buffpoint==BUFFSIZE)
full=1;//若buff已經滿,則full=1
DeQueue (ready,e);//從就緒隊列中取出;
QueuePtr q;
q=(QueuePtr)malloc(sizeof(QNode));
q->data=e;
EnList(over,q);//放入已執行鏈表
}
else
{
cout<<endl<<"緩存已滿,進入生產等待隊列等待;";
DeQueue (ready,e);//從就緒隊列中取出;
e.state=0;//等待
EnQueue(waitproducer,e);//進入生產等待隊列
}
}
else
{
cout<<"消費者";
if(empty!=0)
{//若緩存不為空,則讓消費者取資源
cout<<endl<<"批準申請";
buff[buffpoint]='a';//把b置為a,意為取出資源
buffpoint--;
empty--;//使用了一個資源,所以 empty+1
if(buffpoint<BUFFSIZE)
full=0;//buff未滿
DeQueue (ready,e);//從就緒隊列中取出;
QueuePtr q;
q=(QueuePtr)malloc(sizeof(QNode));
q->data=e;
EnList(over,q);//放入已執行鏈表
}
else
{
cout<<endl<<"緩存為空,進入消費等待隊列等待;";
DeQueue (ready,e);//從就緒隊列中取出;
e.state=0;//等待
EnQueue(waitcustomer,e);//進入消費等待隊列
}
}
if(full!=1&&waitproducer.front!=waitproducer.rear)
{//緩存不為滿且生產者等待隊列不為空時讓生產者等待隊列隊頭的進程進入到就緒隊列中
DeQueue (waitproducer,e);//從生產者等待隊列中取出;
cout<<endl<<"由于有了空間"<<e.name<<"進入就緒隊列";
e.state=1;//再次就緒
EnQueue(ready,e);//進入就緒隊列
}
if(empty!=0&&waitcustomer.front!=waitcustomer.rear)
{//緩存不為空且消費者等待隊列不為空時讓消費者等待隊列隊頭的進程進入到就緒隊列中
DeQueue (waitcustomer,e);//從消費者等待隊列中取出;
cout<<endl<<"由于有了資源"<<e.name<<"進入就緒隊列";
e.state=1;//再次就緒
EnQueue(ready,e);//進入生產等待隊列
}
p=ready.front->next;//再讓p指向隊頭進程
}
cout<<endl<<"就緒隊列沒有進程";
if(waitcustomer.front!=waitcustomer.rear)
{
cout<<endl<<"消費者等待隊列中有進程:"<<endl;
display(waitcustomer);
}
else
cout<<endl<<"消費者等待隊列中沒有進程:"<<endl;
if(waitproducer.front!=waitproducer.rear)
{
cout<<endl<<"生產者等待隊列中有進程:"<<endl;
display(waitproducer);
}
else
cout<<endl<<"生產者等待隊列中沒有進程:"<<endl;
cout<<endl<<endl<<" 還要繼續再試一次么?(Y/N)";
cin>>c;
}while(c=='Y'||c=='y');
system("cls");
cout<<endl<<endl<<endl<<endl;
cout<<" **************************************************"<<endl;
cout<<" * ^-^ 謝謝使用 ^-^ * "<<endl;
cout<<" * * "<<endl;
cout<<" * * "<<endl;
cout<<" * * "<<endl;
cout<<" * 信息05-2 伊冰靜 050814226 *"<<endl;
cout<<" **************************************************"<<endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -