msg2.c

来自「本文件介绍了unix内核的c源代码」· C语言 代码 · 共 36 行

C
36
字号
#include <sys/msg.h>#include <sys/types.h>#include <sys/ipc.h>#include <stdio.h>#include <sys/errno.h>extern int errno;struct mymsgbuf 			/* 定义消息结构 */{	long	mtype;   		/* 消息类型 */	char	ctext[100];		/* 消息数据 */};void main(){	struct mymsgbuf buf;	/* 申请消息缓冲 */	int msid;	int ret;	/* 打开(或创建)消息队列 */	if ((msid = msgget(0x1234, 0666|IPC_CREAT)) < 0) 	{		fprintf(stderr, "open msg %X failed.\n", 0x1234);		return ;	}	while(strncmp(buf.ctext, "exit", 4))	{		/* 置空消息缓冲区 */		memset(&buf, 0, sizeof(buf));			/* 接收消息 */		while ((ret = msgrcv(msid, &buf, sizeof(buf.ctext), 0, 0)) < 0)		{			if (errno == EINTR) continue;			return;		}		fprintf(stderr, "Msg : Type=%d, Len=%d, Text:%s", buf.mtype, ret, buf.ctext);	}}

⌨️ 快捷键说明

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