IPC--消息队列 message queue --msgctl函数(删除指定msqid的消息队列)

来源:互联网 发布:苏州网络犯罪举报网站 编辑:程序博客网 时间:2024/06/09 22:58
msgctl 系统调用对 msgqid 标识的消息队列执行 cmd 操作,系统定义了 3 种 cmd 操作: IPC_STAT ,  IPC_SET , IPC_RMID ,意义分别如下:
  • IPC_STAT :  该命令用来获取消息队列对应的 msqid_ds 数据结构,并将其保存到 buf  指定的地址空间。
  • IPC_SET :    该命令用来设置消息队列的属性,要设置的属性存储在 buf 中,可设置的属性包括: msg_perm.uid , msg_perm.gid , msg_perm.mode 以及 msg_qbytes .
  • IPC_RMID :  从内核中删除 msqid 标识的消息队列。
当cmd是IPC_RMID时,buf指定为NULL,对msqid对应的消息队列进行删除
当cmd是IPC_SET 时,将buf  set为  msqid对应的消息队列
当cmd是IPC_STAT ,将msqid对应的消息队列 get到buf

1. 删除消息队列
/* * rmmsq.c * *  Created on: 2011-11-14 *      Author: snape */#include <stdio.h>#include <wait.h>#include <stdlib.h>#include <sys/msg.h>#include <unistd.h>#include <errno.h>int main(int argc, char **argv) {int msgid;int pid;if ((msgid = msgget(0x1234, 0666)) < 0) {fprintf(stderr, "open message queue %X error\n", 0x1234);exit(1);}if ((pid = fork()) < 0) {perror("fork");exit(1);} else if (pid == 0) {wait(NULL);printf("============================rm msqid %d success\n", msgid);execlp("ipcs", "ipcs", NULL);exit(1);} else {sleep(2);if (msgctl(msgid, IPC_RMID, NULL) < 0) {fprintf(stderr, "rm msqid %d error\n", msgid);exit(1);} else {execlp("ipcs", "ipcs", NULL);}}return 0;}


原创粉丝点击