📄 p11-14.c
字号:
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include "err_exit.h"struct my_msg { /* 消息缓冲区结构类型定义 */ long int my_msg_type; char text[BUFSIZ];};int main(){ int running = 1; struct my_msg msgbuf; int msgid; /* 创建\打开我们的消息队列 */ msgid = msgget((key_t)1234, 0666 | IPC_CREAT); if (msgid == -1) err_exit("msgget failed\n"); /* 反复将用户输入的数据发送至消息队列,字符串"end"标志输入结束 */ while(running) { /* 读入输入字符串至消息缓冲区 */ printf("Enter some text: "); fgets(msgbuf.text, BUFSIZ, stdin); msgbuf.my_msg_type = 1; /* 向消息队列发送消息,阻塞方式 */ if (msgsnd(msgid, (void *)&msgbuf, BUFSIZ, 0) == -1) err_exit("msgsnd failed\n"); /* 如果是"end",结束循环 */ if (strncmp(&msgbuf.text, "end", 3) == 0) running = 0; } exit(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -