#include
#include
#include
#include
#include
#include
#include
#include
#define QUEUE1 100
#define QUEUE2 101
/*
- redefine struct msg_buf */ struct msgmbuf{ //msg type long mtype; //the field used to store URL char mtext[256]; };
int main()
{
/*
* create mesage queue!!!
*/
key_t key;
//create msg_queue
int msg_flags;
//msg is
int msg_id1;
msg_flags = IPC_CREAT|IPC_EXCL;
msg_id1 = msgget((key_t)1111, msg_flags|0x0666);
if(-1 == msg_id1){
printf("create msg_queue failed");
return -1;
}
/*
* send mesage
*/
int msg_sflags;
struct msgmbuf msg_mbuf;
msg_sflags = IPC_NOWAIT;
msg_mbuf.mtype = QUEUE1;
char *url1 = "www.baidu.com";
strcpy(msg_mbuf.mtext, url1);
printf("%s\n", msg_mbuf.mtext);
printf("%ld\n", msg_mbuf.mtype);
int ret = msgsnd(msg_id1, &msg_mbuf, 256, msg_sflags);
if(-1 == ret){
printf("send msg failed,errno = %d,%s\n",errno,strerrno(errno));
msgctl(msg_id1, IPC_RMID, NULL);
exit(1);
}
/*
* receive mesage
*/
int msg_rflags;
msg_rflags = IPC_NOWAIT|MSG_NOERROR;
struct msgmbuf msg_rev;
ret = msgrcv(msg_id1, &msg_rev, sizeof(struct msgmbuf), QUEUE1, msg_rflags);
if(-1 == ret){
printf("receive msg failed\n");
msgctl(msg_id1, IPC_RMID, NULL);
return -1;
}else{
printf("mtext: %s\n", msg_rev.mtext);
printf("mtype: %ld\n", msg_rev.mtype);
}
msgctl(msg_id1, IPC_RMID, NULL);
return 0;
}