新聞中心
FIFO通信(first in first out)

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、大洼網(wǎng)站維護、網(wǎng)站推廣。
FIFO 有名管道,實現(xiàn)無血緣關(guān)系進程通信。
- 創(chuàng)建一個管道的偽文件
- a.mkfifo testfifo 命令創(chuàng)建
- b.也可以使用函數(shù)int mkfifo(const char *pathname, mode_t mode);
- 內(nèi)核會針對fifo文件開辟一個緩沖區(qū),操作fifo文件,可以操作緩沖區(qū),實現(xiàn)進程間通信–實際上就是文件讀寫
man 3 mkfifo
#include#include int mkfifo(const char *pathname, mode_t mode);
注意事項:
FIFOs
Opening the read or write end of a FIFO blocks until the other end is also opened (by another process or thread). See
fifo(7) for further details.
打開fifo文件時候,read端會阻塞等待write端open,write端同理,也會阻塞等待另外一段打開。
代碼示例:
file_w.c 寫端
#include#include #include #include #include #include #include #include int main(int argc, char *argv[]) { if(argc != 2) { printf("./a.out filename1\n"); return -1; } printf("begin open w\n"); int o_ret = open(argv[1], O_WRONLY); printf("end open w\n"); char buf[256]; int num = 0; while (1) { memset(buf, '\0', sizeof(buf)); sprintf(buf, "xiaoming--%d", num++); printf("strlen(buf) = %d\n", strlen(buf)); write(o_ret, buf, strlen(buf)); sleep(1); } close(o_ret); return 0; }
file_r.c 讀端
#include#include #include #include #include #include #include #include int main(int argc, char *argv[]) { if(argc != 2) { printf("./a.out filename1\n"); return -1; } printf("begin open r\n"); int o_ret = open(argv[1], O_RDONLY); printf("end open r\n"); char buf[256]; int num = 0; while (1) { memset(buf, '\0', sizeof(buf)); read(o_ret, buf, sizeof(buf)); printf("strlen(buf) = %d\n", strlen(buf)); printf("read is%s\n", buf); } close(o_ret); return 0; }
本篇文章到此結(jié)束,如果您有相關(guān)技術(shù)方面疑問可以聯(lián)系我們技術(shù)人員遠(yuǎn)程解決,感謝大家支持本站!
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機、虛擬主機、域名注冊、VPS主機、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
網(wǎng)站題目:Linux進程通信之FIFO的實現(xiàn)
網(wǎng)站地址:http://m.fisionsoft.com.cn/article/cdseccg.html


咨詢
建站咨詢
