一. 管道:
1.只能用於具有親緣關係的進程之間的通信
2.半雙工通信模式
3.一種特殊的文件,是一種只存在於內核中的讀寫函數
管道基於文件描述符,管道建立時,有兩個文件描述符:
a. fd[0]: 固定用於讀管道
b. fd[1]: 固定用於寫管道
創建管道:pipe()
一般步驟:1. pipe()創建管道 2. fork()創建子進程 3. 子進程會繼承父進程的管道
關閉管道:1. 逐個關閉文件描述符 2. close()
eg. 父子進程間的管道通信:父子進程對管道分別有自己的讀寫通道,把無關的讀端或寫段關閉。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_DATA_LEN 256
#define DELAY_TIME 1
int main() {
pid_t pid;
char buf[MAX_DATA_LEN];
const char *data="Pipe Test program";
int real_read,real_write;
int pipe_fd[2];
memset((void*)buf,0,sizeof(buf));
if(pipe(pipe_fd)<0){
perror("Pipe create error!\n");
exit(1);
}
if ((pid=fork())<0) {
perror("Fork error!\n");
exit(1);
} else if (pid==0) {
close(pipe_fd[1]);
sleep(DELAY_TIME*3);
if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
printf("Child receive %d bytes from pipe: '%s'.\n",real_read,buf);
}
close(pipe_fd[0]);
exit(0);
} else {
close(pipe_fd[0]);
sleep(DELAY_TIME);
if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
printf("Parent write %d bytes into pipe: '%s'.\n",real_write,data);
}
close(pipe_fd[1]);
waitpid(pid,NULL,0);
exit(0);
}
}
二. 有名管道FIFO
1. 使不相關的兩個進程彼此通信:a. 通過路徑名指出,在文件系統中可見
b. 管道建立後,兩進程可按普通文件一樣對其操作
2. FIFO遵循先進先出規則:a. 對管道讀從開始處返回數據
b. 對管道寫則把數據添加到末尾
c. 不支持如lseek()等文件定位操作
創建有名管道:mkfifo()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#define FIFO "myfifo"
#define BUFF_SIZE 1024
int main(int argc,char* argv[]) {
char buff[BUFF_SIZE];
int real_write;
int fd;
if(argc<=1){
printf("Usage: ./fifo_write string\n");
exit(1);
}
sscanf(argv[1],"%s",buff);
% 測試FIFO是否存在,若不存在,mkfifo一個FIFO
if(access(FIFO,F_OK)==-1){
if((mkfifo(FIFO,0666)<0)&&(errno!=EEXIST)){
printf("Can NOT create fifo file!\n");
exit(1);
}
}
% 調用open以只寫方式打開FIFO,返回文件描述符fd
if((fd=open(FIFO,O_WRONLY))==-1){
printf("Open fifo error!\n");
exit(1);
}
% 調用write將buff寫到文件描述符fd指向的FIFO中
if ((real_write=write(fd,buff,BUFF_SIZE))>0) {
printf("Write into pipe: '%s'.\n",buff);
exit(1);
}
close(fd);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#define FIFO "myfifo"
#define BUFF_SIZE 1024
int main() {
char buff[BUFF_SIZE];
int real_read;
int fd;
%access確定文件或文件夾的訪問權限。即,檢查某個文件的存取方式
%如果指定的存取方式有效,則函數返回0,否則函數返回-1
%若不存在FIFO,則創建一個
if(access(FIFO,F_OK)==-1){
if((mkfifo(FIFO,0666)<0)&&(errno!=EEXIST)){
printf("Can NOT create fifo file!\n");
exit(1);
}
}
%以只讀方式打開FIFO,返回文件描述符fd
if((fd=open(FIFO,O_RDONLY))==-1){
printf("Open fifo error!\n");
exit(1);
}
% 調用read將fd指向的FIFO的內容,讀到buff中,並打印
while(1){
memset(buff,0,BUFF_SIZE);
if ((real_read=read(fd,buff,BUFF_SIZE))>0) {
printf("Read from pipe: '%s'.\n",buff);
}
}
close(fd);
exit(0);
}