A pipe for inter-process communication

    A pipe is a way of interprocess communication in Linux that connects the output of one program directly to the input of another program.

nameless pipe

      1. It can only be used for inter-process communication with affinity (between parent-child processes or between sibling processes)

      2. It is a half-duplex communication mode with fixed read and write ends

            Half-duplex : Data can be transmitted in two directions, but a channel can only be transmitted in one direction at the same time, also known as two-way alternate communication.

      3. It can be regarded as a special file, which can use ordinary read(), write() and other functions. But it is not an ordinary file, it does not belong to any other file system and exists only in memory.

legend

    

Example

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	int pipefd[2];
	int pid;
	if (pipe(pipefd) < 0){
		return -1;
	}
	pid = fork();
	if (pid < 0 ){
		return -1;
	}else if (pid == 0){
		//Close the write end and read data from the pipe
		close(pipefd[1]);
		//Redirect the read end of the pipe to 0
		dup2(pipefd[0], 0);
		execl("/usr/bin/wc", "wc", NULL);
	}
	//Close the read end of the pipe and write data
	close(pipefd[0]);
	// redirect the pipe to 1
	dup2(pipefd[1], 1);
	close(pipefd[1]);
	execl("/bin/ls", "ls","-l", NULL);
	close(1);
	return 0;
}

    1. Use pipe to create an unnamed pipe

    2. The child process closes the write end and reads data from fd[0]

    3. The parent process closes the read end and writes data from fd[1]

    4. The parent process executes ls, passes the result to the child process to execute the wc command, and prints the final result

named pipe

        1. It is possible to make unrelated processes communicate with each other

        2. The pipe can be specified by a pathname and is visible in the filesystem. After the pipeline is established, the two processes can read and write it as a normal file, which is more convenient to use.

        3. FIFO strictly follows the first-in-first-out rule. Reading of pipes and FIFOs always returns data from the beginning, and writing adds data to the end. File positioning operations such as lseek() are not supported.

legend

    

Example

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

void test()
{
	printf("test\n");
}

int main(int argc, char *argv[])
{
	int fifofd;
	char buff[1024];
	umask(0);
	if (mkfifo("./test.fifo", 0664) < 0){
		if (errno != EEXIST){
			return -1;
		}
	}

	fifofd = open("./test.fifo", O_RDWR);
	if (fifofd < 0){
		return -1;
	}
	while (1){
		memset(buff, 0x00, 1024);
		read(fifofd, buff, 1024);
		if (strncasecmp(buff, "test", 4) == 0){
			test();
		}
	}
	close(fifofd);
	return 0;
}

    1.mkfifo creates a pipeline

    2. Use open to open the pipe file

    3. Start reading and writing to the pipe file

    4. By writing data to the pipeline file externally, it is possible to call the test function inside the program to achieve the specified function

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324719999&siteId=291194637