Linux anonymous pipeline

Different communication objects and communication methods

1. Pipeline: one is nameless pipeline and the other is famous pipeline

There is no file node in the file system

Unnamed Pipes

Communication principle:

  Pipeline file is a special file, which is implemented by queue.

Creating a file or opening a file in file IO is implemented by the open function, which cannot create a pipeline file. Pipes can only be created with the pipe function.

File open - only ordinary files can be created or opened. It cannot create pipeline files. Pipes can only be created with the pipe function.

Create pipe - pipe

Function form: int pipe(int fd[2]);

 #include <unistd.h>

 int pipe(int pipefd[2]);

Function: create a pipe for system call: unistd.h

Parameter: is the file descriptor obtained. It can be seen that there are two file descriptors: fd[0] and fd[1]. The pipeline has a read end fd[0] for reading and a write end fd[1] for writing. This rule cannot be changed.

Return value: success is 0, error is - 1  

Example 1: use the pipe function.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
        int fd[2];
        int ret;
        ret=pipe(fd);
        if(ret<0)
        {
                printf("creat pipe failure\n");
                return -1;
        }
        printf("creat pipe sucess fd[0]=%d,fd[1]=%d\n",fd[0],fd[1]);
        return 0;
}

The results are as follows:

  Successfully created pipeline, fd[0]=3,fd[1]=4

The pipeline has a read side fd[0] for reading and a write side fd[1] for writing

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
	int fd[2];
	int ret;
	char write_buf[]="hello kiki";
	char read_buf[128]={0};
	ret=pipe(fd);
	if(ret<0)
	{
		printf("creat pipe failure\n");
		return -1;
	}
	printf("creat pipe sucess fd[0]=%d,fd[1]=%d\n",fd[0],fd[1]);

	write(fd[1],write_buf,sizeof(write_buf));
	//start read from pipe
	
	read(fd[0],read_buf,128);
	printf("read_buf=%s\n",read_buf);

	close(fd[0]);
	close(fd[1]);
	return 0;
}

The results are as follows:

be careful:

-The pipeline is created in memory. When the process ends and the space is released, the pipeline does not exist;

-The things in the pipeline are deleted after reading; Queue - after leaving the queue, the contents of the queue are no longer there

-If there is nothing readable in the pipe, it will be blocked.

Example 2: verify read blocking

 
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	int fd[2];
	int ret;
	char write_buf[]="hello kiki";
	char read_buf[128]={0};
	ret=pipe(fd);
	if(ret<0)
	{
		printf("creat pipe failure\n");
		return -1;
	}
	printf("creat pipe sucess fd[0]=%d,fd[1]=%d\n",fd[0],fd[1]);

	write(fd[1],write_buf,sizeof(write_buf));
	//start read from pipe
	
	read(fd[0],read_buf,128);
	printf("read_buf=%s\n",read_buf);

	//second read from pipe
	
	memset(read_buf,0,128);

	read(fd[0],read_buf,128);
	printf("second read after\n ");

	close(fd[0]);
	close(fd[1]);
	return 0;
}

The results are as follows:

 

Example 3: verify write blocking: you can calculate the size of the pipeline opened by the kernel. sixty-five thousand five hundred and thirty-six  

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	int fd[2];
	int ret;
	int i=0;
	char write_buf[]="hello kiki";
	char read_buf[128]={0};
	ret=pipe(fd);
	if(ret<0)
	{
		printf("creat pipe failure\n");
		return -1;
	}
	printf("creat pipe sucess fd[0]=%d,fd[1]=%d\n",fd[0],fd[1]);
	while(i<6500)
	{
		write(fd[1],write_buf,sizeof(write_buf));
		i++;
	}

	printf("write pipe end\n");

	close(fd[0]);
	close(fd[1]);
	return 0;
}

The results are as follows:

  If the write is blocked and the following printf statement will not be executed, it indicates that the pipeline is full

Example 4: process communication

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
{
	pid_t pid;
	int fd[2];
	char process_inter=0;
	int ret=pipe(fd);
	if(ret<0)
	{
		printf("creat pipe failure\n");
		return -1;
	}
	printf("creat pipe sucess\n");
	pid = fork();
	
	if(pid==0)
	{
		int i=0;
		read(fd[0],&process_inter,1);//if pipe empty sleep
		while(process_inter==0);
		for(i=0;i<5;i++)
		{
			printf("this is child process i=%d\n",i);
			usleep(100);
		}
	}
	if(pid>0)
	{
		int i=0;
		for(i=0;i<5;i++)
		{
			printf("this is parent process i=%d\n",i);
			usleep(100);
		}
		process_inter=1;
		sleep(3);
		write(fd[1],&process_inter,1);
	}
	while(1);
	return 0;
}

The results are as follows:

 

Disadvantages of anonymous pipeline: communication between processes that are not parent-child (kinship) cannot be realized.

Tags: Linux

Posted on Thu, 28 Oct 2021 12:08:47 -0400 by nonphixion