您好,欢迎访问三七文档
消息缓冲通信进程一、实习内容消息缓冲通信过程(发送与接收)要求:(1)发送进程A创建消息;(2)执行send(B,a)后,查看进程B的进程控制块(PCB)表中mq,mutex,sm的情况,查看消息缓冲区中内容。二、实习目的模拟实现进程间通信过程,理解进程通信的基本原理与所用数据结构,消息缓冲区、信号量及消息队列的有关算法。三、实验数据结构及符号说明1、进程控制块(PCB)typedefstructPCB{stringname;semaphoremutex;semaphoresm;message*mq;PCB(stringn=){name=n;mutex.value=1;sm.value=1;mq=NULL;}}PCB;2、消息缓冲区typedefstructmessage{stringsender;intsize;stringtext;message*next;message(){sender=;size=-1;text=;next=NULL;}}message;message*msg=new(message);message*back=msg;3、信号量typedefstruct{intvalue;//PCB*list;}semaphore;四、程序源代码#includeiostream#includequeue#includestringusingnamespacestd;typedefstructmessage//消息缓冲区声明{stringsender;//消息发送者intsize;//消息长度stringtext;//消息正文message*next;//指向下一个消息缓冲区message(){sender=;size=-1;text=;next=NULL;}}message;message*msg=new(message);//消息缓冲区message*back=msg;typedefstruct//信号量{intvalue;//PCB*list;}semaphore;typedefstructPCB//进程控制块{stringname;//进程名semaphoremutex;//互斥信号量semaphoresm;//资源信号量message*mq;//消息队列首指针PCB(stringn=){name=n;mutex.value=1;sm.value=1;mq=NULL;}}PCB;voidP(semaphore&s){s.value--;if(s.value0){cout进程阻塞endl;}}voidV(semaphore&s){s.value++;if(s.value=0){cout唤醒进程endl;}}voidshowProcess(PCB&B)//查看消息队列{cout进程B.name信息endl;if(B.mq==NULL){cout消息队列为空endl;}else{message*q=B.mq;while(q!=NULL){cout消息发送者:q-sender消息长度:q-size消息正文:q-textendl;q=q-next;}}cout互斥信号量mutex:B.mutex.valueendl;cout资源信号量sm:B.sm.valueendl;coutendl;return;}voidshowBuffer()//查看缓冲区{if(msg-next==NULL){cout\n当前缓冲区为空\nendl;}else{cout\n当前消息缓冲区内容:endl;message*q=msg-next;intcount=0;while(q!=NULL){cout++count.消息发送者:q-sender消息长度:q-size消息正文:q-textendl;coutendl;q=q-next;}}}voidsend(PCB&A,message*a)//消息发送原语{message*q=new(message);q-size=a-size;q-sender=a-sender;q-text=a-text;back-next=q;back=q;q-next=NULL;showBuffer();cout进程A.name互斥信号量执行P操作endl;P(A.mutex);if(A.mq==NULL){cout消息缓冲区插入进程A.name消息队列endl;A.mq=q;}else{cout消息缓冲区插入进程A.name消息队列endl;message*p=A.mq;while(p-next!=NULL)p=p-next;p-next=msg-next;}msg-next=NULL;cout缓冲区清空...endl;cout进程A.name互斥信号量执行V操作endl;V(A.mutex);cout进程A.name资源信号量执行V操作endl;V(A.sm);coutsend()发送原语结束endl;coutendl;}voidreceive(PCB&B,message*b)//消息接收原语{cout进程B.name互斥信号量执行P操作endl;P(B.mutex);cout进程B.name资源信号量执行P操作endl;P(B.sm);message*p=new(message);if(B.mq==NULL){cout进程B.name消息队列为空endl;return;}else{cout消息移出队列并删除endl;p-sender=B.mq-sender;//将消息移出消息队列p-size=B.mq-size;p-text=B.mq-text;message*q=B.mq-next;//删除消息队列中接收的信息delete(B.mq);B.mq=q;}cout进程B.name互斥信号量执行V操作endl;V(B.mutex);cout进程B.name消息送入接收区bendl;b-sender=p-sender;b-sender=p-size;b-sender=p-text;}voidcreateMessage(constPCB&A,message*m)//创建消息{m-next=NULL;m-sender=A.name;cout进程A.name创建消息endl;cout输入消息长度与正文:;cinm-sizem-text;//进入消息缓冲区cout消息进入缓冲区...endl;}intmain(){PCBA(A);//进程APCBB(B);//进程Bmessage*a=new(message);//消息发送区createMessage(A,a);send(B,a);showProcess(B);showBuffer();message*b=new(message);//消息接收区receive(B,b);return0;}五、运行程序、输出及实验截图1、初始化,创建进程A、BPCBA(A);PCBB(B);2、进程A创建消息message*a=new(message);createMessage(A,a);3、发送消息到进程B并查看PCB和消息缓冲区send(B,a);showProcess(B);showBuffer();4、进程B接收消息message*b=new(message);receive(B,b);5、实验截图
本文标题:消息缓冲通信进程
链接地址:https://www.777doc.com/doc-2024892 .html