# define MAX 10
#include<iostream>
using namespace std;
int mutex = 1;
int full = 0;//产品个数
int empty = MAX;//空缓冲区个数
int pro = 0;
int con = 0;
int Wait(int n)
{
if(n == 0)
return 0;
else
{
n--;
return n;
}
}
int Signal(int n)
{
n++;
return n;
}
typedef struct Process
{
int attitude;
int processID;
int priority;
struct Process *next;
}*ProcessNode;
void Display(ProcessNode p)
{
int i = 0;
p = p->next;
cout<<"***************************************************************"<<endl;
while(p != NULL)
{
cout<<"进程P"<<p->processID;
switch(p->attitude)
{
case 1:cout<<"获得mutex正在执行..."<<endl;break;
case 2:cout<<"缓冲区已满,等待释放缓冲区..."<<endl;break;
case 3:cout<<"获得缓冲区,等待mutex..."<<endl;break;
case 4:cout<<"执行完成..."<<endl;break;
default:break;
}
p = p->next;
}
cout<<"***************************************************************"<<endl;
}
void ChangeMutex(ProcessNode &p,int choose)
{
int flag = 0;
ProcessNode q = p->next;
while(q != NULL)
{
if((flag == 0 && q->attitude ==3) || (flag == 0 && q->attitude == 1))
{
q->attitude = 1;
flag = 1;
q = q->next;
continue;
}
if(q->attitude == 1)
{
q->attitude = 3;
break;
}
q = q->next;
}
if(flag != 1)
{
if(choose == 1)
cout<<"请消费产品或者创建生产者."<<endl;
else
cout<<"请生产产品或者创建消费者."<<endl;
}
}
ProcessNode FindPosition(ProcessNode p,int priority)
{//插入到返回节点的后面
while(1)
{
if( p->next == NULL || p->next->priority >= priority)//要注意先后否则会出错
return p;
else
p = p->next;
}
}
void Build(ProcessNode &p,int n,int choose)//choose为1时生产
{//建立并按照优先数排序
p = (ProcessNode)malloc(sizeof(ProcessNode));
ProcessNode q=p,position = NULL,newNode = NULL;
for(int i = 0;i < n;i++)
{
cout<<"请分别输入进程P"<<i+1<<"的进程号和优先数:";
if(i == 0)
{
q->next = (ProcessNode)malloc(sizeof(ProcessNode));
cin>>q->next->processID>>q->next->priority;
q->next->next = NULL;
if(choose == 1)
{
if(Wait(empty) > 0 || empty == 1)
{
empty--;
cout<<"进程P"<<q->next->processID<<"申请临界段成功."<<endl;
q->next->attitude = 3;
pro++;
}
else
{
cout<<"进程P"<<q->next->processID<<"申请临界段失败."<<endl;
q->next->attitude = 2;
pro++;
}
}
else
if(choose == 0)
{
if( full == 1 || Wait(full) > 0)
{
full--;
cout<<"进程P"<<q->next->processID<<"申请临界段成功."<<endl;
q->next->attitude = 3;
con++;
}
else
{
cout<<"进程P"<<p->next->processID<<"申请临界段失败."<<endl;
q->next->attitude = 2;
con++;
}
}
}
else
{
newNode = (ProcessNode)malloc(sizeof(ProcessNode));
cin>>newNode->processID>>newNode->priority;
position = FindPosition(p,newNode->priority);
newNode->next = position->next;
position->next = newNode;
if(choose == 1)
{
if(Wait(empty) > 0 || empty == 1)
{
empty--;
cout<<"进程P"<<newNode->processID<<"申请临界段成功."<<endl;
newNode->attitude = 3;
pro++;
}
else
{
cout<<"进程P"<<newNode->processID<<"申请临界段失败."<<endl;
newNode->attitude = 2;
pro++;
}
}
else
{
if(full == 1 || Wait(full) > 0)
{
full--;
cout<<"进程P"<<newNode->processID<<"申请临界段成功."<<endl;
newNode->attitude = 3;
con++;
}
else
{
cout<<"进程P"<<newNode->processID<<"申请临界段失败."<<endl;
newNode->attitude = 2;
con++;
}
}
}
}
}
void Insert(ProcessNode &p,int choose)
{
int m = 0;
if(choose == 1)
{
cout<<"请输入生产者进程个数:";
}
else
{
cout<<"请输入消费者进程个数:";
}
cin>>m;
for(int i = 0;i < m;i++)
{
if(choose ==1)
{
cout<<"请分别输入新建进程P"<<++pro<<"的进程号和优先数:";
}
else
{
cout<<"请分别输入新建进程P"<<++con<<"的进程号和优先数:";
}
ProcessNode newNode = (ProcessNode)malloc(sizeof(ProcessNode));
cin>>newNode->processID>>newNode->priority;
ProcessNode position = FindPosition(p,newNode->priority);
newNode->next = position->next;
position->next = newNode;
if(choose == 1)
{
if(Wait(empty) > 0 || empty == 1)
{
empty--;
cout<<"进程P"<<newNode->processID<<"申请临界段成功."<<endl;
newNode->attitude = 3;
}
else
{
cout<<"进程P"<<newNode->processID<<"申请临界段失败."<<endl;
newNode->attitude = 2;
}
}
else
{
if(full == 1 || Wait(full) > 0)
{
full--;
cout<<"进程P"<<newNode->processID<<"申请临界段成功."<<endl;
newNode->attitude = 3;
}
else
{
cout<<"进程P"<<newNode->processID<<"申请临界段失败."<<endl;
newNode->attitude = 2;
}
}
}
}
void FindWait(ProcessNode &p,int choose)
{
ProcessNode q = p->next;
while(q != NULL)
{
if(q->attitude == 2)
{
if(choose == 1)
{
if(Wait(empty) > 0 || empty == 1)
{
empty--;
cout<<"生产者进程P"<<q->processID<<"获得缓冲区资源,等待mutex..."<<endl;
q->attitude = 3;
}
}
else
{
if(full == 1 || Wait(full) > 0)
{
full--;
cout<<"消费者进程P"<<q->processID<<"获得缓冲区资源,等待mutex..."<<endl;
q->attitude = 3;
}
}
}
q = q->next;
}
}
void FindRun(ProcessNode &p,int choose)
{//将第一个执行进程状态变为完成
ProcessNode q = p->next;
while(q != NULL)
{
if(q->attitude == 1)
{
q->attitude = 4;
if(choose == 1)
full = Signal(full);
else
empty = Signal(empty);
break;
}
q = q->next;
}
}
void FindReady(ProcessNode &p,int choose)
{//将第一个就绪进程变为执行
ProcessNode q = p->next;
while(q != NULL)
{
if(q->attitude == 3)
{
q->attitude = 1;
break;
}
q = q->next;
}
}
void Run(ProcessNode &p,int choose)
{
FindWait(p,choose);
ChangeMutex(p,choose);
FindRun(p,choose);
FindReady(p,choose);
cout<<"full "<<full<<" empty "<<empty<<endl;
cout<<"***************************************************************"<<endl;
}
int main()
{
ProcessNode p = NULL;
ProcessNode c = NULL;
int produceNum = 0;
int consumerNum = 0;
cout<<"请输入生产者进程个数:";
cin>>produceNum;
Build(p,produceNum,1);
cout<<"请输入消费者进程个数:";
cin>>consumerNum;
Build(c,consumerNum,0);
ChangeMutex(p,1);
ChangeMutex(c,0);
int i=0;
while(i != 6)
{
cout<<"1,创建新的生产者进程.2,创建新的消费者进程.3,生产者按优先级执行."<<endl;
cout<<"4,消费者按优先级执行.5,查看生产者与消费者进程的状态.6,退出."<<endl;
cout<<"请选择功能:";
cin>>i;
cout<<"***************************************************************"<<endl;
switch(i)
{
case 1:
{
Insert(p,1);
Display(p);
break;
}
case 2:
{
Insert(c,0);
Display(c);
break;
}
case 3:
{
Run(p,1);
cout<<"***************************************************************"<<endl;
cout<<"生产者各进程运行情况:"<<endl;
Display(p);
break;
}
case 4:
{
Run(c,0);
cout<<"消费者各进程运行情况:"<<endl;
Display(c);
break;
}
case 5:
{
cout<<"***************************************************************"<<endl;
cout<<"生产者各进程运行情况:"<<endl;
Display(p);
cout<<"消费者各进程运行情况:"<<endl;
Display(c);
cout<<"***************************************************************"<<endl;
break;
}
case 6:i = 6;break;
default:break;
}
}
return 0;
}
操作系统-生产者与消费者
需积分: 0 147 浏览量
2008-11-24
16:15:54
上传
评论
收藏 2KB RAR 举报
liuliuxpai
- 粉丝: 1
- 资源: 13
最新资源
- 笔记实验六,spark,大数据分析
- ####蓝桥杯python的详细的信息介绍
- 电子万年历软件仿真(经过多次修改,保证正确性)
- Unity XR 手势射击控制脚本(适用于任何可手势识别的设备)
- 机械设计全自动电表(NB和IC卡表)控制和上壳装配线sw16可编辑非常好的设计图纸100%好用.zip
- 基于matlab的EAN-13条形码识别系统GUI界面.zip代码53
- matlab基于bp神经网络交通信号标志识别GUI界面13个标志.zip代码54
- 电子万年历答辩实物展示视频mp4格式
- 基于python实现的程序,包括哈希感知算法cvHash,图像切割cvsplit,固定目标检测cvRec(附文档ppt)等
- 计算0-10000之间所有偶数的和
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