}//消息发送后输出消息队列的属性showmsgattr(qid, msgattr);//设置消息队列的属性msgattr.msg_perm.uid =33; msgctl(qid, IPC_SET,&msgattr);//设置属性后输出消息队列的属性showmsgattr(qid, msgattr);//删除后再输出消息队列的属性msgctl(qid, IPC_RMID, NULL); showmsgattr(qid, msgattr);return0;...
pid_tmsg_lspid; /* PID of last msgsnd(2) */ pid_t msg_lrpid; /* PID of last msgrcv(2) */ }; 可以看到第一个条目就是IPC结构体,即是共有的,后面的都是消息队列所私有的成员。 四、消息队列在内核中的表示 消息队列是用链表实现的,这里需要提出的是MSGMAX指的是一条消息的纯数据大小的上限,...
1.删除消息队列 ret = msgctl(msg_id , IPC_RMID,NULL); //不保存消息队列的属性用NULL 2.保存消息队列 struct msqid_ds tmp; ret = msgctl(msg_id , IPC_STAT,&tmp);//保存消息队列的属性 3.设置结构体属性 struct msqid_ds tmp; ret = msgctl(msg_id , IPC_SET,&tmp);//设置消息队列的属性(...
16、flag = msgrcv( msqid, &buf1, recvlength ,3,0 ) ; if ( flag 0 ) perror(recv message error) ; return -1 ; printf(type=%d, message=%sn, buf1.mtype, buf1.mtext) ; flag = msgctl( msqid, IPC_RMID,NULL) ; if ( flag 0 ) perror(rm message queue error) ; return -1 ; ...
types.h>#include<unistd.h>#include<errno.h>#defineERR_EXIT(m)\do{\perror(m);\exit(EXIT_FAILURE);\}while(0)intmain(void){intmsgid;msgid=msgget(1234,0);if(msgid==-1)ERR_EXIT("msgget");printf("msgget success\n");printf("msgid=%d\n",msgid);msgctl(msgid,IPC_RMID,NULL);return0;...
消息队列函数(msgget、msgctl、msgsnd、msgrcv)及其范例
#include <sys/ipc.h> #include <sys/msg.h> 函数说明 得到消息队列标识符或创建一个消息队列对象并返回消息队列标识符 函数原型 intmsgget(key_tkey,intmsgflg)函数传入值 key 0(IPC_PRIVATE):会建立新的消息队列 大于0的32位整数:视参数msgflg来确定操作。通常要求此值来源于ftok返回的IPC键值 msgflg 0...
被动端,进行消息队列的创建和销毁msqid = msgget( key, 0600|IPC_CREAT ) ;//创建flag =msgctl( msqid, IPC_RMID,NULL) ;//销毁主动端,无需创建和销毁。2、命令删除消息队列我们在做测试时,被动端是while死循环,不断的接收消息,while外才会销毁消息,(正常情...
The IPC_RMID command can be run only by a thread with appropriate privileges or one that has an effective user ID equal to the user ID of the owner or the user ID of the creator of the message queue. The structure pointed to by *buf is ignored and can be NULL. IPC_SET (0x00000001...
msqid = msgget( IPC_PRIVATE, 0666 ) ;if ( msqid < 0 ){perror("get ipc_id error") ;return -1 ;}buf.mtype = 1 ;strcpy(buf.mtext, "happy new year!") ;sendlength = sizeof(struct msgbuf) - sizeof(long) ;flag = msgsnd( msqid, &buf, sendlength , 0 ) ;...