Linux下进程之间通过消息队列通信小程序示例
来源:互联网 发布:屏幕检测软件 编辑:程序博客网 时间:2024/05/29 02:10
/*commom.h*/#include <sys/types.h>#include <unistd.h>#include <sys/ipc.h>#include <sys/msg.h>#include <string.h>#include <malloc.h>#define SIZE 512#define MAXSENDSIZE (sizeof(Message)-sizeof(long int))typedef struct {long int type;int val;char buffer[SIZE];}Message;int alloc_msg(Message **msg)/*注意要用双重指针来传递动态内存*/{*msg = (Message *)malloc(sizeof(Message));if (NULL == *msg){perror("alloc_msg failed!\n");exit(1);}memset(*msg, 0, sizeof(Message));return 0;}int free_msg(Message **msg)/*注意要用双重指针来释放动态内存*/{free(*msg);return 0;}int create_msg(key_t key){int msgid;msgid = msgget(key, 0666|IPC_CREAT);if (msgid == -1){perror("create_msg failed!\n");exit(1);}return msgid;}int send_msg(int msgid, Message *msg){if (msgsnd(msgid, (void*)msg, MAXSENDSIZE, 0)==-1){perror("send_msg failed!\n");exit(1);}return 0;}int rev_msg(int msgid, Message *msg,long int type){if (msgrcv(msgid, (void *)msg, MAXSENDSIZE, type, 0)==-1){perror("msg_rev failed!\n");exit(1);}return 0;}int del_msg(int msgid){if (msgctl(msgid, IPC_RMID, NULL)==-1){perror("msg_del failed!\n");exit(1);}return 0;}
/*send.c*/#include <stdio.h>#include <stdlib.h>#include "commom.h"int main(void){int msgid;key_t key;Message *msg = NULL;alloc_msg(&msg);key = ftok("./commom.h", 0);msgid = create_msg(key);msg->type = 1;msg->val = 10;strncpy(msg->buffer, "hello world", SIZE);send_msg(msgid, msg);msg->type = 2;msg->val = 12;strncpy(msg->buffer, "english test", SIZE);send_msg(msgid, msg);free_msg(&msg);return 0;}
/*receive.c*/#include <stdio.h>#include <stdlib.h>#include "commom.h"int main(void){int msgid;key_t key;Message *msg = NULL;alloc_msg(&msg);long int type1 = 1;long int type2 = 2;key = ftok("./commom.h", 0);msgid = create_msg(key);rev_msg(msgid, msg, type1);printf("rev_message is:type = %ld; %s, val = %d\n", msg->type, msg->buffer, msg->val);rev_msg(msgid, msg, type2);printf("rev_message is:type = %ld; %s, val = %d\n", msg->type, msg->buffer, msg->val);del_msg(msgid);free_msg(&msg);return 0;}
#makefileCFLAGS = -Wall -gCC = gccTARGET1 = sendTARGET2 = receiveall:$(TARGET1) $(TARGET2)$(TARGET1):$(TARGET1).o$(CC) $(CFLAGS) -o $@ $^$(TARGET2):$(TARGET2).o$(CC) $(CFLAGS) -o $@ $^%.o:%.c$(CC) -c $(CFLAGS) -o $@ $<clean:rm -f *.o $(TARGET1) $(TARGET2)
先发送后接收
0 0
- Linux下进程之间通过消息队列通信小程序示例
- Linux进程之间通信消息队列
- Linux下进程通信之消息队列
- Linux下进程间通信--消息队列
- linux c之通过消息队列实现进程通信
- linux消息队列进程通信
- linux消息队列进程通信
- linux消息队列进程通信
- linux进程通信---消息队列
- linux进程通信--消息队列
- linux进程通信--消息队列
- 【Linux】 进程通信--消息队列
- linux进程通信---消息队列
- linux进程通信-消息队列
- 进程通信程序整理---消息队列(Linux)
- 消息队列不同进程之间通信
- linux下用消息队列关闭bash-----进程间通信
- Linux下利用消息队列实现进程间通信
- 批量配置MySQL主从同步
- IOS界面UI设计9之常用工具包category、常用第三方框架的使用
- 第14周 项目2-带姓名的成绩单(1)
- 删数问题
- mysql只读模式的设置方法与实验
- Linux下进程之间通过消息队列通信小程序示例
- 第十四周项目三多科成绩单(3)
- Oracle 内存管理
- ORACLE学习之旅——分组取每组数据中最近的一条记录
- 深入Cocos2d-x】使用MVC架构搭建游戏Four
- 深入研究Clang(六) Clang Lexer代码阅读笔记之Preprocesser
- Java初学习 - Java中的异常[Exception]public class NeverCaught { static void f() throws ExceptionB{
- 使用pt-heartbeat监控主从复制延迟
- 从一家电子商务网站学到的经验教训