ipc通訊之管道

2021-09-25 01:43:31 字數 1724 閱讀 6293

首先:

一、無名管道pipe:

1,沒有名字的

2,半雙工  //讀寫不能同時進行

3,通過直系親屬訪問繼承

4,管道缺省會阻塞

5,不能用lseek定位

6,操作沒有原子性

示例** :

#include

#include

#include

#include

#include

#include

#include

void signal_hand(int signum)

int main(void)

printf("buffer=%s\n", buffer);

exit(exit_success);

}close(pipe_fd[0]);

while(1)

}return 0;

}二、有名管道fifo:(檔案)

1,有名字

2,全雙工  //可以同時進行讀寫

3,可以沒有親屬關係

4,管道缺省會阻塞

5,不能用lseek定位

6,操作有原子性 // 當有乙個程序對其進行操作是其他程序不能對其操作

示例**:

有名管道要先建立乙個管道檔案(ps:window下不支援管道檔案,所以不要在ubuntu的共享目錄下建立)

fifo寫端.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define    path    "/home/myfifo"

int main(void)

printf("create success\n");

}int fifo_fd;        //存放open函式的返回值

fifo_fd = open(path, o_rdwr);

if(fifo_fd == -1)

char buffer[100];  //設定管道一次可以寫的大小

bzero(buffer, sizeof(buffer));

while(1)

close(fifo_fd);

return 0;

}fifo讀端.c   

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define    path    "/home/gecmyfifo"

int main(void)

printf("create success\n");

}int fifo_fd;        //存放open函式的返回值

fifo_fd = open(path, o_rdwr);

if(fifo_fd == -1)

char buffer[100];  //設定管道一次可以寫的大小

bzero(buffer, sizeof(buffer));

while(1)

close(fifo_fd);

return 0;

}

IPC(程序間通訊)之管道詳解

linux和windows中都有 1.管道一次通訊四次資料拷貝 由使用者空間的buf中將資料拷貝到核心中 核心將資料拷貝到記憶體中 記憶體到核心 核心到使用者空間的buf。而共享記憶體則只拷貝兩次資料 使用者空間到記憶體 記憶體到使用者空間。2.管道用迴圈佇列實現,連續傳送資料可以不限大小。共享記憶...

程序間通訊 IPC 管道

管道是unix ipc最古老的形式,所有unix系統都提供此通訊機制。管道的兩種限制 1 半雙工,資料單方向流動。2只能用於具有公共祖先的程序之間。需要雙方通訊時,需要建立起兩個管道。例外 流管道沒有限制1,fifo和命名管道沒有限制2。管道是乙個檔案,但它不屬於某種檔案系統,而是 單獨構成一種檔案...

程序間通訊(IPC) 管道

二 fifo 命名管道 參考管道是一種最基本的ipc機制,作用於有血緣關係的程序之間,完成資料傳遞,呼叫pipe系統函式即可建立乙個管理。有如下性質 1.其本質是乙個偽檔案 實為核心緩衝區 所謂的偽檔案是指不佔磁碟空間大小的檔案,linux的檔案除了 l d,其它的s,b,c,p等都是偽檔案。2.由...