无码视频在线观看,99人妻,国产午夜视频,久久久久国产一级毛片高清版新婚

  • 始創(chuàng)于2000年 股票代碼:831685
    咨詢熱線:0371-60135900 注冊(cè)有禮 登錄
    • 掛牌上市企業(yè)
    • 60秒人工響應(yīng)
    • 99.99%連通率
    • 7*24h人工
    • 故障100倍補(bǔ)償
    全部產(chǎn)品
    您的位置: 網(wǎng)站首頁 > 幫助中心>文章內(nèi)容

    Linux關(guān)于消息隊(duì)列的使用

    發(fā)布時(shí)間:  2012/8/10 14:21:21
     今天完成消息隊(duì)列的使用。

      消息隊(duì)列類似FIFO,但是可以實(shí)現(xiàn)隨機(jī)查詢。

      主要函數(shù):msgget,msgsnd,msgrcv,msgctl

      實(shí)驗(yàn)實(shí)現(xiàn)兩個(gè)進(jìn)程之間的通信。

      一個(gè)發(fā)送,一個(gè)接受。并不需要額外實(shí)現(xiàn)進(jìn)程同步。

      源代碼來自華清遠(yuǎn)見:

      發(fā)送:

      #include

      #include

      #include

      #include

      #include

      #include

      #include

      #define BUFFER_SIZE 512

      struct message

      {

      long msg_type;

      char msg_text[BUFFER_SIZE];

      };

      int main()

      {

      int qid;

      key_t key;

      struct message msg;

      //ftok根據(jù)不同路徑和關(guān)鍵字產(chǎn)生標(biāo)準(zhǔn)的KEY

      if((key = ftok(".", 'a')) == -1)

      {

      perror("ftok");

      exit(1);

      }

      if((qid = msgget(key, IPC_CREAT|0666)) == -1)//創(chuàng)建

      {

      perror("msgget");

      exit(1);

      }

      printf("open queue %dn", qid);

      while(1)

      {

      printf("enter some message to the queue:");

      if((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)

      {

      puts("no message");

      exit(1);

      }

      msg.msg_type = getpid();//消息類型為進(jìn)程號(hào)

      if((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0)//發(fā)送消息

      {

      perror("message posted");

      exit(1);

      }

      if(strncmp(msg.msg_text, "quit", 4) == 0)

      {

      break;

      }

      }

      exit(0);

      }

      接收:

      #include

      #include

      #include

      #include

      #include

      #include

      #include

      #define BUFFER_SIZE 512

      struct message

      {

      long msg_type;

      char msg_text[BUFFER_SIZE];

      };

      int main()

      {

      int qid;

      key_t key;

      struct message msg;

      if((key = ftok(".", 'a')) == -1)

      {

      perror("ftok");

      exit(1);

      }

      if((qid = msgget(key, IPC_CREAT|0666)) == -1)//創(chuàng)建

      {

      perror("msgget");

      exit(1);

      }

      printf("open queue %dn", qid);

      do

      {

      memset(msg.msg_text, 0, BUFFER_SIZE);

      if(msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0) < 0)//接收消息

      {

      perror("msgrcv");

      exit(1);

      }

      printf("the message from process %d : %s", msg.msg_type, msg.msg_text);

      }while(strncmp(msg.msg_text, "quit", 4));

      if((msgctl(qid, IPC_RMID, NULL)) < 0)//從系統(tǒng)內(nèi)核移走消息隊(duì)列

      {

      perror("msgctl");

      exit(1);

      }

      exit(0);

      }

      打開兩個(gè)終端,1號(hào)發(fā)送,2號(hào)接收


    本文出自:億恩科技【mszdt.com】

    服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經(jīng)營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經(jīng)營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經(jīng)營性ICP/ISP證:贛B2-20080012
  • 服務(wù)器/云主機(jī) 24小時(shí)售后服務(wù)電話:0371-60135900
  • 虛擬主機(jī)/智能建站 24小時(shí)售后服務(wù)電話:0371-60135900
  • 專注服務(wù)器托管17年
    掃掃關(guān)注-微信公眾號(hào)
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權(quán)所有  地址:鄭州市高新區(qū)翠竹街1號(hào)總部企業(yè)基地億恩大廈  法律顧問:河南亞太人律師事務(wù)所郝建鋒、杜慧月律師   京公網(wǎng)安備41019702002023號(hào)
      1
     
     
     
     

    0371-60135900
    7*24小時(shí)客服服務(wù)熱線