日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

POSIX 消息队列基础知识复习,以及相关例程

發布時間:2025/3/20 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POSIX 消息队列基础知识复习,以及相关例程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.1????????Posix消息隊列

1.1.1???????消息隊列的創建和刪除

1.1.1.1?????mq_open( )

#include<mqueue.h>

mqd_tmq_open( const char *name, int flag )

mqd_t mq_open( const char *name, int flag, mode_t mode, mq_attrattr )

創建或獲取一個消息隊列。成功返回消息隊列描述符;失敗返回-1。

參數name指定與消息隊列相關聯的名字。

參數flags可以取:

O_RDONLY???? O_WRONLY????? O_RDWR(三選一)

O_CREAT

單獨使用O_CREAT,如果不存在與name相關聯的消息隊列,就創建一個新的消息隊列,并返回其描述符,如果已經存在與name相關聯的消息隊列,就返回該消息隊列的描述符。

如果指定了O_CREAT,需要使用mq_open( )的第二種形式,其中參數mode指定了新創建消息隊列的訪問權限,參數attr指定了新創建消息隊列的屬性。

O_EXCL。

單獨使用O_EXCL是沒有意義的,如果O_EXCL和O_CREAT一起使用,當與name相關聯的消息隊列已經存在時,就失敗返回。

O_NONBLOCK

決定在mq_send( )和mq_receive( )時是否會掛起當前進程,直到讀取或發送消息成功。

1.1.1.2????? mq_close( )

#include <mqueue.h>

int mq_close( mqd_t mqdes )

關閉當前進程和指定消息隊列之間的聯系。

成功返回0,失敗返回-1。

1.1.1.3????? mq_unlink( )

#include <mqueue.h>

int mq_unlink( const char *name )

刪除指定的消息隊列,但是如果當前仍有其它進程正在使用消息隊列,則暫時放棄刪除操作,并立即返回,直到其它進程通過調用mq_close( )關閉之后,再進行刪除操作。

成功返回0,失敗返回-1。

1.1.2???????消息隊列的屬性

1.1.2.1?????mq_getattr( )

#include<mqueue.h>

intmq_getattr( mqd_t mqdes, struct mq_attr *attr )

成功返回0,失敗返回-1。

struct mq_attr

{

long?????? mq_flags;???????? //message queue flags

long?????? mq_maxmsg;?????? //maximum number of messages

long?????? mq_msgsize; // maximummessage size

long?????? mq_curmsgs;?????? // numberof messages currently queued

}

1.1.2.2?????mq_setattr( )

#include<mqueue.h>

intmq_setattr( mqd_t mqdes, const struct mq_attr *newAttr, struct mq_attr *oldAttr)

注意:只能設定mq_attr結構中的mq_flags,mq_attr結構中的其它成員將被忽略。

成功返回0,失敗返回-1。

1.1.3???????消息隊列的操作

1.1.3.1?????mq_notify()

#include<mqueue.h>

int mq_notify( mqd_t mqdes, const struct sigevent*notification )

成功返回0,失敗返回-1。

為當前進程在指定的消息隊列上注冊notify操作,當消息隊列由空變為非空時,也就是說有消息加入原本為空的消息隊列時,會觸發進程注冊的nofity操作。

參數notification指定需要注冊的nofity操作。如果參數notification為NULL,而且進程之前注冊過notify操作,會取消之前注冊的notify操作。

需要注意的是:

1.只能有唯一的一個進程在消息隊列上注冊notify操作。如果已經有其它進程在消息隊列上注冊了notify操作,試圖再次進行notify()會失敗返回;

2.當進程注冊的nofity操作被觸發之后,該nofity操作就會被刪除,其它進程可以重新向該消息隊列注冊notify操作。

如果進程已經注冊過notify操作,而且進程在消息隊列為空時,阻塞調用了mq_receive( )(即在mq_open()時設定了O_NONBLOCK),如果有一個消息到達,會先滿足mq_receive( )調用,所以消息隊列仍然為空,不會觸發notify操作。

struct sigevent

