linux 进程间通信--systemV 消息队列 实例

2024-02-12 04:18

本文主要是介绍linux 进程间通信--systemV 消息队列 实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1:消息接收端

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include <sys/wait.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include <errno.h>
#include <sys/stat.h>
#define MAX_TEXT 512
struct my_msg_st
{int my_msg_type;char msg_text[MAX_TEXT];
};
#define MSG_FILE "/wlan/configap/mkwrielessconfig.sh"
#define MSG_FLAG IPC_CREAT|S_IRUSR|S_IWUSR
int msgid=0;
void signalHandler(int sig)
{
#if 0switch (sig){case SIGINT:case SIGTERM:if (msgctl(msgid, IPC_RMID, NULL) == -1){perror("msgget() failed to delete old queue");}else{printf("remove msgid:%d\n",msgid);}}
#endifprintf("main process exit\n");exit(0);
}
int checkSelect(int handfd,char *rwflag)
{int ret, retval=0xff;fd_set readfds, writefds;struct timeval	timeout;FD_ZERO(&readfds);FD_ZERO(&writefds);static int numfds = 0;if (handfd >= numfds){numfds = handfd + 1;              /* Record maximum fd + 1 */}if (strchr(rwflag, 'r') != NULL){FD_SET(handfd, &readfds);}if (strchr(rwflag, 'w') != NULL){FD_SET(handfd, &writefds);}while (1){/*设置阻塞时间为5秒*/timeout.tv_sec = 0;timeout.tv_usec = 20;retval =0xff;ret = select(numfds, &readfds, &writefds, NULL, &timeout);switch (ret){case 0://exit(0);break;case -1:perror("select");exit(1);break;default:if (FD_ISSET(handfd, &readfds)){retval =0;FD_CLR(handfd, &readfds);FD_SET(handfd, &readfds);}if(FD_ISSET(handfd, &writefds)){retval =1;FD_CLR(handfd, &writefds);FD_SET(handfd, &writefds);}break;}// printf("time out1--------------\n");return retval;}
}int main(int argc,char *argv[])
{int running=1;int childPid;struct my_msg_st some_data;int fd=0;char buffer[BUFSIZ];int msg_to_receive=2;int stat_val;key_t key;if((key=ftok(MSG_FILE,'a'))==-1){fprintf(stderr,"Creat Key Error:%s\n\n",strerror(errno));exit(1);}if((msgid=msgget(key,MSG_FLAG)) == -1){perror("msgget");exit(0);}
#if 0childPid=fork();if( childPid < 0 ){exit(0);}else if( childPid == 0 ){while(running){if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1){perror("msgrcv");exit(EXIT_FAILURE);}printf("\nreceiver mssage:%s",some_data.msg_text);if(strncmp(some_data.msg_text,"end",3)==0){running=0;kill(getppid(),SIGINT);}}}else{#if 1struct sigaction sa;sa.sa_handler = signalHandler;sa.sa_flags = 0;	/* Interrupt system calls */sigemptyset(&sa.sa_mask);sigaction(SIGINT, &sa, NULL);sigaction(SIGQUIT, &sa, NULL);#endifwhile(running){printf("Enter the mssage to send:");fgets(buffer,BUFSIZ,stdin);some_data.my_msg_type=1;strcpy(some_data.msg_text,buffer);if((msgsnd(msgid,(void *)&some_data,MAX_TEXT,0))==-1){perror("msgsnd");exit(EXIT_FAILURE);}if(strncmp(buffer,"end",3)==0){running=0;printf("kill child pid:%d getpid:%d\n",childPid,getpid());kill(childPid,SIGINT);}}}printf("pid:%d getpid:%d\n",childPid,getpid());waitpid(childPid, &stat_val, 0);if (WIFEXITED(stat_val)){printf("Child exited with code %d\n", WEXITSTATUS(stat_val));}else if (WIFSIGNALED(stat_val)){printf("Child terminated abnormally, signal %d\n", WTERMSIG(stat_val));}
#endif#if 1while(1){if(checkSelect(fd,"r")==0){printf("Enter the mssage to send:");fgets(buffer,BUFSIZ,stdin);some_data.my_msg_type=1;strcpy(some_data.msg_text,buffer);if((msgsnd(msgid,(void *)&some_data,MAX_TEXT,0))==-1){perror("msgsnd");exit(EXIT_FAILURE);}if(strncmp(buffer,"end",3)==0){running=0;}}if(running){if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,IPC_NOWAIT)==-1){}else{printf("\nreceiver mssage:%s",some_data.msg_text);if(strncmp(some_data.msg_text,"end",3)==0){running=0;}}}if(running==0){if(msgctl(msgid,IPC_RMID,0)==-1){if(EINVAL == errno){msgctl(msgid,IPC_RMID,0);exit(EXIT_FAILURE);}}else{printf("remove msgid:%d\n",msgid);}exit(0);}}
#endifreturn 0;
}


2:消息发送端

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include <sys/wait.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include <errno.h>
#include <sys/stat.h>
#define MSG_FILE "/wlan/configap/mkwrielessconfig.sh"
#define MSG_FLAG IPC_CREAT|S_IRUSR|S_IWUSR
int msgid=0;
#define MAX_TEXT 512
struct my_msg_st
{int my_msg_type;char msg_text[MAX_TEXT];
};
void signalHandler(int sig)
{
#if 0printf("-----------------------\n");if(SIGINT == sig){if (msgctl(msgid, IPC_RMID, NULL) == -1){perror("msgget() failed to delete old queue");}else{printf("remove msgid:%d\n",msgid);}}
#endifprintf("main process exit\n");exit(0);
}
int main(int argc,char *argv[])
{int running=1;int childPid;struct my_msg_st some_data;char buffer[BUFSIZ];int msg_to_receive=1;int stat_val;key_t key;if((key=ftok(MSG_FILE,'a'))==-1){fprintf(stderr,"Creat Key Error:%s\n\n",strerror(errno));exit(1);}if((msgid=msgget(key,MSG_FLAG)) == -1){perror("msgget");exit(0);}childPid=fork();if( childPid < 0 ){exit(0);}else if( childPid == 0 ){while(running){if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1){perror("msgrcv");exit(EXIT_FAILURE);}printf("\nreceiver mssage:%s",some_data.msg_text);if(strncmp(some_data.msg_text,"end",3)==0){running=0;kill(getppid(),SIGINT);}}}else{#if 1struct sigaction sa;sa.sa_handler = signalHandler;sa.sa_flags = 0;	/* Interrupt system calls */sigemptyset(&sa.sa_mask);sigaction(SIGINT, &sa, NULL);sigaction(SIGQUIT, &sa, NULL);#endifwhile(running){printf("Enter the mssage to send:");fgets(buffer,BUFSIZ,stdin);some_data.my_msg_type=2;strcpy(some_data.msg_text,buffer);if((msgsnd(msgid,(void *)&some_data,MAX_TEXT,0))==-1){perror("msgsnd");exit(EXIT_FAILURE);}if(strncmp(buffer,"end",3)==0){running=0;printf("kill child pid:%d getpid:%d\n",childPid,getpid());kill(childPid,SIGINT);}}}printf("pid:%d getpid:%d\n",childPid,getpid());waitpid(childPid, &stat_val, 0);if (WIFEXITED(stat_val)){printf("Child exited with code %d\n", WEXITSTATUS(stat_val));}else if (WIFSIGNALED(stat_val)){printf("Child terminated abnormally, signal %d\n", WTERMSIG(stat_val));}if(msgctl(msgid,IPC_RMID,0)==-1){if(EINVAL == errno){msgctl(msgid,IPC_RMID,0);exit(EXIT_FAILURE);}}else{printf("remove msgid:%d\n",msgid);}return 0;
}

3:执行结果

消息查询

发送端及接收端结果




这篇关于linux 进程间通信--systemV 消息队列 实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/701646

相关文章

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

Linux环境变量&&进程地址空间详解

《Linux环境变量&&进程地址空间详解》本文介绍了Linux环境变量、命令行参数、进程地址空间以及Linux内核进程调度队列的相关知识,环境变量是系统运行环境的参数,命令行参数用于传递给程序的参数,... 目录一、初步认识环境变量1.1常见的环境变量1.2环境变量的基本概念二、命令行参数2.1通过命令编程

Linux之进程状态&&进程优先级详解

《Linux之进程状态&&进程优先级详解》文章介绍了操作系统中进程的状态,包括运行状态、阻塞状态和挂起状态,并详细解释了Linux下进程的具体状态及其管理,此外,文章还讨论了进程的优先级、查看和修改进... 目录一、操作系统的进程状态1.1运行状态1.2阻塞状态1.3挂起二、linux下具体的状态三、进程的