msqid :消息队列的ID,由msgget()
buf 结构体指针
cmd
IPC_STAT从内核相关结构体中拷贝消息队列相关的信息到buf指向的结构体中
IPC_SET把buf指向的结构体的内容写入到内核相关的结构体中,同时更显msg_ctimer成员,同时以下成员也会被更新:msg_qbytes, msg_perm.uid, msg_perm.gid, msg_perm.mode。调用队列的进程的effective UID必须匹配队列所有者或创建者的msg_perm.uid或msg_perm.cuid或者该进程拥有特权级别,
IPC_RMID立即销毁消息队列,唤醒所有正在等待读取或写入该消息队列进程,调用的进程的UID必须匹配队列所有者或创建者或者该进程拥有足够的特权级别
IPC_INFO (Linux-specific)返回整个系统对与消息队列的限制信息到buf指向的结构体中
//_GNU_SOURCE //<sys/msg.h> struct msginfo { int msgpool;/*Size in kibibytes of buffer pool used to hold message data; unused within kernel*/ int msgmap; /*Maximum number of entries in message map; unused within kernel*/ int msgmax; /*Maximum number of bytes that can be written in a single message*/ int msgmnb; /*Maximum number of bytes that can be written to queue; used to initialize msg_qbytes during queue creation*/ int msgmni; /*Maximum number of message queues*/ int msgssz; /*Message segment size; unused within kernel*/ int msgtql; /*Maximum number of messages on all queues in system; unused within kernel*/ unsigned short int msgseg; /*Maximum number of segments; unused within kernel*/ }; int res=msgctl(msqid,IPC_RMID,NULL); if(-1==res) perror("msgctl"),exit(-1); 例子 //Sys V IPC msg #include<sys/types.h> #include<sys/ipc.h> #include<signal.h> #include<stdio.h> #include<stdlib.h> typedef struct{ long mtype; //消息的类型 char buf[20]; //消息的内容 }Msg; int msqid; //使用全局变量,这样就可以在fa中使用msqid了 void fa(int signo){ printf("deleting..\n"); sleep(3); int res=msgctl(msqid,IPC_RMID,NULL); if(-1==res) perror("msgctl"),exit(-1); exit(0); } int main(){ //ftok() key_t key=ftok(".",150); if(-1==key) perror("ftok"),exit(-1); printf("key%#x\n",key); //msgget() msqid=msgget(key,IPC_CREAT|IPC_EXCL|0664); if(-1==msqid) perror("msgget"),exit(-1); printf("msqid%d\n",msqid); //msgsnd() Msg msg1={1,"hello"};//消息的类型是1,内容是hello Msg msg2={2,"world"}; int res=msgsnd(msqid,&msg2,sizeof(msg2.buf),0); if(-1==res) perror("msgsnd"),exit(-1); res=msgsnd(msqid,&msg1,sizeof(msg1.buf),0); if(-1==res) perror("msgsnd"),exit(-1); //msgctl() //Ctrl+C delete msq printf("Press CTRL+C to delete msq\n"); if(SIG_ERR==signal(SIGINT,fa)) perror("signal"),exit(-1); while(1); return 0; }