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

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值