msg1.c
来自「本文件介绍了unix内核的c源代码」· C语言 代码 · 共 38 行
C
38 行
#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; /* 打开(或创建)消息队列 */ 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)); fprintf(stderr, "Please input:"); /* 从键盘输入消息数据内容 */ fgets(buf.ctext, sizeof(buf.ctext), stdin); /* 设置消息类型 */ buf.mtype = getpid(); /* 发送消息 */ while ((msgsnd(msid, &buf, strlen(buf.ctext), 0)) < 0) { if (errno == EINTR) continue; return; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?