2017年7月9日 星期日

Linux IPC 機制 -- named pipe 與 unix domain socket

named pipe 與 unix domain socket 都是 Linux 上的 IPC 機制, 兩個機制都會在檔案系統上建立一個特殊的文件, 有時候可能會不確定這兩種機制的區別, 但其實兩者是有很多不同的地方

names pipe 是單向的傳輸資料, 但是 unix domain socket 可以雙向的傳輸資料, 如果想用 names pipe 雙向傳輸資料, 則需要兩個 names pipe

一個 unix domain socket 可以用來在很多個 process 中做資料傳輸, 一個 server process 可以在一個 unix domain socket 上同時接收很多的 client process, 若是 names pipe 則 server 跟每個 client 之間需要各自的 named pipe file

大致來說 unix domain socket 比 names pipe 擴充性更好, 但是若是簡單的場景中使用 named pipe 會更容易, 因為 unix domain socket 需要用 socket 系列 API 存取, 但存取 named pipe 就跟存取一般檔案一樣沒有分別

沒有留言:

張貼留言