初始化消息队列
static int init_msq_master(char *ipcpath, int clean)
{
struct msg_buf msg;
int msqid = -1, ipckey = -1;
int ret = -1;
int msglen = 4096;
if ((stat(ipcpath,&buf))==-1) {
printf("file is not exist\n");
return -1;
}
ipckey = ftok(ipcpath, 1);
if (ipckey < 0) {
printf("ftok for msq error! [err=%d]\n", errno);
return -2;
}
#if 0
msgid = msgget(ftok(ipcpath, 1), 0666);
if (msgid >= 0) {
msgctl(msgid, IPC_RMID, NULL);
}
#endif
msqid = msgget(ipckey, IPC_CREAT|0666);
if (msqid < 0) {
int err = errno;
printf("Create IPC queue error! [err=%d]\n", err);
return -3;
}
if(clean){
int cn = 0;
while (msgrcv(msqid, &msg, msglen, 0, IPC_NOWAIT) > 0) {
printf("msglen=%d, mtype=%ld\n", msglen, msg.mtype);
cn++;
continue;
}
printf("msglen=%d, %d msg has been clean.\n", msglen, cn);
}
printf("msqid = %d\n", msqid);
return msqid;
}