溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

進(jìn)程間通信 :消息隊(duì)列的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-07-06 21:52:58 來(lái)源:網(wǎng)絡(luò) 閱讀:474 作者:mdd9502053669 欄目:編程語(yǔ)言

一.消息隊(duì)列

    消息隊(duì)列是一個(gè)進(jìn)程向另一個(gè)進(jìn)程發(fā)送一個(gè)數(shù)據(jù)塊的方法,所以消息隊(duì)列是基于消息的,而管道則是基于字節(jié)流的。消息隊(duì)列提供的是進(jìn)程間的雙向通信。

消息隊(duì)列中的幾個(gè)原型函數(shù):

   1.獲取消息信息:int msgget(key_t key,int msgflag);key 是用ftok()函數(shù)創(chuàng)建的

   2.接收消息:ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

   3.發(fā)送消息:int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

   4.銷(xiāo)毀消息信息:int msfctl(int msgid)

查看key值命令:ipcs -q

刪除key值命令:ipcs -q key值                    

//comm.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define _PATH_ "."  //路徑
#define _PROJ_ID_ 0x7777  
#define _BLOCK_SIZE_ 1024
#define _CLIENT_TYPE_ 1
#define _SERVER_TYPE_ 2
struct msgBuf  //定義一個(gè)消息結(jié)構(gòu)體
{
  long mtype; //消息類(lèi)型
  char mtext[_BLOCK_SIZE_];
 };

static int creat_msg_queue(); 
int get_msg_queue();
int send_msg_queue(int msg_id,const char*info,long type);
int recv_msg_queue(int msg_id,char info[],long type);
int destroy_msg_queue(int msg_id);

//comm.c

#include"comm.h"

int get_msg_queue()
{
  return creat_msg_queue();
 }

static int creat_msg_queue()
{
  key_t key=ftok(_PATH_,_PROJ_ID_);
  if(key<0)
   {
     perror("ftok");
     return -1;
    }
  int msg_id=msgget(key,IPC_CREAT);
  if(msg_id<0)
  {
    perror("msgget");
    return -1;
   }

   return msg_id;
}


int send_msg_queue(int msg_id,const char*info,long type)//將要發(fā)送的消息存入mtext中
{
   struct msgBuf msg;
   msg.mtype=type;
   memset(msg.mtext,'\0',sizeof(msg.mtext));
   strcpy(msg.mtext,info);
   if(msgsnd(msg_id,&msg,sizeof(msg.mtext),0)<0)
   {
     perror("msgsnd");
     return -1;

    }
    return 0;
}

int recv_msg_queue(int msg_id,char* info,long type)//將mtext中的消息拿出放入info中
{
  struct msgBuf msg;
  if(msgrcv(msg_id,&msg,sizeof(msg.mtext),type,0)<0)
  {  
     perror("msgrcv");
     return -1;
   }
   strcpy(info,msg.mtext);
   return 0;
}

int destroy_msg_queue(int msg_id)
{
  if(msgctl(msg_id,IPC_RMID,NULL)<0)
  {
    perror("msgctl");
    return -1;
  }
  return 0;

}
                                                                                         //server.c  先發(fā)送后接收
                                                                                         
#include"comm.h"

int main()
{
  int msgid=get_msg_queue();
  if(msgid<0)
  {
    exit(1);
   }
   char info[_BLOCK_SIZE_];

   while(1)
   {
     memset(info,'\0',sizeof(info));
     printf("please input:");
     fflush(stdout);
     gets(info);
     if(send_msg_queue(msgid,info,_SERVER_TYPE_)<0)
      {
        printf("send  information failed\n");
        exit(1);
       }
      if(recv_msg_queue(msgid,info,_CLIENT_TYPE_)<0)
      {
        printf("recieve information failed\n");
        exit(1);
     }
     printf("client:%s\n",info);
}
  destroy(msgid);
  return 0;
}


//client.c 先接收后發(fā)送
 #include"comm.h"
 int main()
{
   int msgid=get_msg_queue();
    if(msgid<0)
     {
      exit(1);
      }
     char info[_BLOCK_SIZE_];
     memset(info,'\0',sizeof(info));
     printf("when input stop endding...\n");
     while(1)
     {
        if(recv_msg_queue(msgid,info,_SERVER_TYPE_)<0)
          {
            printf("recieve information failed\n");
            exit(1);
           }
       else
         {
   
           if(strcmp("stop",info)==0)
           {
             return 0;
            }
          printf("server :%s\n",info);
         }

      printf("please input:");
      fflush(stdout);
      gets(info);
      if(send_msg_queue(msgid,info,_CLIENT_TYPE_)<0)
       {
         printf("send information failed\n");
         exit(1);
        }
    }
  destroy(msgid);
  return 0;

}

運(yùn)行結(jié)果:

進(jìn)程間通信 :消息隊(duì)列的實(shí)現(xiàn)






向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI