首页 > 代码库 > Linux消息队列实践(3)
Linux消息队列实践(3)
API综合使用
//父进程发送消息,子进程接收消息 struct msgBuf { long mtype; /* message type, must be > 0 */ char mtext[104]; /* message data */ }; const int MSGNUMBER = 10; int main() { //获取一个键 key_t msgKey = ftok("/tmp/mySeedFile",‘f‘); //获取一个消息队列 int msgid = msgget(msgKey,IPC_CREAT|0666); if (msgid == -1) { err_exit("msgget error"); } struct msgBuf myBuffer; pid_t pid = fork(); if (pid == -1) { err_exit("fork error"); } //父进程,发送数据 if (pid > 0) { myBuffer.mtype = getpid(); //向消息队列中发送消息,如果队列full,则该进程一直阻塞 for (int i = 0; i < MSGNUMBER; ++i) { sprintf(myBuffer.mtext,"Hello, My Number is %d",i); msgsnd(msgid,&myBuffer,strlen(myBuffer.mtext),0); } //等待子进程结束 wait(NULL); } else if (pid == 0) //子进程 { //睡眠1秒,等待父进程发送结束 sleep(1); memset(&myBuffer,0,sizeof(myBuffer)); //从队首不断的取数据 for (int i = 0; i < MSGNUMBER; ++i) { int recvBytes = 0; if ((recvBytes = msgrcv(msgid,&myBuffer,sizeof(myBuffer.mtext), getppid(),IPC_NOWAIT)) == -1) { err_exit("msgrcv error"); } else { cout << "recvBytes = " << recvBytes << endl; cout << "myBuffer.mtype = " << myBuffer.mtype << endl; cout << "\t" << myBuffer.mtext << endl; } } cout << "strlen(myBuffer.mtext) = " << strlen(myBuffer.mtext) << endl; } return 0; }
消息队列项目开发案例
消息队列实现回射客户/服务器
/**一个简化实现->程序说明: 1.客户端发送数据格式: 类型为:客户端pid 内容为键盘输入的内容 2.客户端接收格式 类型为自己的pid 内容为服务器发送过来的内容 3.服务器接收的数据 类型为客户端pid 内容为各个客户端的消息内容 4.服务器发送的数据 类型为客户端id 内容为客户端消息内容 */
//server.cpp #include "commen.h" void echo_server(int msgid) { struct msgBuf myMsgBuf; while (true) { memset(&myMsgBuf,0,sizeof(myMsgBuf)); int recvBytes = msgrcv(msgid,&myMsgBuf,MAXMSGSIZE,0,0); if (recvBytes == -1) { err_exit("msgrcv error"); } fputs(myMsgBuf.mtext,stdout); if (msgsnd(msgid,&myMsgBuf,strlen(myMsgBuf.mtext),0) < 0) { err_exit("msgsnd error"); } } } int main() { key_t key = ftok(FILESEED,‘f‘); int msgid = msgget(key,0666|IPC_CREAT); if (msgid == -1) { err_exit("msgget error"); } echo_server(msgid); return 0; }
//client.cpp #include "commen.h" void echo_client(int msgid) { struct msgBuf myMsgBuf,recvMsgBuf; myMsgBuf.mtype = getpid(); while (fgets(myMsgBuf.mtext,MAXMSGSIZE,stdin) != NULL) { if (msgsnd(msgid,&myMsgBuf,strlen(myMsgBuf.mtext),0) == -1) { err_exit("msgsnd error"); } memset(&recvMsgBuf,0,sizeof(recvMsgBuf)); if (msgrcv(msgid,&recvMsgBuf,sizeof(recvMsgBuf),getpid(),0) == -1) { err_exit("msgrcv error"); } fputs(recvMsgBuf.mtext,stdout); memset(&myMsgBuf+4,0,sizeof(myMsgBuf)-4); } } int main() { key_t key = ftok(FILESEED,‘f‘); int msgid = msgget(key,0666); if (msgid == -1) { err_exit("msgget error"); } echo_client(msgid); return 0; }
//commen.h #ifndef COMMEN_H_INCLUDED #define COMMEN_H_INCLUDED #include <string> #include <string.h> #include <errno.h> #include <stdlib.h> #include <stdio.h> const char FILESEED[] = "/tmp/mySeedFile"; const int MAXMSGSIZE = 1024; struct msgBuf { long mtype; //Message Type: Client PID char mtext[MAXMSGSIZE]; //message data }; void err_exit(std::string str) { perror(str.c_str()); exit(EXIT_FAILURE); } #endif // COMMEN_H_INCLUDED
开拓眼界
附-Makefile
CC = g++ CPPFLAGS = -Wall -g BIN = client server SOURCES = $(BIN.=.cpp) .PHONY: clean all all: $(BIN) $(BIN): $(SOURCES) clean: -rm -rf $(BIN) bin/ obj/ core
Linux消息队列实践(3)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。