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进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon