LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 861|回复: 1

消息队列的程序

[复制链接]
发表于 2006-4-2 10:03:03 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
void quit(char *err)
{
  perror(err);
  exit(EXIT_FAILURE);
}
struct msgbuf
{
  long mtype;
  char msg_text[100];
};
int main(int arg,char *arc[])
{
  int ret;
  struct msgbuf msg;
  int msqid;
  msg.mtype=getpid();
  strcpy(msg.msg_text,"I send the message!\0");
  while(arg!=2)
  {
    printf("the number of arguments should be two\n");
    quit("arg number");
  }
  int key=atoi(arc[1]);
  if((msqid=msgget(key,0))<0)
  { printf("key:%d don't exists\n",key);
    msqid=msgget(key,IPC_CREAT|0666);
    if(msqid<0)
      quit("msgget create");
    printf("msg queue create successfully\n");
  }
  system("ipcs -q");
  int len=strlen(msg.msg_text);
  int pid;
  //if((pid=fork())<0)
   // quit("fork");
  //if(pid==0)
  {
    ret=msgsnd(msqid,&msg,len,0);
    if(ret<0)
      quit("msgsnd");
  }
  //else
  {
   // wait(pid);
    ret=msgrcv(msqid,&msg,len,0,0);
    if(ret<0)
      quit("msgrcv");
    printf("message content:%s\n",msg.msg_text);
    printf("message type=%d\n",msg.mtype);
  }
  system("ipcs -q");
  msgctl(msqid,IPC_RMID,NULL);
  exit(EXIT_SUCCESS);
}
现在这个程序是单进程,程序好使,当将注释加入的时候,想实现多进程,程序执行时就出现msgrcv的参数无效,为什么?
另外问一下,对于进程之间共资源“子进程具有父进程的副本”这句话怎样理解,是不是父进程的所有资源子进程都具有它的副本呢,请指教
发表于 2006-4-2 10:35:09 | 显示全部楼层
是你的wait搞错了,改一下,变成下面这个样子:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. void
  10. quit (char *err)
  11. {
  12.   printf("error : %d\n",errno);
  13.   perror (err);
  14.   exit (EXIT_FAILURE);
  15. }
  16. struct msgbuf
  17. {
  18.   long mtype;
  19.   char msg_text[100];
  20. };

  21. int
  22. main (int arg, char *arc[])
  23. {
  24.   int ret;
  25.   struct msgbuf msg = {1,{0}};;
  26.   int msqid;
  27.   msg.mtype = getpid ();
  28.   strcpy (msg.msg_text, "I send the message!\0");
  29.   while (arg != 2)
  30.     {
  31.       printf ("the number of arguments should be two\n");
  32.       quit ("arg number");
  33.     }
  34.   int key = atoi (arc[1]);
  35.   if ((msqid = msgget (key, 0)) < 0)
  36.     {
  37.       printf ("keyd don't exists\n", key);
  38.       msqid = msgget (key, IPC_CREAT | 0666);
  39.       if (msqid < 0)
  40.                 quit ("msgget create");
  41.       printf ("msg queue create successfully\n");
  42.     }
  43.   system ("ipcs -q");
  44.   int len = strlen (msg.msg_text);
  45.   int pid;
  46.   if((pid=fork())<0)
  47.         quit("fork");
  48.   if(pid==0)
  49.   {
  50.     ret = msgsnd (msqid, &msg, len, 0);
  51.     if (ret < 0)
  52.       quit ("msgsnd");
  53.     exit(0);
  54.   }
  55.   else
  56.   {
  57.     int status;
  58.     waitpid(pid,&status,0);
  59.     ret = msgrcv (msqid, &msg, len, 0, 0);
  60.     if (ret < 0)
  61.       quit ("msgrcv");
  62.     printf ("message contents %s\n", msg.msg_text);
  63.     printf ("message type=%d\n", msg.mtype);
  64.   }
  65.   system ("ipcs -q");
  66.   msgctl (msqid, IPC_RMID, NULL);
  67.   exit (EXIT_SUCCESS);
  68. }

复制代码
回复 支持 反对

使用道具 举报

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

本版积分规则

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