pipe原型
NAME
pipe, pipe2 - create pipe
SYNOPSIS
#include <unistd.h>
int pipe(int pipefd[2]);
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
int pipe2(int pipefd[2], int flags);
pipe无名管道 学习内容
#include<stdio.h>
#include<unistd.h>
int main()
{
int fd[2];
pid_t pid;
char buff[30];
if(pipe(fd)<0){
printf("create pipe error\n");
}
pid = fork();
if(pid <0){
printf(" fork error \n");
}
else if(pid > 0){
printf("this is a father\n");
close(fd[0]);
write(fd[1],"jianglongjie henshuai",30);
}
else{
printf("this is a child\n");
close(fd[1]);
read(fd[0],buff,30);
printf("read successed\n");
printf("%s\n",buff);
}
return 0;
}
~
pipe部分内容学习代码
FIFO 有名管道 部分内容学习
FIFO有名管道原型
MKFIFO(1) User Commands MKFIFO(1)
NAME
mkfifo - make FIFOs (named pipes)
SYNOPSIS
mkfifo [OPTION]... NAME...
1.建立FIFO有名管道
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h>
int main()
{
int ret=0;
int buf[30];
if(mkfifo("./fifo",0600)&& errno!= EEXIST)
{
printf("create fifo fail\n");
perror("why");
}
int fd = open("./file",O_RDONLY);
printf("open success");
return 0;
}
~
~
~
~
2.FIFO有名管道内容写入
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h>
int main()
{
char *str ="jianglongjiehenshuai write successed";
int fd = open("./file",O_WRONLY);
write(fd,str,strlen(str));
printf("write success\n");
close(fd);
return 0;
}
~
~
~
3.FIFO有名管道内容读取
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h>
int main()
{
char buf[50]= {0};
if(mkfifo("./file",0600)== -1 && errno != EEXIST)
{
printf("mkfifo failed");
perror("why");
}
int fd = open("./file",O_RDONLY);
int nread = read(fd, buf , 50);
printf("read success\n");
printf("read %d byte from fifo :%s \n",nread, buf);
close(fd);
return 0;
}
~
~
~
~
~
效果图