当前位置:网站首页>作业8.6 进程间的通信 消息队列与共享内存
作业8.6 进程间的通信 消息队列与共享内存
2022-08-06 17:26:00 【不知名大学生M】
题目一:要求AB进程做通信
1.A进程发送一句话,B进程接收打印
2.然后B进程发送给A进程一句话,A进程接收打印
3.重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
4.在1,2,3的基础上实现,AB进程能够随时收发数据(附加题)
实现代码
进程A
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
//消息包结构体
struct msgbuf
{
long mtype; //消息类型,必须大于0
char mtext[128]; //消息内容,该成员类型可变
};
void *callBack_send(void *arg)
{
int msqid=*(int*)arg;
//消息包
struct msgbuf snd;
snd.mtype=1; //消息类型
while(1)
{
printf("A进程说>>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]=0;
//以阻塞的方式发送,消息队列满了,阻塞
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
{
perror("msgsnd");
break;
}
if(strcasecmp(snd.mtext, "quit") == 0)
exit(1);
}
pthread_exit(NULL);
}
void *callBack_receive(void *arg)
{
int msqid=*(int*)arg;
//消息包
struct msgbuf rcv;
ssize_t res=0;
while(1)
{
res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0);
if(res<0)
{
perror("msgcrv");
break;
}
if(strcasecmp(rcv.mtext, "quit") == 0)
{
printf("\n");
exit(1);
}
printf("\rA进程收到B进程说:%s\n",rcv.mtext);
printf("A进程说>>>");
fflush(stdout);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//计算key值
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
//创建一个消息队列
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
pthread_t tid1, tid2;
//创建一个线程,发送消息包
if(pthread_create(&tid1, NULL,callBack_send,(void*)&msqid) != 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程,接受消息包
if(pthread_create(&tid2, NULL, callBack_receive, (void*)&msqid) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
进程B
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
//消息包结构体
struct msgbuf
{
long mtype; //消息类型,必须大于0
char mtext[128]; //消息内容,该成员类型可变
};
void *callBack_receive(void *arg)
{
int msqid=*(int*)arg;
//消息包
struct msgbuf rcv;
ssize_t res=0;
while(1)
{
res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0);
if(res<0)
{
perror("msgcrv");
break;
}
if(strcasecmp(rcv.mtext, "quit") == 0)
{
printf("\n");
exit(1);
}
printf("\rB进程收到A进程说:%s\n",rcv.mtext);
printf("B进程说>>>");
fflush(stdout);
}
pthread_exit(NULL);
}
void *callBack_send(void *arg)
{
int msqid=*(int*)arg;
struct msgbuf snd;
snd.mtype=2; //消息类型
while(1)
{
printf("B进程说>>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]=0;
//以阻塞的方式发送,消息队列满了,阻塞
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
{
perror("msgsnd");
break;
}
if(strcasecmp(snd.mtext, "quit") == 0)
exit(1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//计算key值
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
//创建一个消息队列
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
pthread_t tid1, tid2;
//创建一个线程,接受消息包
if(pthread_create(&tid2, NULL, callBack_receive, (void*)&msqid) != 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程,发送消息包
if(pthread_create(&tid1, NULL,callBack_send,(void*)&msqid) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
运行结果

题目二:A进程写入一个整型,在该整型后,写入一个字符串。B进程将共享内存中的整型以及字符串读取出来;
实现代码
进程A
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
int shmid = shmget(key,32,IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmget");
return -1;
}
void *shmaddr=shmat(shmid,NULL,0);
if(*(int*)shmaddr<0)
{
perror("shmat");
return -1;
}
int *a = (int*)shmaddr;
printf("请输入一个整型:");
scanf("%d",a);
getchar();
char *b = (char*)(a+1);
printf("请输入一个字符串:");
fgets(b,128,stdin);
printf("写入成功\n");
return 0;
}
进程B
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
int shmid = shmget(key,32,IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmget");
return -1;
}
void *shmaddr=shmat(shmid,NULL,0);
if(*(int*)shmaddr<0)
{
perror("shmat");
return -1;
}
int *a = (int*)shmaddr;
printf("整型:%d\n",*a);
char *b = (char*)(a+1);
printf("字符串:%s",b);
return 0;
}
运行结果

边栏推荐
猜你喜欢

win7无u盘重置开机密码 win7忘记密码怎么重置电脑密码

ESP8266-Arduino编程实例-磁簧开关传感器驱动

王学岗————从零实现手写音视频通话(H265)

win7后缀名隐藏了怎么打开 win7文件怎么显示后缀名

bonjour是什么软件 bonjour可以卸载吗

2020最新office产品密钥永久激活码_office激活密钥永久key免费(附激活方法)

Win7 wireless network list does not show up Win7 network connection icon disappears what to do

优秀的软件测试工程师是怎样练成的?

电容器选型指南-电子元器件选型指导系列

FP6601AP5 CPC-16L Type-A HVDCP控制器与插入/输出自动检测
随机推荐
win7设置每天自动关机 自动关机怎么设置win7
win7 suffix name is hidden how to open win7 file how to display suffix name
电脑每次开机都要按f1怎么解决 电脑按f1才能启动什么原因
How to format computer to restore factory settings in win7
DMA/DMA2D concept
Win7旗舰版永久激活密钥 win7激活密钥序列号 win7激活神key大全
下一个十年,什么样的测试最吃香?
DMA/DMA2D概念
How to turn on the wireless screen mirroring function in win7
【无标题】
win7强制恢复出厂设置 win7怎么格式化电脑恢复出厂设置
软件测试周刊(第83期):当你感觉忙得没时间休息,就是你最需要找时间休息的时候。
php ini sets session expiration time
Talking about the Jmeter performance test process
How to reset the power-on password of win7 without u disk
FR9608SP SOP-8 28V、3A、340 KHz同步降压DC/DC转换器
Is it necessary to do code review to test the code that students participated in the development of?
2021最新Office激活密钥正版分享
`英语` 2022/8/6
2020最新office产品密钥永久激活码_office激活密钥永久key免费(附激活方法)