歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> Unix資訊 >> 教你如何創建Unix消息隊列

教你如何創建Unix消息隊列

日期:2017/3/6 11:40:56   编辑:Unix資訊

在我們以前學習過Unix 線程的知識後,我們來學習下Unix消息隊列的知識,在文章中,我們主要講解Unix消息隊列的創建的知識,希望對大家對Unix的學習有所幫助。

依據此數據結構進行Unix消息隊列的創建,函數為msqueue_create(參數解釋:name消息隊列名,maxnum消息的最大個數,length單個消息的長度)。

  1. int msqueue_create( name, maxnum, length )
  2. char name;
  3. int maxnum,length;
  4. {
  5. int i;
  6. for ( i=0; i
  7. if ( msqueue[i]==NULL )break;
  8. //如果Unix消息隊列全部被分配,返回錯
  9. if ( i==MAXQUEUE ) return MQERROR;
  10. msqueue[i]=malloc(sizeof(mq_attribstruct));
  11. sprintf( msqueue[i]->name, "%s", name);
  12. msqueue[i]->maxElements = maxnum;
  13. msqueue[i]->elementLength = length;
  14. msqueue[i]->curElementNum = 0;
  15. msqueue[i]->buff=malloc(maxnum?length);
  16. //對保護鎖進行初始化
  17. pthread_mutex_init(&&msqueue[i]
  18. ->mutex_buff, NULL);
  19. pthread_mutex_init(&&msqueue[i]
  20. ->mutex_cond, NULL);
  21. //對線程同步條件變量初始化
  22. pthread_cond_init(&&msqueue[i]->cond, NULL);
  23. return i;
  24. }

應用Unix消息隊列進行消息的發送和接收發送消息到Unix消息隊列。

Unix消息隊列的發送和接收是在不同的線程中進行的。首先介紹發送消息到Unix消息隊列的函數:

  1. int msqueue_send ( id, buff, length )
  2. int id, length;
  3. caddr_t buff;
  4. {
  5. int pos;
  6. //消息隊列id錯,返回錯
  7. if ( id<0 || id >= MAXQUEU ) return MQERROR;
  8. //消息長度與創建時的長度不符,返回錯
  9. if ( length != msqueue[id]->elementLength ) return MQERROR;
  10. //消息隊列滿,不能發送
  11. if ( msqueue[id]->curElementNum >= msqueue[id]->maxElements )
  12. return MQERROR;
  13. //在對消息隊列緩沖區操作前,鎖住緩沖區,以免其他線程操作
  14. pthread_mutex_lock ( &&msqueue[id]->mutex_buff );
  15. pos = msqueue[id]->curElementNum * msqueue[id]->elementLength;
  16. bcopy ( buff, &&msqueue[id]->buff[pos], msqueue[id]->elementLength );
  17. msqueue[id]->curElementNum ++;
  18. pthread_mutex_unlock ( &&msqueue[id]->mutex_buff );
  19. //如果插入消息前,消息隊列是空的,插入消息後,消息隊列為非空,則通知等待從消
  20. 息隊列取消息的線程,條件滿足,可以取出消息進行處理
  21. if ( msqueue[id]->curElementNum == 1 ) {
  22. pthread_mutex_lock ( &&msqueue[id]->mutex_cond );
  23. pthread_cond_broadcast ( &&msqueue[id]->cond );
  24. pthread_mutex_unlock ( &&msqueue[id]->mutex_cond );
  25. }
  26. return length;
  27. }


從Unix消息隊列中接收消息:
消息隊列的接收函數 msqueue_receive,其參數:id為消息隊列數組的索引號,buff為
消息內容,length為消息長度。

  1. int msqueue_receive ( id, buff, length )
  2. int id, length;
  3. caddr_t buff;
  4. {
  5. caddr_t temp;
  6. int pos;
  7. if(id<0||id>=MAXQUEUE)return MQERROR;
  8. if(length != msqueue[id]->elementLength)
  9. return MQERROR;
  10. //如果消息隊列為空,則等待,直到消息隊列為非空條件滿足
  11. if ( msqueue[id]->curElementNum == 0){
  12. pthread_mutex_lock ( &&msqueue[id]->mutex_cond );
  13. pthread_cond_wait ( &&msqueue[id]->cond, &&msqueue[id]->mutex_cond );
  14. pthread_mutex_unlock ( &&msqueue[id]->mutex_cond );
  15. }
  16. //取消息前,鎖住消息隊列緩沖區,以免其他線程存放或取消息
  17. pthread_mutex_lock ( &&msqueue[id]->mutex_buff );
  18. //為符合消息隊列FIFO特性,取出消息後,進行消息隊列的調整
  19. temp =
  20. malloc((msqueue[id]->curElementNum-1)
  21. msqueue[id]-elementLength );
  22. bcopy ( &&msqueue[id]->buff[0], buff, msqueue[id]->elementLength );
  23. msqueue[id]->curElementNum --;
  24. bcopy ( &&msqueue[id]->buff[msqueue[id]->elementLength], temp,
  25. msqueue[id]->elementLength
  26. msqueue[id]->curElementNum);
  27. bcopy ( temp, &&msqueue[id]->buff[0],
  28. msqueue[id]->elementLength
  29. msqueue[id]->curElementNum);
  30. free ( temp );
  31. //解除緩沖區鎖
  32. pthread_mutex_unlock ( &&msqueue[id]->mutex_buff );
  33. return length;
  34. }

Unix消息隊列的創建工作,我們就學習到這裡了,希望大家能夠學習如何創建Unix消息隊列。

Copyright © Linux教程網 All Rights Reserved