1. Establishing a pipeline of data flows between two cities
2. Can be unidirectional or bidirectional
3. Similar to files, but when the data is read out, there is no information in the pipeline.
4. Anonymous half-duplex pipe:
ls | grep *, the output of LS is the input of grep, the anonymous half-duplex pipeline is just the resource of the system, but there is no real name, it is impossible to see the pipeline in the file system as any file
The process ends up being purged by the system
5.
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fd[2];
if(pipe(fd)==-1)
{
perror("create pipe fail");
exit(-1);
}
char wbuf[] = "Hello,I am pipe!";
if(write(fd[1],wbuf,sizeof(wbuf))==-1)
{
perror("write fail");
}
char rbuf[sizeof(wbuf)];
if(read(fd[0],rbuf,sizeof(wbuf))==-1)
{
perror("read fail");
}
puts(rbuf);
close(fd[0]);
close(fd[1]);
return 0;
}
Note: Two file descriptors are not associated with any known file, there is no actual file
Pipe function Prototype:
#inlcude<unistd.h>
int pipe(int fd[2])
成功返回0,反之为-1
Anonymous pipe limit:
1. Single direction transmission information, fd[1] can only be write end, fd[0] can only be read end
2. Anonymous pipelines apply only to blood-related processes, such as parent-child processes and sibling processes
3. When the pipe read end is closed and the writing end is still written, the sigpipe signal is generated and the program is interrupted.
4. When the end of the pipeline is closed and the read is still read, the Read function returns the number of bytes read from the pipe and returns 0 if there is no data in the pipeline, instead returning the read word
Number of knots
6. Pipeline for inter-parent interprocess communication
The parent process sends information to the child process:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
int main()
{
int fd[2];
pipe(fd);
pid_t pid = fork();
char buf[6]= "Hello";
char rbuf[6]={0};
if(pid!=0)
{
close(fd[0]);
write(fd[1],buf,6);
close(fd[1]);
}
else if(pid==0)
{
close(fd[1]);
read(fd[0],rbuf,6);
puts(rbuf);
close(fd[0]);
}
return 0;
}
The child process sends information to the parent process:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
int main()
{
int fd[2];
pipe(fd);
pid_t pid = fork();
char buf[6]= "Hello";
char rbuf[6]={0};
if(pid==0)
{
close(fd[0]);
write(fd[1],buf,6);
close(fd[1]);
}
else if(pid!=0)
{
close(fd[1]);
read(fd[0],rbuf,6);
puts(rbuf);
close(fd[0]);
}
return 0;
}
From for notes (Wiz)
IPC---piping