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

来源:互联网 发布:光束灯编程 编辑:程序博客网 时间:2024/04/29 09:14

2. msgctl(msqid,IPC_STAT,&buf); get消息队列的信息


/* * print_msq.c * *  Created on: 2011-11-15 *      Author: snape */#include <stdio.h>#include <fcntl.h>#include <i386-linux-gnu/sys/types.h>#include <i386-linux-gnu/sys/msg.h>#include <string.h>#include <errno.h>#include <stdlib.h>void print_msq(int msqid);char * getMode(mode_t, char *);int main(int argc, char **argv) {int msqid;if ((msqid = msgget(0x1234, 0666 | IPC_CREAT)) < 0) {perror("msgget");exit(1);}print_msq(msqid);return 0;}//get 指定msqid的消息队列得信息 并打印void print_msq(int msqid) {struct msqid_ds buf;char s[20];memset(&buf, 0, sizeof(buf));if (msgctl(msqid, IPC_STAT, &buf) < 0) {perror("msgctl IPC_STAT");return;} else {getMode(buf.msg_perm.mode, s);fprintf(stderr, "%10d %10x%10o%10s\n", buf.msg_perm.uid,buf.msg_perm.gid, buf.msg_perm.mode, s);printf("hello %d\n", msqid);}}char * getMode(mode_t mode, char *buf) {if (buf == NULL) {fprintf(stderr, "buf can't be NULL\n");return NULL;}memset(buf, '-', 9);if (mode & S_IRUSR)buf[0] = 'r';if (mode & S_IWUSR)buf[1] = 'w';if (mode & S_IXUSR)buf[2] = 'x';if (mode & S_IRGRP)buf[3] = 'r';if (mode & S_IWGRP)buf[4] = 'w';if (mode & S_IXGRP)buf[5] = 'x';if (mode & S_IROTH)buf[6] = 'r';if (mode & S_IWOTH)buf[7] = 'w';if (mode & S_IXOTH)buf[8] = 'x';buf[9] = 0;return buf;}


原创粉丝点击