教你如何創(chuàng)建Unix消息隊列
在我們以前學習過Unix 線程的知識后,我們來學習下Unix消息隊列的知識,在文章中,我們主要講解Unix消息隊列的創(chuàng)建的知識,希望對大家對Unix的學習有所幫助。
依據(jù)此數(shù)據(jù)結(jié)構(gòu)進行Unix消息隊列的創(chuàng)建,函數(shù)為msqueue_create(參數(shù)解釋:name消息隊列名,maxnum消息的最大個數(shù),length單個消息的長度)。
- int msqueue_create( name, maxnum, length )
- char name;
- int maxnum,length;
- {
- int i;
- for ( i=0; i
- if ( msqueue[i]==NULL )break;
- //如果Unix消息隊列全部被分配,返回錯
- if ( i==MAXQUEUE ) return MQERROR;
- msqueue[i]=malloc(sizeof(mq_attribstruct));
- sprintf( msqueue[i]->name, "%s", name);
- msqueue[i]->maxElements = maxnum;
- msqueue[i]->elementLength = length;
- msqueue[i]->curElementNum = 0;
- msqueue[i]->buff=malloc(maxnum?length);
- //對保護鎖進行初始化
- pthread_mutex_init(&&msqueue[i]
- ->mutex_buff, NULL);
- pthread_mutex_init(&&msqueue[i]
- ->mutex_cond, NULL);
- //對線程同步條件變量初始化
- pthread_cond_init(&&msqueue[i]->cond, NULL);
- return i;
- }
應用Unix消息隊列進行消息的發(fā)送和接收發(fā)送消息到Unix消息隊列。
Unix消息隊列的發(fā)送和接收是在不同的線程中進行的。首先介紹發(fā)送消息到Unix消息隊列的函數(shù):
- int msqueue_send ( id, buff, length )
- int id, length;
- caddr_t buff;
- {
- int pos;
- //消息隊列id錯,返回錯
- if ( id<0 || id >= MAXQUEU ) return MQERROR;
- //消息長度與創(chuàng)建時的長度不符,返回錯
- if ( length != msqueue[id]->elementLength ) return MQERROR;
- //消息隊列滿,不能發(fā)送
- if ( msqueue[id]->curElementNum >= msqueue[id]->maxElements )
- return MQERROR;
- //在對消息隊列緩沖區(qū)操作前,鎖住緩沖區(qū),以免其他線程操作
- pthread_mutex_lock ( &&msqueue[id]->mutex_buff );
- pos = msqueue[id]->curElementNum * msqueue[id]->elementLength;
- bcopy ( buff, &&msqueue[id]->buff[pos], msqueue[id]->elementLength );
- msqueue[id]->curElementNum ++;
- pthread_mutex_unlock ( &&msqueue[id]->mutex_buff );
- //如果插入消息前,消息隊列是空的,插入消息后,消息隊列為非空,則通知等待從消
- 息隊列取消息的線程,條件滿足,可以取出消息進行處理
- if ( msqueue[id]->curElementNum == 1 ) {
- pthread_mutex_lock ( &&msqueue[id]->mutex_cond );
- pthread_cond_broadcast ( &&msqueue[id]->cond );
- pthread_mutex_unlock ( &&msqueue[id]->mutex_cond );
- }
- return length;
- }
從Unix消息隊列中接收消息:
消息隊列的接收函數(shù) msqueue_receive,其參數(shù):id為消息隊列數(shù)組的索引號,buff為
消息內(nèi)容,length為消息長度。
- int msqueue_receive ( id, buff, length )
- int id, length;
- caddr_t buff;
- {
- caddr_t temp;
- int pos;
- if(id<0||id>=MAXQUEUE)return MQERROR;
- if(length != msqueue[id]->elementLength)
- return MQERROR;
- //如果消息隊列為空,則等待,直到消息隊列為非空條件滿足
- if ( msqueue[id]->curElementNum == 0){
- pthread_mutex_lock ( &&msqueue[id]->mutex_cond );
- pthread_cond_wait ( &&msqueue[id]->cond, &&msqueue[id]->mutex_cond );
- pthread_mutex_unlock ( &&msqueue[id]->mutex_cond );
- }
- //取消息前,鎖住消息隊列緩沖區(qū),以免其他線程存放或取消息
- pthread_mutex_lock ( &&msqueue[id]->mutex_buff );
- //為符合消息隊列FIFO特性,取出消息后,進行消息隊列的調(diào)整
- temp =
- malloc((msqueue[id]->curElementNum-1)
- msqueue[id]-elementLength );
- bcopy ( &&msqueue[id]->buff[0], buff, msqueue[id]->elementLength );
- msqueue[id]->curElementNum --;
- bcopy ( &&msqueue[id]->buff[msqueue[id]->elementLength], temp,
- msqueue[id]->elementLength
- msqueue[id]->curElementNum);
- bcopy ( temp, &&msqueue[id]->buff[0],
- msqueue[id]->elementLength
- msqueue[id]->curElementNum);
- free ( temp );
- //解除緩沖區(qū)鎖
- pthread_mutex_unlock ( &&msqueue[id]->mutex_buff );
- return length;
- }
Unix消息隊列的創(chuàng)建工作,我們就學習到這里了,希望大家能夠?qū)W習如何創(chuàng)建Unix消息隊列。
【編輯推薦】