{

int?? sigev_notify;

int?? sigev_signo;? // signal numbersent to current process when the timer expires

union sigval???????? sigev_value;? // info carried with signal

NotifyFUN???????? sigev_notify_function;????? //typedef? void (*NotifyFUN)( union sigval)

pthread_attr_t*?? sigev_notify_attributes;

}

sigev_notify的取值:

SIGEV_NONE??????? :No notification will be delivered when the event ofinterest occurs.

SIGEV_SIGNAL???? :A queued signal will be generated when theevent of interest occurs.

SIGEV_THREAD?? :A notification function will be called toperform notification.

1.2.3.2?????mq_receive( )

#include<mqueue.h>

ssize_tmq_receive( mqd_t mqdes, char *msg, size_t len, unsigned int *prio )

讀取the oldest of the highest priority message。

參數len指定讀取消息的長度,如果len<attr(mq_msgsize),失敗返回。

mq_open( )是否設定O_NONBLOCK,會決定mq_receive( )是否進行阻塞讀取。

成功返回讀取消息的字節數,失敗返回-1。

1.2.3.3?????mq_send( )

#include<mqueue.h>

intmq_send( mqd_t mqdes, const char *msg, size_t len, unsigned int prio )

參數len指定發送消息的長度,如果len>attr(mq_msgsize),失敗返回。

參數prio<MQ_PRIO_MAX。

mq_open( )是否設定O_NONBLOCK,會決定mq_send( )是否進行阻塞發送。

成功返回0,失敗返回-1。

?

2.1? Code

2.1.1 創建一個消息隊列

1 /* 2 @Author: shaosli 3 @data: 2016/1/5 4 @function: test create mq of posxi 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 #include <mqueue.h> 11 #include <fcntl.h> 12 #include <errno.h> 13 #include <sys/stat.h> 14 15 int main( int argc, char* argv[]) 16 { 17 18 mqd_t mqd; 19 struct mq_attr attr; 20 int flags = O_RDWR | O_CREAT | O_EXCL; 21 char *pdName = "/mqu"; 22 printf("create mqueue.\n"); 23 //create a new queue. 24 if( mqd = mq_open(pdName, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, NULL) == -1) 25 { 26 perror("open failure."); 27 exit(-1); 28 } 29 30 mq_getattr(mqd, &attr); 31 printf("max msgs = %ld, mq_msgsize = %ld,mq_curmsgs = %ld\n", attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs); 32 mq_close(0); 33 return 0; 34 } View Code

?

2.1.2 發送消息

1 /* 2 @Author: shaosli 3 @data: 2016/1/5 4 @function: test send mq of posxi 5 */ 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <unistd.h> 9 #include <mqueue.h> 10 #include <fcntl.h> 11 #include <errno.h> 12 #include <sys/stat.h> 13 14 int main() 15 { 16 mqd_t mqd; 17 char *pdName = "/mqu"; 18 char* ptr = "hi"; 19 mqd = mq_open(pdName, O_WRONLY); 20 21 if( mq_send(mqd, ptr, 10, 10) == -1) 22 { 23 perror("mq_send(),error."); 24 exit(-1); 25 } 26 27 return 0; 28 } View Code

2.1.3 接受消息

?

1 /* 2 @Author: shaosli 3 @data: 2016/1/5 4 @function: test receive mq of posxi 5 */ 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <unistd.h> 9 #include <mqueue.h> 10 #include <fcntl.h> 11 #include <errno.h> 12 #include <sys/stat.h> 13 14 int main() 15 { 16 mqd_t mqd; 17 int priot, n; 18 struct mq_attr attr; 19 char *pdName = "/mqu"; 20 mqd = mq_open(pdName, O_RDONLY); 21 22 mq_getattr(mqd, &attr); 23 char *buf = malloc(10); 24 25 if( (n = mq_receive(mqd, buf, attr.mq_msgsize, &priot)) == -1) 26 { 27 perror("mq_receive(),error."); 28 exit(-1); 29 } 30 printf("priot = %d, recbuf=%s, attr.mq_msgsize =%ld", priot, buf,attr.mq_msgsize); 31 32 return 0; 33 } View Code

?

轉載于:https://www.cnblogs.com/shaosli/p/5103054.html

總結

以上是生活随笔為你收集整理的POSIX 消息队列基础知识复习,以及相关例程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。