首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

初次编写消息队列程序,异常百出

2012-02-12 
初次编写消息队列程序,错误百出。用的sunOS5.0+gcc两个程序互相通信第一个ipc_ex1.c:#defineKEY_MSG0x101#d

初次编写消息队列程序,错误百出。
用的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   );
}
    }
}



[解决办法]
unix网络编程 书上有,看看。
[解决办法]
sunOS5.0+gcc
---------------
每个os的消息队列的实现或者函数名称都不是一样的
[解决办法]
sunOS5.0+gcc
---------------
每个os的消息队列的实现或者函数名称都不是一样的
===========
怎么都不一样了?在下只听说过SYSTEM V IPC 和 POSIX IPC两种, Solaris两种都支持,主要区别在于POSIX IPC是线程安全的。

google 一下,什么都有了
[解决办法]
message queue
有现成的库函数吧,
create, delete, send, receive,
[解决办法]
楼主对基本的消息队列使用方法还不了解,建议:
1.去这里看看:
http://www.ibm.com/developerworks/cn/aix/library/au-ipc/index.html
把几个例子程序自己输入一遍
2. Richard Stevens 的《UNIX网络编程》,看三遍,输入所有的例子程序, 然后换英文版。

热点排行