鱼C论坛

 找回密码
 立即注册
查看: 1874|回复: 0

消息队列

[复制链接]
发表于 2021-11-21 15:37:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>

  7. #define MSGKEY 75
  8. struct msgform
  9. {
  10.         long mtype;
  11.         char mtext[512];
  12.        
  13. };

  14. int main()
  15. {
  16.         struct msgform msg;
  17.         int msgqid,pid,*pint;
  18.         msgqid =msgget (MSGKEY,0777);
  19.         pid= getpid ();
  20.         pint =(int*)msg.mtext;
  21.         *pint=pid;
  22.         msg.mtype=1;//定义消息类型
  23.         msgsnd(msgqid,&msg,sizeof(int),0);//发送消息
  24.         msgrcv(msgqid,&msg,512,pid,0);//接受服务者server消息
  25.         printf("client: receive from pid %d\n",*pint);
  26.        
  27. }




复制代码


  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #define MSGKEY 75
  9. struct msgform
  10. {
  11.         long mtype;
  12.         char mtext[512];
  13.        
  14. }msg;
  15. int msgqid;

  16. int main()
  17. {
  18.         int i,pid,*pint;
  19.         extern cleanup();
  20.         for(i=0;i<20;i++){//软中断处理
  21.                 signal (i,cleanup);
  22.         }
  23.         //建立与顾客进程相同的消息队列
  24.         msgqid=msgget(MSGKEY,0777|IPC_CREAT);
  25.         for(;;)
  26.         {
  27.                 msgrcv(msgqid,&msg, 512,1,0);
  28.                 pint=(int*)msg.mtext;
  29.                 pid =*pint;
  30.                 printf ("server:receive from pid %d\n",pid);//接受客户client消息
  31.                 msg.mtype= pid;
  32.                 *pint= getpid();
  33.                 msgsnd(msgqid,&msg,sizeof(int),0);
  34.         }
  35.        
  36. }

  37. cleanup()
  38. {
  39.         msgctl(msgqid,IPC_RMID,0);
  40.         exit (1);
  41. }


复制代码
请问为什么在我运行server接收不到消息?

ipc消息

ipc消息
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 05:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表