消息队列练习题
进程A
/********************************************************************
*
* file name: mesqa.c
* author: [email protected]
* date: 2024/5/28
* function: 接收进程b的信号,读出消息队列中的信息
* note: 应该先执行mesqb.c再执行该程序
*
* CopyRight (c) 2024/5/28 [email protected] All Right Reseverd
*
********************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <stdlib.h>
//管道标识符
int fifo_fd = 0;
//存储信息结构体
struct msgbuf
{
long mtype;
char mtext[128];
};
/*****************************************************************************
* 函数名称: readmsg
* 函数功能: 信号接收函数
* 函数参数: none
* 返回结果: none
* 注意事项: none
* 函数作者: [email protected]
* 创建日期: 2024/5/28
* 修改历史: 2024/5
* 函数版本: 1.0
*
*****************************************************************************/
void readmsg()
{
printf("信息接收,正在处理\n");
struct msgbuf buf;
//创建消息队列
key_t key = ftok(".",22);
int msg_id =msgget(key,IPC_CREAT|0664);
if(-1 == msg_id){
fprintf(stderr,"msg_id获取失败,errno[%d]:%s\n",errno,strerror(errno));
exit(-1);
}
//读出信息
int i = msgrcv(msg_id,&buf,sizeof(buf),0,0);
if(-1 == i){
fprintf(stderr,"msgrcv获取失败,errno[%d]:%s\n",errno,strerror(errno));
exit(-1);
}
printf("i = %d\n",i);
printf("%s\n",buf.mtext);
printf("信息处理完毕\n");
}
int main()
{ //获取当前进程的pid
int b_pid = 0;
int a_pid = getpid();
//打开管道文件
fifo_fd = open("./fifopid",O_RDWR);
if(-1 == fifo_fd){
fprintf(stderr,"管道文件打开失败 errno[%d]:%s\n",errno,strerror(errno));
return -1;
}
//读管道
read(fifo_fd,&b_pid,sizeof(b_pid));
printf("b_pid = %d\n",b_pid);
close(fifo_fd);
//重新打开管道
fifo_fd = open("./fifopid",O_RDWR);
if(-1 == fifo_fd){
fprintf(stderr,"管道文件打开失败 errno[%d]:%s\n",errno,strerror(errno));
return -1;
}
//写入管道
write(fifo_fd,&a_pid,sizeof(a_pid));
printf("a_pid = %d\n",a_pid);
//发送信号
kill(b_pid,SIGUSR1);
printf("信息发送完毕\n");
//接收信号
signal(SIGUSR2,readmsg);
while(1);
close(fifo_fd);
return 0;
}
进程B
/********************************************************************
*
* file name: mesqb.c
* author: [email protected]
* date: 2024/5/28
* function: 接收进程a的信号,向消息队列写入信息
* note: 应该先执行mesqb.c再执行该程序
*
* CopyRight (c) 2024/5/28 [email protected] All Right Reseverd
*
********************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <stdlib.h>
//pid标识符
int a_pid = 0;
int b_pid = 0;
//管道标识符
int fifo_fd = 0;
struct msgbuf
{
long mtype;
char mtext[128];
};
/*****************************************************************************
* 函数名称: wrimsg
* 函数功能: 信号接收函数
* 函数参数: none
* 返回结果: none
* 注意事项: none
* 函数作者: [email protected]
* 创建日期: 2024/5/28
* 修改历史: 2024/5
* 函数版本: 1.0
*
*****************************************************************************/
void wrimsg()
{
printf("信息接收,正在处理\n");
struct msgbuf buf;
sprintf(buf.mtext,"BPID is %d\n",b_pid);
printf("%s\n",buf.mtext);
//打开管道文件
fifo_fd = open("./fifopid",O_RDWR);
if(-1 == fifo_fd){
fprintf(stderr,"管道文件打开失败 errno[%d]:%s\n",errno,strerror(errno));
exit(-1);
}
//读管道
read(fifo_fd,&a_pid,sizeof(a_pid));
printf("a_pid = %d\n",a_pid);
//创建消息队列
key_t key = ftok(".",22);
int msg_id =msgget(key,IPC_CREAT|0664);
if(-1 == msg_id){
fprintf(stderr,"msg_id获取失败,errno[%d]:%s\n",errno,strerror(errno));
exit(-1);
}
//写入信息
msgsnd(msg_id,&buf,strlen(buf.mtext),0);
//发送信号
kill(a_pid,SIGUSR2);
printf("信息发送完毕\n");
close(fifo_fd);
}
int main()
{
b_pid = getpid();
printf("b_pid = %d\n",b_pid);
//创建管道文件
int fifo = mkfifo("./fifopid",0644);
if(-1 == fifo){
fprintf(stderr,"管道文件创建失败 errno[%d]:%s\n",errno,strerror(errno));
return -1;
}
//打开管道文件
fifo_fd = open("./fifopid",O_RDWR);
if(-1 == fifo_fd){
fprintf(stderr,"管道文件打开失败 errno[%d]:%s\n",errno,strerror(errno));
return -1;
}
//写入管道文件
write(fifo_fd,&b_pid,sizeof(b_pid));
printf("b_pid = %d\n",b_pid);
//接收信号
signal(SIGUSR1,wrimsg);
while(1);
close(fifo_fd);
return 0;
}
标签:练习题,队列,errno,pid,fifo,fd,消息,printf,include
From: https://www.cnblogs.com/waibibabu-/p/18217048