自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你如何創(chuàng)建Unix消息隊列

系統(tǒng) 其他OS
在這篇文章中,我們會教你如何創(chuàng)建Unix消息隊列的方法,應用Unix消息隊列進行消息的發(fā)送和接收發(fā)送消息到Unix消息隊列。

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

依據(jù)此數(shù)據(jù)結(jié)構(gòu)進行Unix消息隊列的創(chuàng)建,函數(shù)為msqueue_create(參數(shù)解釋:name消息隊列名,maxnum消息的最大個數(shù),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消息隊列進行消息的發(fā)送和接收發(fā)送消息到Unix消息隊列。

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

  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. //消息長度與創(chuàng)建時的長度不符,返回錯   
  9. if ( length != msqueue[id]->elementLength ) return MQERROR;   
  10. //消息隊列滿,不能發(fā)送   
  11. if ( msqueue[id]->curElementNum >= msqueue[id]->maxElements )   
  12. return MQERROR;   
  13. //在對消息隊列緩沖區(qū)操作前,鎖住緩沖區(qū),以免其他線程操作   
  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消息隊列中接收消息:
消息隊列的接收函數(shù) msqueue_receive,其參數(shù):id為消息隊列數(shù)組的索引號,buff為
消息內(nèi)容,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. //取消息前,鎖住消息隊列緩沖區(qū),以免其他線程存放或取消息   
  17. pthread_mutex_lock ( &&msqueue[id]->mutex_buff );   
  18. //為符合消息隊列FIFO特性,取出消息后,進行消息隊列的調(diào)整   
  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. //解除緩沖區(qū)鎖   
  32. pthread_mutex_unlock ( &&msqueue[id]->mutex_buff );   
  33. return length;   
  34. }   

Unix消息隊列的創(chuàng)建工作,我們就學習到這里了,希望大家能夠?qū)W習如何創(chuàng)建Unix消息隊列。

【編輯推薦】

  1. 詳解Unix消息隊列知識
  2. 知識講堂Unix內(nèi)核教學
  3. HP Tru64 UNIX消息隊列處理本地拒絕服務漏洞
  4. Unix內(nèi)核與Linux內(nèi)核大比拼
  5. 知識講解Unix 消息隊列
責任編輯:小霞
相關推薦

2010-04-21 14:49:13

Unix消息隊列

2010-04-21 12:12:56

Unix 消息隊列

2010-04-13 17:00:43

Unix消息隊列

2010-04-21 12:39:48

Unix 消息隊列

2017-10-11 15:08:28

消息隊列常見

2024-03-22 12:10:39

Redis消息隊列數(shù)據(jù)庫

2024-05-10 09:36:36

架構(gòu)消息隊列

2023-11-27 13:42:00

消息隊列RocketMQ

2024-08-02 10:55:30

2011-08-01 15:27:49

iPhone 界面

2020-10-26 09:19:11

線程池消息

2009-03-26 11:42:36

定時備份Oracle

2009-12-22 13:56:24

如何創(chuàng)建IP標準訪問表

2014-11-06 09:36:50

OVSvlan

2009-07-07 17:10:04

創(chuàng)建UNIX后門

2009-07-07 17:12:05

創(chuàng)建UNIX后門

2017-02-27 14:25:50

Java隊列Web

2009-12-07 09:23:05

2022-04-12 11:15:31

Redis消息隊列數(shù)據(jù)庫

2012-09-24 11:48:05

IBMdw
點贊
收藏

51CTO技術棧公眾號