1614010102曹妍消息缓冲通讯机制

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

课程名称:操作系统考核日期:2018年6月21日

题目:消息缓冲通信机制

一、要求简述

(1)发送进程A创建消息;

(2)执行send(B,a)后,查看进程B的进程控制块(PCB)表中mq,mutex,sm的情况,查看消息缓冲区中内容。

二、主要代码

cout << "互斥信号量mutex:" << B.mutex.value << endl;

cout << "资源信号量sm:" << B.sm.value << endl;

cout << endl;

return;

}

void showBuffer() //查看缓冲区

{

if(msg->next == NULL)

{

cout << "\n当前缓冲区为空\n" << endl;

}

else

{

cout << "\n当前消息缓冲区内容:" << endl;

message *q = msg->next;

int count = 0;

while(q != NULL)

{

cout << ++count << ".消息发送者:" << q->sender << " 消息长度:" << q->size << " 消息正文:" << q->text << endl;

cout << endl;

q = q->next;

}

}

}

void send(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 << "进程" << << "互斥信号量执行P操作" << endl;

P(A.mutex);

if(A.mq == NULL)

{

cout << "消息缓冲区插入进程" << << "消息队列" << endl;

A.mq = q;

}

else

{

cout << "消息缓冲区插入进程" << << "消息队列" << endl;

message *p = A.mq;

while(p->next != NULL)

p = p->next;

p->next = msg->next;

}

msg->next = NULL;

cout << "缓冲区清空..." << endl;

cout << "进程" << << "互斥信号量执行V操作" << endl;

V(A.mutex);

cout << "进程" << << "资源信号量执行V操作" << endl;

V(A.sm);

cout << "send()发送原语结束" << endl;

cout << endl;

}

void receive(PCB &B,message *b) //消息接收原语

{

cout << "进程" << << "互斥信号量执行P操作" << endl;

P(B.mutex);

cout << "进程" << << "资源信号量执行P操作" << endl;

P(B.sm);

message *p = new(message);

if(B.mq == NULL)

{

cout << "进程" << << "消息队列为空" << 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 << "进程" << << "互斥信号量执行V操作" << endl;

V(B.mutex);

cout << "进程" << << "消息送入接收区b" << endl;

b->sender = p->sender;

b->sender = p->size;

b->sender = p->text;

}

void createMessage(const PCB &A,message *m) //创建消息

{

m->next = NULL;

m->sender = ;

cout << "进程" << << "创建消息" << endl;

cout << "输入消息长度与正文:";

cin >> m->size >> m->text; //进入消息缓冲区

cout << "消息进入缓冲区..." << endl; }

int main()

{

PCB A("A"); //进程A

PCB B("B"); //进程B

message *a = new(message); //消息发送区

createMessage(A,a);

send(B,a);

showProcess(B);

showBuffer();

message *b = new(message); //消息接收区

receive(B,b);

return 0;

}

三、测试结果

相关文档
最新文档