有名管道 非父子程序間通訊

2021-10-07 22:16:36 字數 1731 閱讀 9611

有名,即檔案系統中存在對應檔案節點,每個檔案節點都有乙個inode號。

open: 普通檔案

mkfifo: 建立管道

socket:建立套接字

mknod:字元裝置檔案

mkdir: 目錄檔案

mkfifo函式:建立管道檔案

函式形式:int mkfifo(const char * filename, mode_t mode);

引數:管道檔名;許可權,檔案許可權仍然和umask有關

返回:成功返回0,失敗返回-1

建立有名管道mkfifo,開啟open,讀read,寫write,關閉close。

非父子程序間通訊

第乙個程序:

#include

"unistd.h"

#include

"stdio.h"

#include

"sys/types.h"

#include

"stdlib.h"

#include

"fcntl.h"

intmain()

printf

("open myfifo sucess\n");

for(i=

0;i<

5;i++

) process_inter=1;

sleep(5

);write

(fd,

&process_inter,1)

;while(1

);return0;

}

執行結果

open myfifo sucess

this is first process i=

0this is first process i=

1this is first process i=

2this is first process i=

3this is first process i=

4

第二個程序:

#include

"unistd.h"

#include

"stdio.h"

#include

"sys/types.h"

#include

"stdlib.h"

#include

"fcntl.h"

intmain()

printf

("open myfifo sucess\n");

read

(fd,

&process_inter,1)

;while

(process_inter ==0)

;for

(i=0

;i<

5;i++

)while(1

);return0;

}

執行結果

open myfifo sucess

this is second process i=

0this is second process i=

1this is second process i=

2this is second process i=

3this is second process i=

4

程序間通訊 有名管道

無名管道,由於沒有名字,只能用於親緣關係的程序間通訊.為了克服這個缺點,提出了有名管道 fifo fifo不同於無名管道之處在於它提供了乙個路徑名與之關聯,以fifo的檔案形式存在於檔案系統中,這樣,即使與fifo的建立程序不存在親緣關係的程序,只要可以訪問該路徑,就能夠彼此通過fifo相互通訊,因...

程序間通訊 有名管道

mkfifo test 這條命令建立了乙個名字為 test 的命名管道。接下來我們用乙個程序向這個管道裡面寫資料,然後有另外乙個程序把裡面的資料讀出來。echo this is a pipe test 寫資料 這個時候管道的內容沒有被讀出的話,那麼這個命令就會一直停在這裡,只有當另外乙個程序把 te...

程序間通訊 管道(有名管道)

前幾天我們簡單的介紹了程序間通訊的一種渠道 管道,我們有說道管道的概念,管道的通訊原理,還提到了管道的兩種型別 有名管道和無名管道,今天我們就來說說有名管道吧 基本概念 有名管道在檔案目錄中有乙個檔案標示 管道檔案 實際不佔據磁碟空間。資料快取在記憶體上 只有使用時記憶體才開闢,由於在記憶體上,因此...