Linux 管道的通訊

2021-08-20 22:40:16 字數 979 閱讀 2265

utili.h

#ifndef _utili_h_

#define _utili_h_

#include

#include

#include

#include

#include

#include

#include

#define fifo_read "readfifo"

#define fifo_write "writefifo"

#define max_msg_len 256

#endif

client.c

#include"utili.h"

int main()

//server的寫對應client讀

rfd = open(fifo_write, o_rdonly);

if(rfd == -1)

wfd = open(fifo_read, o_wronly);

if(wfd == -1)

while(1)

write(wfd, sendbuf, strlen(sendbuf)+1);

}server.c

#include"utili.h"

int main()

//以只寫方式開啟,會阻塞到有讀方式開啟管道

int wfd = open(fifo_write, o_wronly);

if(wfd == -1)

//以唯讀方式開啟,會阻塞到有寫方式開啟管道

int rfd = open(fifo_read, o_rdonly);

if(rfd == -1)

while(1)

write(wfd, sendbuf, strlen(sendbuf)+1);

read(rfd, recvbuf, max_msg_len);

printf("cli:>%s\n", recvbuf);

}可以實現簡單的通訊

Linux 管道通訊

一 定義 管道是單向的 先進先出的。它將乙個程式的輸入和另乙個程式的輸出連線起來。資料被乙個程序讀出後,將被從管道中刪除。分為無名和有名管道兩種。前者用於父程序和子程序間的通訊,後者用於同一系統的兩個程序間通訊。二 無名管道 int pipe int fd 2 其中,fd 0 用於讀管道,fd 1 ...

Linux管道通訊

現在在linux 中使用較多的程序間通訊方式主要有以下幾種。1 管道 pipe 及有名管道 named pipe 管道可用於具有親緣關係程序間的通訊,有名管道,除具有管道所具有的功能外,它還允許無親緣關係程序間的通訊。2 訊號 signal 訊號是在軟體層次上對中斷機制的一種模擬,它是比較複雜的通訊...

linux 管道通訊

無名管道 1 管道是半雙工的,只能支援資料的單向流動 兩程序間需要通訊時需要建立起兩個管道 2 使用無名管道通訊的程序必須擁有公共祖先程序 pipe 1 標頭檔案 include 2 定義函式 int pipe int filedes 2 3 函式說明 pipe 會建立管道,並將檔案描述詞由引數fi...