進程通訊-管道建立

管道通訊node

管道是單向的、先進先出的,由隊列來實現,它把一個進程的輸出和另外一個進程的輸入鏈接在一塊兒socket

一個進程(寫進程)在管道的尾部寫入數據,另外一個進程(讀進程)從管道的頭部讀出數據函數

管道包括無名管道和有名管道。前者用於父進程和子進程間的通訊,後者可用於運行於同一系統的任意兩個進程間的通訊。spa

 

無名管道3d

無名管道由pipe()函數建立code

int pipe(int fd[2]);//建立管道,爲系統調用:unistd.hblog

建立成功返回0,失敗返回-1繼承

建立兩個文件描述符:fd[0]用於讀管道,fd[1]用於寫管道隊列

注意:進程

管道是建立在內存中的,進程結束,空間釋放,管道就不存在了

管道中的東西,讀完了就刪除了

若是管道沒有東西可讀,就會讀堵塞

關閉管道,close關閉兩個文件描述符

 

 

必須在系統調用fork()前調用pipe(),不然子進程將不會繼承文件描述符(子父各建立了一個管道)

無名管道源代碼

#include <stdio.h> #include<malloc.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>


int main() { int fd[2]; int fd1; int ret; int i; char a[100]; char b[10] = "123456"; ret = pipe(fd);//管道建立必須在fork()函數以前 if (ret < 0) { printf("建立管道失敗\n"); } else { printf("建立管道成功\n"); } fd1 = fork(); if (fd1 == 0)//子進程
 { printf("正在讀取\n"); i = read(fd[0], a, sizeof(a)); printf("已接受%s\n", a); close(fd[0]); } if (fd1 > 0)//父進程
 { write(fd[1], b, sizeof(b)); printf("已發送123456\n"); close(fd[1]); } return 0; }

 

 

有名管道

一、建立這個文件節點,不能夠經過open函數,open函數只能建立普通文件,不能建立特殊文件(管道-mkdifo,套接字-socket,字符設備文件-mknod,塊設備文件-mknod,符號連接文件-ln-s,目錄文件 mkdir)

二、管道文件只有inode號,不佔磁盤塊空間,和套接字、字符設備文件、塊設備文件同樣。普通文件和符號連接文件及目錄文件,不只有inode號,還佔磁盤塊空間

三、mkfifo 用來建立管道文件的節點,沒有在內核中建立管道

只有經過open函數打開這個文件時纔會在內核空間建立管道

mkfifo

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

功能:建立管道文件

參數:管道文件文件名,權限,建立的文件權限仍然和umask有有關係

返回值:建立成功返回0,建立失敗返回-1

 代碼以下,建立3個.c文件

//建立管道節點 1.c
#include <stdio.h> #include<malloc.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>
int main() { int fd; fd = mkfifo("./write_mkfifo",0777); if (fd < 0) { printf("建立管道節點失敗"); return -1; } else printf("建立管道節點成功"); }
//建立一個進程寫管道 write.c
#include <stdio.h> #include<malloc.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>

int main() { int fd; char a[10]="123456"; fd = open("./write_mkfifo", O_WRONLY); if (fd < 0) { printf("打開管道失敗\n"); } else { printf("打開管道成功\n"); } write(fd,a,sizeof(a)); printf("已發送數據到管道\n"); } 
//建立一個進程讀管道 read.c
#include <stdio.h> #include<malloc.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>

int main() { int fd; char b[10]; fd = open("./write_mkfifo", O_RDONLY); if (fd < 0) { printf("打開管道失敗\n"); } else { printf("打開管道成功\n"); } read(fd,b,sizeof(b)); printf("接收數據成功:%s\n",b); close(fd); }

結果以下

相關文章
相關標籤/搜索