| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 335 人关注过本帖
标题:两个进程间用消息队列的 通信 高手帮帮忙 谢谢哈
只看楼主 加入收藏
露营地
Rank: 2
来 自:湛江
等 级:论坛游民
帖 子:150
专家分:23
注 册:2015-6-27
结帖率:92.86%
收藏
已结贴  问题点数:30 回复次数:1 
两个进程间用消息队列的 通信 高手帮帮忙 谢谢哈
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <sys/types.h>
  6 #include <sys/ipc.h>
  7 #include <sys/msg.h>
  8
  9 typedef struct
 10 {
 11     long int mtype;
 12     char mtext[50];
 13     char name[20];
 14 }MSG;
 15
 16 int main(int argc, char *argv[])
 17 {
 18     key_t key;
 19     MSG msg;
 20     MSG msgg;
 21     int i=1;
 22     int msgid;
 23     int pid;
 24     char buf[100];
 25
 26     key=ftok("/desktop/gcc1", 2012);
 27     msgid=msgget(key, IPC_CREAT|0666);
 28     if(msgid==-1)
 29     {
 30         perror("msgget");
 31         exit(-1);
 32     }
 33
 34     pid=fork();
 35     if(pid<0)
 36     {
 37         perror("fork");
 38         exit(1);
 39     }
 40     if(pid==0)
 41     {
 42         while(i)
 43         {
 44             msgrcv(msgid, &msg, 50, 10, 0);
 45             printf("%s: ", msg.name);
 46             fflush(stdout);
 47             printf("%s\n", msg.mtext);
 48             fflush(stdout);
 49             if(strcmp(msg.mtext, "end")==0)
 50                 i=0;
 51         }
 52             msgctl(msgid, IPC_RMID, NULL);
 53
 54     }
 55     if(pid>0)
 56     {
 57         strcpy(msgg.name, "lucy");
 58         msgg.mtype=9;
 59         while(i)
 60         {
 61             memset(buf, 0, sizeof(buf));
 62             printf("%s: ", msgg.name);
 63             fflush(stdout);
 64             fgets(buf, 100, stdin);
 65             strcpy(msgg.mtext, buf);
 66             msgsnd(msgid, &msgg, 50, 0);
 67             if(strcmp(msgg.mtext,"end")==0)
 68                 i=0;
 69         }
 70     }
 71     return 0;
 72 }





  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <sys/types.h>
  5 #include <sys/ipc.h>
  6 #include <sys/msg.h>
  7 #include <string.h>
  8
  9 typedef struct
 10 {
 11     long int mtype;
 12     char mtext[50];
 13     char name[20];
 14 }MSG;
 15
 16 int main(int argc, char *argv[])
 17 {
 18     int msgid;
 19     int pid;
 20     int i=1;
 21     key_t key;
 22     MSG msg, msgg;
 23     char buf[100];
 24
 25     key=ftok("/desktop/gcc1", 2012);
 26     msgid=msgget(key, IPC_CREAT|0666);
 27     if(msgid==-1)
 28     {
 29         perror("msgget");
 30         exit(1);
 31     }
 32
 33     pid=fork();
 34     if(pid<0)
 35     {
 36         perror("fork");
 37         exit(1);
 38     }
 39     if(pid==0)
 40     {
 41         while(i)
 42         {
 43             msgrcv(msgid, &msgg, 50, 9, 0);
 44             printf("%s: ", msgg.name);
 45             fflush(stdout);
 46             printf("%s\n", msgg.mtext);
 47             fflush(stdout);
 48             if(strcmp(msgg.mtext, "end")==0)
 49                 i=0;
 50         }
 51             msgctl(msgid, IPC_RMID, NULL);
 52     }
 53     if(pid>0)
 54     {
 55         strcpy(msg.name, "peter");
 56         msg.mtype=10;
 57         while(i)
 58         {
 59             memset(buf, 0, sizeof(buf));
 60             printf("%s: ", msg.name);
 61             fflush(stdout);
 62             fgets(buf, 100, stdin);
 63             strcpy(msg.mtext, buf);
 64             msgsnd(msgid, &msg, 50, 0);
 65             if(strcmp(msg.mtext, "end")==0);
 66                 i=0;
 67         }
 68     }
 69     return 0;
 70
 71 }
   

这两个程序是我写的两个进程间用消息队列的 通信     不过有点不对   找不出原因  希望高手帮帮忙  谢谢                                                                    1,1           Top

搜索更多相关主题的帖子: include 通信 
2015-11-22 11:23
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:30 
问题挺多的。

首先、消息结构定义的就有问题。而且你msgsnd中消息的长度也只等于第一个数组的长度。

第二、两个进程都是以IPC_CREAT方式创建消息队列,虽然生成的键相同,但当key已经存在时后一个进程将返回-1,并不能获取已经存在的消息队列标识符,进而并不能通过消息队列传递信息。

第三、看代码你是希望两个进程都可以创建和删除消息队列,但你的实现方式会导致一个进程关闭了消息队列另一个进程仍发送或接收消息的问题。由于你这两个进程又各fork成两个进程,其中子进程接收对方父进程的消息,父进程给对方子进程发消息。可以建立两个消息队列。用一个也可以,但需要设置个标志来协调队列调度。

重剑无锋,大巧不工
2015-11-22 17:32
快速回复:两个进程间用消息队列的 通信 高手帮帮忙 谢谢哈
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015009 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved