1、命名管道
建立命名管道
(1)命令列方法
mkfifo filename
(2)程式裡建立
int
mkfifo
(const
char
* filename, mode_t mode)
;
int
main()
匿名管道與命名管道的區別命名管道的開啟規則命名管道實現檔案拷貝讀原始檔,寫入命名管道
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define size 1024
#define err_exit(m) dowhile(0)
using
namespace std;
void
mk_fifo()
void
rdfile_wrtofifo()
int outfd =
open
("tp"
, o_wronly);if
(outfd ==-1
)char buf[size]=;
int len;
while
((len =
read
(infd, buf, size)
)>0)
close
(infd)
;close
(outfd);}
intmain()
讀命名管道,寫入目標檔案
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define size 1024
#define err_exit(m) dowhile(0)
using
namespace std;
void
rdfifo_wrtofile()
int outfd =
open
("file.bak"
, o_wronly|o_creat|o_trunc,
0644);
if(outfd ==-1
)char buf[size]=;
int len;
while
((len =
read
(infd, buf, size)
)>0)
close
(infd)
;close
(outfd)
;unlink
("tp");
}int
main()
命名管道實現server/client通訊
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define size 1024
#define err_exit(m) dowhile(0)
using
namespace std;
void
mk_fifo()
}void
serverpipe()
char buf[size]=;
while(1
)else
if(len >0)
else
}close
(rfd);}
intmain()
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define size 1024
#define err_exit(m) dowhile(0)
using
namespace std;
void
clientpipe()
char buf[size]=;
while(1
)else
if(len >0)
}close
(wfd);}
intmain()
2、其他幾種程序間通訊方式system v 共享記憶體共享記憶體是最快的ipc形式,一旦這樣的記憶體對映到共享他的程序的位址空間,這些程序間資料傳遞不再涉及核心,程序不再通過執行進入核心的系統呼叫來傳遞資料
system v 訊息佇列
system v 訊號量
訊號量主要用於同步和互斥
Linux程序間通訊之 管道和命名管道
int pipes 2 pid res pipe pipes pid fork 通過pipe函式建立的這兩個檔案描述符 fd 0 和 fd 1 分別構成管道的兩端,往 fd 1 寫入的資料可以從 fd 0 讀出。並且fd 1 一端只能進行寫操作,fd 0 一端只能進行讀操作,不能反過來使用。要實現雙...
Linux程序間通訊 命名管道
ipc 命名管道 一 原理 管道的乙個不足之處是沒有名字,因此,只能用於具有親緣關係的程序間通訊,在命名管道 named pip 或fifo 提出後,該限制得到了克服。fifo 不同於管道之處 在於它提供乙個路徑名與之關聯,以fifo的檔案形式儲存於檔案系統中 命名管道是乙個裝置檔案,因此,即使程序...
linux程序間通訊(命名管道)
在處理程序間通訊的問題時,匿名管道只能在有親緣關係的程序中進行通訊。如何做到在任意兩個程序之間通訊,這就要用到命名管道。命名管道也被稱為fifo檔案,它是一種特殊型別的檔案,在檔案系統中以檔案的形式存在,它的行為和匿名管道類似。可以使用mkfifo函式來建立乙個命名管道。int mkfifo con...