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

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py