msg_server.c

来自「Linux大学上机源码学习」· C语言 代码 · 共 36 行

C
36
字号
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/ipc.h>#include <sys/msg.h>#define MSGKEY 75struct my_msg  {long mtype;   char text[256];  };main(){ struct my_msg msgbuf;  int running=1;  int msgid; long int msg_to_receive=0;  msgid=msgget(MSGKEY,0777|IPC_CREAT);  if(msgid==-1)    { printf("error\n");      exit(0);    } while(running)  { if (msgrcv(msgid,(void*)&msgbuf,256,msg_to_receive,0)==-1)      {  printf(" msgrcv error\n");         exit(0);}   printf("you wrote:%s",msgbuf.text);   if (strncmp(msgbuf.text,"end",3)==0)         running=0; }/* if(msgctl(msgid,IPC_RMID,0)==-1)  {  printf("msgctl(IPC_RMID)error");     exit(1); }*/ exit(0);}

⌨️ 快捷键说明

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