shell? ? ? ?
mkfifo? ? ? fifo1 [名稱] 建立一個有名管道
cat < fifo1 ?用cat讀取有名管道fifo1數據
ls > fifo1 ?把ls輸出內容寫到管道中
創建管道
int mkfifo(const char *pathname,mode_t mode);? mode_t權限 0777 0666
if(mkfifo("fifo1",0666) == -1){
printf("error %s\n",strerror(errno));
return -1;
}
讀管道數據
int fd = open("fifo1",O_RONLY);
if(fd == -1){
printf("error %s\n",strerror(errno));
return -1;
}
char buf[1024];
memset(buf,0,sizeof(buf));
read(fd,buf,sizeof(buf)); //read函數是阻塞的直到讀到數據后才返回
printf("%s\n",buf);
向管道寫數據
int fd = open("fifo1",O_WRONLY);
if(fd == -1){
printf("error %s\n",strerror(errno));
return -1;
}
char buf[1024];
memset(buf,0,sizeof(buf));
strcpy(buf,"helloworld");
write(fd,buf,sizeof(buf)); //將buf內容寫入管道
close(fd);
return 0;