最近在搞进程间通信,首先在我的ubuntu 14.04上写了接口和测试demo,编译和执行都OK,,代码如下:
接口文件ipcmsg.h
/* ipcmsg.h */ #ifndef H_MSGIPC_H #define H_MSGIPC_H
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <ipcmsg.h>
#define MSGKEY 666 #define BUF_SIZE 64 #define APP_PARA_LEN 16 #ifdef __cplusplus extern "C"{ #endif typedef enum ipc_msg_type { APP_MSG, INPUT_MSG, GPS_MSG }ipc_type_t ; typedef struct msgIpc { ipc_type_t type; char buf[BUF_SIZE]; }ipc_msg_t; typedef enum app_cmd { //RECORD CMDS START_RECORD = 1001, STOP_RECORD, GET_RECORD_TIME, SWITCH_CAMERA, TAKE_PICTURE, //FILE PROCESS GET_FILE_LIST = 2001, DELETE_FILE, //SETTINGS SET_VIDEO_RESOLUTION = 3001, GET_VIDEO_RESOLUTION, SET_GSENSOR , GET_GSENSOR , SET_PARKING , GET_PARKING, SET_AUTO_RECORD, GET_AUTO_RECORD, SET_SOUNDINDICATOR, GET_SOUNDINDICATOR,//3010 SET_LOOPING, GET_LOOPING, SET_LANGUAGE, GET_LANGUAGE, SET_WIFI, GET_WIFI, SET_TIME, GET_SD_INFO, FORMAT_SD, FACTORY_RESET,//3020 GET_DEVICE_INFO, GET_ALL_SETTINGS, ADJUST, ADJUST_CONTINUOUS, //NOTIFY NOTIFY = 4001 } app_cmd_t; typedef struct app_data{ app_cmd_t type; int fd; int id ; char code[APP_PARA_LEN]; char value[APP_PARA_LEN]; }app_data_t; //return msqid if success extern int initIpcMsg(key_t msqkey); //remove a msq extern int exitIpcMsg(key_t msqkey); //send ipc msg extern int sendIpcMsg(key_t msqkey, const ipc_msg_t *msg); //recv ipc msg extern int recvIpcMsg(key_t msqidkey, ipc_msg_t *msg); #ifdef __cplusplus } #endif/*end of _cplusplus*/ #endif/*end of H_MSGIPC_H*/
对应的ipcmsg.c如下:
/*ipcmsg.c*/ #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <errno.h> #include "ipcmsg.h" //return msqid if success int initIpcMsg(key_t msqkey) { int msqid = -1; msqid=msgget(msqkey,IPC_EXCL); /*check msq*/ if(msqid < 0){ msqid = msgget(msqkey,IPC_CREAT|0666);/*create msq*/ if(msqid <0){ perror("failed to create msq"); return -1; } } else { printf("msqid %d exist ", msqid); return -2; } return msqid; } //remove a msqid int exitIpcMsg(key_t msqkey) { int msqid = -1; msqid=msgget(msqkey,IPC_EXCL); /*check msq*/ if(msqid <0){ perror("failed to get msq"); return -1; } return msgctl(msqid,IPC_RMID,0); //remove msq } int sendIpcMsg(key_t msgkey, const ipc_msg_t *msg) { char str[256]; int ret = 0, msqid =0; if(NULL == msg) return -1; msqid=msgget(msgkey,IPC_EXCL); /*检查消息队列是否存在*/ if(msqid < 0){ perror("failed to create msq"); return -2; } ret= msgsnd(msqid,msg,sizeof(ipc_msg_t ),IPC_NOWAIT); if ( ret< 0 ) { perror("msgsnd() write msg failed"); } return ret; } /* return byte of msg if success else reurn less than 0 */ int recvIpcMsg(key_t msgkey, ipc_msg_t *msg) { int msgid = 0,ret = 0; char str[512]; //first check parameter if(NULL == msg) return -1; //second check if the msgqueue exit msgid = msgget(msgkey,IPC_EXCL ); if(msgid < 0){ //perror("msq not existed!"); return -2; } //third get msg ret= msgrcv(msgid,msg,sizeof(ipc_msg_t),msg->type,0); if(ret < 0) perror("get msg failed"); return ret; }
接下来测试用的demon分为三个文件,一个send用来发送数据 一个recv用来接收数据 还有一个service(用来启动和结束消息队列)
服务启动端service.c
/*service.c*/ #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <errno.h> #include "ipcmsg.h" #ifdef IPCST int main() { ipc_msg_t msg = {.type = APP_MSG,}; int ipc_msg_type = APP_MSG; app_data_t adat = {.type = 3021, .fd =12, .id =8}; int msqid = 0; msqid=initIpcMsg(MSGKEY); /*init msq*/ if(msqid < 0){ return -1; } printf("start ipc key 0x%x msqid %d ", MSGKEY, msqid); sleep(20); printf("exit ipc key 0x%x msqid %d ", MSGKEY, msqid); return exitIpcMsg(MSGKEY); //remove msq } #endif
发送端send.c
/*send.c*/ #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <errno.h> #include "ipcmsg.h" #include <string.h> #ifdef IPCSND int main() { ipc_msg_t msg = {.type = APP_MSG,}; int ipc_msg_type = APP_MSG; app_data_t adat = {.type = GET_DEVICE_INFO, .fd =12, .id =8,.code="App ", .value="Test"}; int msqid = 0; int i = 0; msqid=msgget(MSGKEY,IPC_EXCL); /*check msq*/ while(msqid < 0){ printf("wait msq key 0x%x ",MSGKEY); sleep(1); msqid=msgget(MSGKEY,IPC_EXCL); /*check msq*/ } while(i ++ < 10) { adat.id = i; memcpy(msg.buf,&adat,sizeof(adat)); sendIpcMsg(MSGKEY,&msg); printf("send %d msg ", i); //sleep(1); } printf("send exit %d ", msqid); return 0; } #endif
接收端recv.c
/*receive.c */ #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <errno.h> #include "ipcmsg.h" #include <string.h> #ifdef IPCRCV int main() { const long type = APP_MSG; app_data_t data = {}; ipc_msg_t msg = {}; while(1) { msg.type = type; if(recvIpcMsg(MSGKEY, &msg) > 0) { memcpy(&data, msg.buf, sizeof(data)); printf("cmd %u fd %d id %d code :%s value: %s ", data.type, data.fd, data.id, data.code, data.value); } else { //printf("recv msg failed key 0x%x ",MSGKEY); sleep(1); } } return 0; } #endif
根据说明将接受函数和发送函数改成如下
ret= msgsnd(msqid,msg,BUF_SIZE,IPC_NOWAIT); ret= msgrcv(msgid,msg,BUF_SIZE,msg->type,0);
改完之后再次编译执行,mmp依然报告同样的错误,说明问题的正因不是这个,但是这确实是个错误,犯了经验注意,一般这种借口习惯性以为就是漆面的那个msg的总大小。以前也是这样用了没报过错误。。。
又继续检索,在下面这个帖子里发现了一条线索
https://bbs.csdn.net/topics/390809159
其中提到的
应该是这个结构体定义的有问题:DMOMsg 设置成员变量udwMsgID的时候,msgsnd发送的消息,需要的那个mttype是个非法值 struct mymsg { long int mtype; /* message type */ char mtext[1]; /* message text */ }; 可以定义个这样的结构体,第一个mtype不要写成负数 原文中这么说的:The structure member mtype is a non-zero positive type long int that can be used by the receiving process for message selection. 看看这个里面的解释:http://pubs.opengroup.org/onlinepubs/007908799/xsh/msgsnd.html
这里面说mtype不要写成负数,但是根据手册说明应该是非零值而不仅是不能是负数
又去仔细看了一下man手册
原文如下:
The mtype field must have a strictly positive integer value. This value can be used by the receiving process for message selec‐
tion (see the description of msgrcv() below).
原话说的是mtype必须是一个严格的正整数,也就是意味着0也是一个非法的。
还有另一个地方也有说明 message type, must be > 0
struct msgbuf { long mtype; /* message type, must be > 0 */ char mtext[1]; /* message data */ };
再回头去看看我初始化的type是APP_MSG,而APP_MSG就一个枚举类型默认第一个,那不就是0
typedef enum ipc_msg_type { APP_MSG, INPUT_MSG, GPS_MSG }ipc_type;
这个APP_MSG默认就是个0。
于是赶紧改一下初始值
typedef enum ipc_msg_type { APP_MSG=0xF0, INPUT_MSG, GPS_MSG }ipc_type;
再次编译执行,果然OK。
查来查去还是man手册才是葵花宝典。
所以以上总共犯了两个经验错误:
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
1、size的参数指的是msg中的buf的大小而非msg的大小——虽然目前这个用错了还没出现严重错误,但还是应该严格根据接口说明来使用。
2、mtype必须是大于0的整数,必须是大于0的整数,必须是大于0的整数,重要的事情说三遍。
事实上type必须大于0,文档中说的是为了用于接受进程的选择。
在man 一下msgrecv
msgrcv() The msgrcv() system call removes a message from the queue specified by msqid and places it in the buffer pointed to by msgp. The argument msgsz specifies the maximum size in bytes for the member mtext of the structure pointed to by the msgp argument. If the message text has length greater than msgsz, then the behavior depends on whether MSG_NOERROR is specified in msgflg. If MSG_NOERROR is specified, then the message text will be truncated (and the truncated part will be lost); if MSG_NOERROR is not specified, then the message isn't removed from the queue and the system call fails returning -1 with errno set to E2BIG. The argument msgtyp specifies the type of message requested as follows: * If msgtyp is 0, then the first message in the queue is read. * If msgtyp is greater than 0, then the first message in the queue of type msgtyp is read, unless MSG_EXCEPT was specified in msgflg, in which case the first message in the queue of type not equal to msgtyp will be read. * If msgtyp is less than 0, then the first message in the queue with the lowest type less than or equal to the absolute value of msgtyp will be read.
接收函数
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
有个参数是msqtyp
如果msqtyp=0 读取队列里的第一条消息
如果msqtyp>0 读取队列里面mtype=msqtyp的第一条消息
如果msqtyp<0 读取队列里面mtype最接近于(等于或小于)msqtyp绝对值的那条消息
所以如果在发送的时候type出现了小于0或者等于0的mtype,那么后面读取的时候msqtyp就没法实现选择。