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 rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息