Inter-Process Communication # # Linux # pipe (pipe) - Standard flow conduit pipe

In the inter-process communication # Linux # # pipe (pipe) - Ordinary pipeline pipe , we can easily see that ordinary pipe First simplex, that is only one-way transmission, while the standard flow conduit for anonymous pipes PIPE series of packages. Returns the file stream. But returned file stream can not use the cursor / offset (offset) related functions, such as lseek the like .

Standard flow conduit with a buffer, the following functions:

FILE* popen(char* command ,char* type);

command: pointed end is a null-terminated character string, this string comprising a shell command, and sent to / bin / sh -c parameters to perform, i.e., executed by the shell;

It is a read-write mode, wherein only one way, not read and write simultaneously: type.

  • "R": file pointer connected to the standard output of command
  • "W": file pointer connected to the standard input of command
int pclose(FILE* stream);

stream: To close the file stream.

 

popen function is actually some packaging operations of the pipeline, the work accomplished by the following steps:

  1. Creating a pipeline.
  2. fork a child process.
  3. Close unneeded file descriptors in the parent-child process.
  4. An exec family of functions called.
  5. Perform functions specified in the command.

 

Specific usage is as follows: Run the command in a shell dmesg | grep "WARNING:" |  wc -l, and the result is the current process to read the contents of a file stream f in.

int get_warnings_count(void)
{
	int warnings;
	FILE *f;

	f = popen("dmesg | grep \"WARNING:\" | wc -l", "r");
	fscanf(f, "%d", &warnings);
	fclose(f);

	return warnings;
}

Thus, the standard flow conduit typically seen for some of the operations associated with the commands shell ...

 

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103730980