C语言实现进程通信模拟
- 格式:doc
- 大小:364.50 KB
- 文档页数:15
msg *msg_que_a=(msg*)malloc(sizeof(msg));
msg_que_a->next_msg = NULL;
msg * msg_que_b=(msg*)malloc(sizeof(msg));
msg_que_b->next_msg = NULL;
while(1)
void print_menu()
{
printf("按1键生产者生产一个产品\n");
printf("按2键消费者消费一个产品\n");
printf("按3键显示缓冲区信息\n");
printf("按4键退出\n");
printf("您的选择:\n");
}
bool IsEmpty()
{
bool flag = false;
{
print_menu();
scanf("%d", &flag);
switch(flag)
{
case 1:
Send_Msg(msg_que_b);
break;
case 2:
Send_Msg(msg_que_a);
break;
case 3:
Print_Msg_Que(msg_que_a);
break;
【题目二】模拟通过消息队列通信。假设有两个进程A和B,A和B分别有一个本进程接收到的消息队列,实现一个C语言程序,用链式存储实现A和B相互发送消息,A向B发送消息即将新消息插入到B消息队列的尾部,B接收消息即从消息队列的对头取出一个消息打印在屏幕上(消息队列为空的话显示空),A或B也可以一次性把所有发送给自己的消息接收(显示出来)。每个消息内容需要由用户手动输入一段文字。可以根据以下程序完成,打印的菜单中需要加入个人学号姓名等信息。
{
int flag = 1;
msg *tempmsg = que->next_msg;
msg *nexttempmsg;
if(tempmsg == NULL)
{
printf("该消息队列为空!\n");
return ;
}
while(tempmsg != NULL)
{
nexttempmsg = tempmsg->next_msg;
{
if(IsFull())
{
printf("仓库满了,不能再生产,自动回到菜单!\n\n");
return ;
}
buffer[in_point] = product++;
in_point = (in_point + 1) % 5;
count++;
}
void Consume()
{
if(IsEmpty())
int flag;
while(1)
{
print_menu();
scanf("%d",&flag);
switch(flag)
{
case 1:
Product();
break;
case 2:
Consume();
break;
case 3:
PrintAll();
break;
case 4:
return 0;
break;
strcpy(tempmsg->data, message);
msg *flag = que;
while(flag->next_msg != NULL)
{
flag = flag->next_msg;
}
flag->next_msg = tempmsg;
tempmsg->next_msg = NULL;
case 4:
Print_Msg_Que(msg_que_b);
break;
case 5:
Receive_Msg(msg_que_a);
break;
case 6:
Receive_Msg(msg_que_b);
break;
case 7:
Receive_All_Msg(msg_que_a);
break;
{
if(que1->next_msg == NULL)
{
printf("A消息队列为空!\n");
}
else
{
printf("A消息队列信息如下:\n");
Print_Msg_Que(que1);
}
if(que2->next_msg == NULL)
{
printf("B消息队列为空!\n");
}
else
printf("按7键A接收所有消息\n");
printf("按8键B接收所有消息\n");
printf("按9键显示A和B的消息队列\n");
printf("按0键退出\n");
printf("您的选择:\n\n");
}
void Print_All_Msg_que(msg *que1,msg *que2)
{
printf("B消息队列信息如下:\n");
Print_Msg_Que(que2);
}
if(que1->next_msg != NULL||que2->next_msg != NULL)
{
printf("A和B的消息队列打印完成!\n\n");
}
}
int main()
{
printf("版权信息:2018015334,数据18-2班,李虎");
}
void Receive_Msg(msg* que)
{
msg *tempmsg;
if(que->next_msg == NULL)
{
printf("该消息队列为空!\n");
return ;
}
tempmsg = que->next_msg;
printf("接收到的消息为:%s\n", tempmsg->data);
printf("接收第%d条消息为:%s\n",
flag++, tempmsg->data);
free(tempmsg);
tempmsg = nexttempmsg;
}
que->next_msg = tempmsg;
printf("接受全部消息完成!\n\n");
}
void print_menu()
{
printf("\n按1键A进程给B进程发送消息\n");
printf("按2键B进程给A进程发送消息\n");
printf("按3键显示A的消息队列\n");
printf("按4键显示B的消息队列\n");
printf("按5键A接收一个消息\n");
printf("按6键B接收一个消息\n");
case 8:
Receive_All_Msg(msg_que_b);
break;
case 9:
Print_All_Msg_que(msg_que_a,msg_que_b);
break;
case 0:
return 0;
break;
default:
printf("请输入菜单数字!");
break;
}
}
return 0;
default:
break;
}
}
return 0;
}
题目二:
源代码:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX_MSG_LEN 1024
typedef struct msg
{
char data[MAX_MSG_LEN];
que->next_msg = tempmsg->next_msg;
free(tempmsg);
}
void Print_Msg_Que(msg *que)
{
int flag = 1;
msg *tempmsg = que->next_msg;
if(tempmsg == NULL)
{
printf("该消息队列为空!\n");
报告正文(运行屏幕截图及源代码)
题目一:
源代码:
#include<stdio.h>
#define LEN 5
static int in_point = 0;
static int out_point = 0;
static int buffer[LEN];
static int count = 0;
static int product = 1;
if(out_point == in_point)
flag = true;
return flag;
}
bool IsFull()
{
bool flag = false;
if((in_point + 1) % 5 == out_point)