在兩個(gè)終端里用 gcc 分別編譯運(yùn)行上面兩個(gè)文件,可以看到輸出結(jié)果如下:
[]$ ./write_fifoI am 7872 processSend message: Process 7872's time is Mon Jan 16 18:00:23 2023Send message: Process 7872's time is Mon Jan 16 18:00:24 2023Send message: Process 7872's time is Mon Jan 16 18:00:25 2023Send message: Process 7872's time is Mon Jan 16 18:00:26 2023Send message: Process 7872's time is Mon Jan 16 18:00:27 2023Send message: Process 7872's time is Mon Jan 16 18:00:28 2023Send message: Process 7872's time is Mon Jan 16 18:00:29 2023Send message: Process 7872's time is Mon Jan 16 18:00:30 2023Send message: Process 7872's time is Mon Jan 16 18:00:31 2023Send message: Process 7872's time is Mon Jan 16 18:00:32 2023
[]$ ./write_fifoI am 7872 processSend message: Process 7872's time is Mon Jan 16 18:00:23 2023Send message: Process 7872's time is Mon Jan 16 18:00:24 2023Send message: Process 7872's time is Mon Jan 16 18:00:25 2023Send message: Process 7872's time is Mon Jan 16 18:00:26 2023Send message: Process 7872's time is Mon Jan 16 18:00:27 2023Send message: Process 7872's time is Mon Jan 16 18:00:28 2023Send message: Process 7872's time is Mon Jan 16 18:00:29 2023Send message: Process 7872's time is Mon Jan 16 18:00:30 2023Send message: Process 7872's time is Mon Jan 16 18:00:31 2023Send message: Process 7872's time is Mon Jan 16 18:00:32 2023
上面的例子可以擴(kuò)展成 客戶端進(jìn)程—服務(wù)端進(jìn)程通信的實(shí)例,write_fifo的作用類似于客戶端,可以打開多個(gè)客戶端向一個(gè)服務(wù)器發(fā)送請求信息,read_fifo類似于服務(wù)器,它適時(shí)監(jiān)控著FIFO的讀端,當(dāng)有數(shù)據(jù)時(shí),讀出并進(jìn)行處理,但是有一個(gè)關(guān)鍵的問題是,每一個(gè)客戶端必須預(yù)先知道服務(wù)器提供的FIFO接口,下圖顯示了這樣的操作:

三、消息隊(duì)列
消息隊(duì)列,是消息的鏈接表,存放在內(nèi)核中。一個(gè)消息隊(duì)列由一個(gè)標(biāo)識符(即隊(duì)列ID)來標(biāo)識。
1、特點(diǎn)
- 消息隊(duì)列是面向記錄的,其中的消息具有特定的格式以及特定的優(yōu)先級。
- 消息隊(duì)列獨(dú)立于發(fā)送與接收進(jìn)程。進(jìn)程終止時(shí),消息隊(duì)列及其內(nèi)容并不會被刪除。
- 消息隊(duì)列可以實(shí)現(xiàn)消息的隨機(jī)查詢,消息不一定要以先進(jìn)先出的次序讀取,也可以按消息的類型讀取。
2、原型
// 創(chuàng)建或打開消息隊(duì)列:成功返回隊(duì)列ID,失敗返回-1intmsgget(key_t key, int flag);// 添加消息:成功返回0,失敗返回-1intmsgsnd(int msqid, constvoid ptr, size_t size, int flag);// 讀取消息:成功返回消息數(shù)據(jù)的長度,失敗返回-1intmsgrcv(int msqid, void* ptr, size_t size, long type, int flag);// 控制消息隊(duì)列:成功返回0, 失敗返回-1intmsgctl(int msqid, int cmd, struct msqid_ds * buf);
在以下兩種情況下,msgget將創(chuàng)建一個(gè)新的消息隊(duì)列:
- 如果沒有與鍵值key相對應(yīng)的消息隊(duì)列,并且flag中包含了
IPC_CREAT標(biāo)志位。 - key參數(shù)為IPC_PRIVATE。
函數(shù)msgrcv在讀取消息隊(duì)列時(shí),type參數(shù)有下面幾種情況:
- type == 0,返回隊(duì)列中的第一個(gè)消息;
- type > 0,返回隊(duì)列中消息類型為 type 的第一個(gè)消息;
- type < 0,返回隊(duì)列中消息類型值小于或等于 type 絕對值的消息,如果有多個(gè),則取類型值最小的消息。
可以看出,type值非 0 時(shí)用于以非先進(jìn)先出次序讀消息。也可以把 type 看做優(yōu)先級的權(quán)值。(其他的參數(shù)解釋,請自行Google之)
-
Linux
+關(guān)注
關(guān)注
88文章
11594瀏覽量
217419 -
IPC
+關(guān)注
關(guān)注
3文章
374瀏覽量
54336 -
進(jìn)程間通信
+關(guān)注
關(guān)注
0文章
16瀏覽量
2575
發(fā)布評論請先 登錄
Linux下進(jìn)程間如何實(shí)現(xiàn)共享內(nèi)存通信
Linux下進(jìn)程間通信
Linux現(xiàn)有的所有進(jìn)程間IPC方式
哪些方式可以實(shí)現(xiàn)Linux系統(tǒng)下的進(jìn)程間通信
Linux進(jìn)程間通信方式——管道
Linux進(jìn)程間的五種通信方式介紹 1

Linux進(jìn)程間的五種通信方式介紹 2
評論