日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

【Linux】IPC-消息队列

發布時間:2023/12/6 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Linux】IPC-消息队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題 消息隊列id 和鍵值KEY區別?

首先要注意一個概念:IPC結構都是內核的結構。也就是說IPC結構由內核維護,對于每個進程都是公共的,不屬于某個特定進程。只有這樣,IPC結構才能支持它們“進程間通信”的功能。

有兩個東西可以標識一個IPC結構:標識符(ID)和鍵(key)。

Key是IPC結構的內部名。內部即在進程內部使用,這樣的標識方法是不能支持進程間通信的。

ID就是IPC結構的外部名。這些進程得到的ID其實是標識了同一個IPC結構,如消息隊列同時被進程A和進程B訪問。多個進程間就可以通過這個IPC結構通信。

已知一個key,當希望利用這個key創建一個新的IPC時,可以使用get函數,并在flag中指定IPC_CREAT位,例如隊列的情況,就是qid = msgget(key, IPC_CREAT)

一、基本概念

  • 消息隊列就是一個消息的鏈表。一條消息可以看作一個有結構的記錄。
  • IPC方式之一(進程間通信)
  • 消息可以通過結構類型區分

二、函數學習

2.1 創建消息隊列

2.1.1 函數名
msgget
2.1.2 函數原型
int msgget(key_t key, int msgflg);
2.1.3 函數功能
打開或創建消息隊列
2.1.4 頭文件
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>
2.1.5 返回值
success:the message queue identifier(消息隊列ID) error:-1

2.1.6 參數說明

key:鍵值 msgflg:打開標志. IPC_CREAT:標明新創建一個消息隊列。IPC_EXCL:標明打開一個消息隊列

2.2 寫消息

2.2.1 函數名
msgsnd
2.2.2 函數原型
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
2.2.3 功能
發送消息到消息隊列
2.2.4 頭文件
#include <sys/ipc.h>#include <sys/msg.h>
2.2.5 返回值
success:0 error:-1
2.2.6 參數說明
msqid:消息隊列的ID msgp:指向要發送的消息 msgsz:消息的長度(與結構有關,不含message type) msgflg:標志位
P.S.General Form of message
struct msgbuf {long mtype; /* message type, must be > 0 */char mtext[1]; /* message data */};The mtext field is an array (or other structure) whose size is speci-fied by msgsz, a non-negative integer value.

2.3 讀消息

2.3.1 函數名
msgrcv
2.3.2 函數原型
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
2.3.3 功能
從消息隊列中接收消息
2.3.4 頭文件
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>
2.3.5 返回值
success:實際接收消息的數據長度 error: -1
2.3.6 參數
msqid:消息隊列的id msgp :存放取出的消息 msgsz:希望取到消息的最大長度 msgtyp:消息的類型 msgtyp = 0 ,忽略類型,直接取隊列中的第一條 msgtyp >0, 取 消息隊列中類型等于msgtyp的第一條消息 msgtyp <0, 取 類型比msgtyp的絕對值要小或者等于的消息,如果有多條消息滿足該條件,則取類型號最小的一條。 If msgtyp is less than 0, then the first message in the queue withthe lowest type less than or equal to the absolute value of msgtypwill be read.msgflg:標志

2.4 刪除消息隊列(控制)

2.4.1 函數名
msgctl
2.4.2 函數原型
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
2.4.3 函數功能
控制消息隊列
2.4.4 頭文件
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>
2.4.5 返回值
success:0 error :-1
2.4.6 參數說明
msqid:消息隊列的id cmd:操作命令,IPC_RMID 用于刪除消息隊列 buf :獲取內核中的msqid_ds 結構

三、綜合實例

發送
/* Send.c*/#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>struct msgbuf {long msgtype;char msgtext[1024]; };int main() {int msqid;int msg_type;char str[256];struct msgbuf msgs;/* 創建消息隊列 */msqid = msgget(1024,IPC_CREAT);while (1){printf("Please input message type,0 for quit!\n");/* 獲取消息類型 */scanf("%d",&msg_type);/* 如果用戶輸入的消息類型為0,退出該循環 */if (msg_type == 0)break;/* 獲取消息數據 */printf("Please input message content!\n");scanf("%s",str);msgs.msgtype = msg_type;strcpy(msgs.msgtext,str);/* 發送消息 */msgsnd(msqid,&msgs,sizeof(struct msgbuf),0);}/* 刪除消息隊列 */msgctl(msqid,IPC_RMID,0);return 0;}
接收
/* Receive.c*/#include <stdio.h> //#include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>/* 多進程-子進程 */int msqid = 0;struct msgt {long msgtype;char msgtext[1024]; };/* 創建子線程 */ void childprocess() {struct msgt msgs;while(1){/* 接收消息隊列 */msgrcv(msqid,&msgs,sizeof(struct msgt), 0, 0);/* 打印消息隊列中的數據 */printf("message text: %s\n",msgs.msgtext);}return; }void main() {int i;int cpid;/* 打開消息隊列 */msqid = msgget(1024, IPC_EXCL);/* 創建3個子進程 */for(i=0;i<3;i++){cpid = fork();//創建子線程???if (cpid<0)printf("Creat childprocess ERROR\n");else if(cpid == 0)childprocess();}}

轉載于:https://www.cnblogs.com/Neo007/p/7287227.html

總結

以上是生活随笔為你收集整理的【Linux】IPC-消息队列的全部內容,希望文章能夠幫你解決所遇到的問題。

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