您的位置:程序门 -> linux/unix社区 -> 程序开发区



初次编写消息队列程序,错误百出。


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


初次编写消息队列程序,错误百出。[已结贴,结贴人:sexisnothing]
发表于:2007-07-05 17:27:25 楼主
用的sunos5.0+gcc
两个程序互相通信
第一个
ipc_ex1.c
:
#define   key_msg     0x101
#define   msgsize     128
#include   <stdio.h>
#include   <sys/types.h>
#include   <sys/ipc.h>
#include   <sys/msg.h>
#include   <string.h>
main()
{
    int   msgid;
    struct   msgbuf
    {
          long     mtype;
          char     mtext[128];
    }   buf1,   buf2,bufsign;
    msgid   =   msgget(   key_msg,   0666   );
    while(   1   )
    {   //   block
        if(gets(   buf1.mtext   ));
        {
buf1.mtype   =   2l;
msgsnd(   msgid,   &buf1,   msgsize,   0   );
if(   buf1.mtext[0]   ==   'x '   ¦ ¦   buf1.mtext[0]   ==   'x '   )
      break;
msgrcv(   msgid,   &bufsign,   msgsize,   1l,   0   );
printf(   "receive   message:   %s\n ",   bufsign.mtext   );
}
      if(msgrcv(   msgid,   &buf2,   msgsize,   2l,   0   ));
{
printf(   "from2:   %s\n ",   buf2.mtext   );
if(   buf2.mtext[0]   ==   'x '   ¦ ¦   buf2.mtext[0]   ==   'x '   )
      break;
bufsign.mtype   =   1l;
strcpy(   bufsign.mtext,   "received   ok "   );
msgsnd(   msgid,   &bufsign,   msgsize,   0   );
}
    }
}


第二个
ipc_ex2.c


#define   key_msg     0x101
#define   msgsize     128
#include   <stdio.h>
#include   <sys/types.h>
#include   <sys/ipc.h>
#include   <sys/msg.h>
#include   <string.h>
main()
{
    int   msgid;
    struct   msgbuf
    {
          long     mtype;
          char     mtext[128];
    }   buf1,   buf2,bufsign;
    msgid   =   msgget(   key_msg,   0666   );
    while(   1   )
    {   //   block
        if(gets(   buf2.mtext   ));
        {
buf2.mtype   =   2l;
        msgsnd(   msgid,   &buf2,   msgsize,   0   );
        if(   buf2.mtext[0]   ==   'x '   ¦ ¦   buf2.mtext[0]   ==   'x '   )
              break;
msgrcv(   msgid,   &bufsign,   msgsize,   1l,   0   );
        printf(   "receive   message:   %s\n ",   bufsign.mtext   );
}
      if(msgrcv(   msgid,   &buf1,   msgsize,   2l,   0   ));
{
        printf(   "from1:   %s\n ",   buf1.mtext   );
        if(   buf1.mtext[0]   ==   'x '   ¦ ¦   buf1.mtext[0]   ==   'x '   )
              break;
bufsign.mtype   =   1l;
        strcpy(   bufsign.mtext,   "received   ok "   );
        msgsnd(   msgid,   &bufsign,   msgsize,   0   );
}
    }
}

发表于:2007-07-05 17:36:301楼 得分:5
unix网络编程     书上有,看看。
发表于:2007-07-05 17:51:242楼 得分:10
sunos5.0+gcc
---------------
每个os的消息队列的实现或者函数名称都不是一样的
发表于:2007-07-06 00:32:093楼 得分:0
谁能帮忙看看?
发表于:2007-07-06 07:52:544楼 得分:10
sunos5.0+gcc
---------------
每个os的消息队列的实现或者函数名称都不是一样的
===========
怎么都不一样了?在下只听说过system   v   ipc   和   posix   ipc两种,   solaris两种都支持,主要区别在于posix   ipc是线程安全的。

google   一下,什么都有了
发表于:2007-07-06 08:26:505楼 得分:10
message   queue
有现成的库函数吧,
create,   delete,   send,   receive,
发表于:2007-07-08 11:47:046楼 得分:15
楼主对基本的消息队列使用方法还不了解,建议:
1.去这里看看:
http://www.ibm.com/developerworks/cn/aix/library/au-ipc/index.html
把几个例子程序自己输入一遍
2.   richard   stevens   的《unix网络编程》,看三遍,输入所有的例子程序,   然后换英文版。


快速检索

最新资讯
热门点击