⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opmsg.c

📁 此源码是著名的教材BeginningLinux Programming中文名字叫Linux程序设计书中的源代码
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define BUF_SIZE  256
#define PROJ_ID   32
#define PATH_NAME  "."

void getmsgattr(int msgid, struct msqid_ds  msq_info); 

int main(void)
{
	/*用户自定义消息缓冲区*/
	struct mymsgbuf{
		long msgtype;
		char ctrlstring[BUF_SIZE];
	} msgbuffer;
	int qid; 	/*消息队列标识符*/
	int msglen;
	key_t msgkey;
	struct msqid_ds  msq_attr;

	/*获取键值*/
	if((msgkey = ftok(PATH_NAME, PROJ_ID)) == -1)
	{
		perror("ftok error!\n");
		exit(1);
	}

	/*获取消息队列标识符*/
	if((qid = msgget(msgkey, IPC_CREAT|0660)) == -1)
	{
		perror("msgget error!\n");
		exit(1);
	}

	getmsgattr(qid, msq_attr); /*输出消息队列的属性*/
	
	/*发送一条消息到消息队列*/
	msgbuffer.msgtype = 2;
	strcpy(msgbuffer.ctrlstring,"Aother message");
	msglen = sizeof(struct mymsgbuf)-4;
	if(msgsnd(qid,&msgbuffer,msglen,0) == -1)
	{
		perror("msgget error!\n");
		exit(1);
	}
	getmsgattr(qid, msq_attr); /*再输出消息队列的属性*/
	
	/*设置消息队列的属性*/
	msq_attr.msg_perm.uid = 3;
	msq_attr.msg_perm.gid = 2;
	if(msgctl(qid,IPC_SET, &msq_attr) == -1)
	{
		perror("msg set error!\n");
		exit(1);
	}
	getmsgattr(qid, msq_attr);/*修改后再观察其属性*/
	if(msgctl(qid, IPC_RMID, NULL) == -1)
	{
		perror("delete msg error!\n");
		exit(1);
	}
	getmsgattr(qid, msq_attr);/*删除后再观察其属性*/
	exit(0);
}

void getmsgattr(int msgid, struct msqid_ds  msg_info)
{
	if(msgctl(msgid,IPC_STAT,&msg_info) == -1)
	{
		perror("msgctl error!\n");
		return;
	}
	printf("****information of message queue %d****\n",msgid);
	printf("last msgsnd to msq time is %s\n", ctime(&(msg_info.msg_stime)));
	printf("last msgrcv time from msg is %s\n", ctime(&(msg_info.msg_rtime)));
	printf("last change msq time is %s\n", ctime(&(msg_info.msg_ctime)));
	printf("current number of bytes on queue is %d\n",msg_info.msg_cbytes);
	printf("number of messages in queue is %d\n",msg_info.msg_qnum);
	printf("max number of bytes on queue is %d\n",msg_info.msg_qbytes);
	printf("pid of last msgsnd is %d\n",msg_info.msg_lspid);
	printf("pid of last msgrcv is %d\n",msg_info.msg_lrpid);
		
	printf("msg uid is %d\n",msg_info.msg_perm.uid);
	printf("msg gid is %d\n",msg_info.msg_perm.gid);
	printf("******information end!**********\n",msgid);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -