SoFunction
Updated on 2025-03-09

Summary of the role of popen function in Linux

Let's briefly talk about the popen() function

Function definition

#include <>
FILE * popen(const char *command , const char *type );
int pclose(FILE *stream);

Function description

The popen() function creates a pipeline, calls fork() to generate a child process, and executes a shell to run a command to start a process. This pipeline must be closed by the pclose() function, not the fclose() function. The pclose() function closes the standard I/O stream, waits for the command execution to end, and then returns the shell's termination state. If the shell cannot be executed, the termination status returned by pclose() is the same as the shell has already exited.

The type parameter can only be one of read or write, and the resulting return value (standard I/O stream) also has a read-only or write-only type corresponding to the type. If type is "r", the file pointer is connected to the command's standard output; if type is "w", the file pointer is connected to the command's standard input.

The command parameter is a pointer to a shell command string ending with NULL. This line of command will be passed to bin/sh and the -c flag is used, and the shell will execute this command.

The return value of popen() is a standard I/O stream and must be terminated by pclose. As mentioned earlier, this stream is unidirectional (only for read or write). Writing content to this stream is equivalent to writing standard input to the command, and the standard output of the command is the same as the process that calls popen(); in contrast, reading data from the stream is equivalent to reading standard output of the command, and the standard input of the command is the same as the process that calls popen().

Return value

NULL will be returned if the call fork() or pipe() fails, or if the memory cannot be allocated, otherwise the standard I/O stream will be returned. popen() does not set an errno value for memory allocation failure. If an error occurs when calling fork() or pipe(), errno is set to the corresponding error type. If the type parameter is not valid, errno will return EINVAL.

Function prototype:

 #include “”
FILE *popen( const char *command, const char* mode )
    int pclose(FILE *stream_to_close);

Parameter description:

command: is a pointer to a shell command string ending with NULL. This line of command will be passed to bin/sh and the -c flag is used, and the shell will execute this command.

mode: It can only be one of read or write, and the obtained return value (standard I/O stream) also has a read-only or write-only type corresponding to type. If type is "r", the file pointer is connected to the standard output of command; if type is "w", the file pointer is connected to the standard input of command.

effect:

The popen function allows a program to start another program as a new process and can pass data or accept data through it.

Its internal implementation is to call fork to generate a child process, execute a shell, and start a process by running a command. This process must be closed by the pclose() function.

shortcoming:

The bad effect of using popen is that for each popen call, not only should one requested program be started, but one shell is also required, that is, each popen call will start two more processes.

Example:

#include&lt;&gt; 
  #include&lt;&gt; 
  #include&lt;&gt;   
  int main() 
  { 
    FILE *fp=NULL; 
    FILE *fh=NULL; 
    char buff[128]={0};   
   memset(buff,0,sizeof(buff)); 
   fp=popen("ls -l","r");//Read the command ls-l to fp through the same pipeline   fh=fopen("","w+");// Create a writable file   fread(buff,1,127,fp);//Read the data stream of fp into the buff   fwrite(buff,1,127,fh);//Write the buff data into the file pointed to by fh   pclose(fp); 
   fclose(fh);   
   return 0;   
   } 
~        

Operation results:

[lol@localhost practice]$ ls
popen  
[lol@localhost practice]$ cat 
total 12
-rwxrwxr-x. 1 lol lol 5478 May 24 15:39 popen
-rw-rw-r--. 1 lol lol 473 May 24 15:39 
-rw-rw-r--. 1 lol lol  [lol@localhost practice]$ vim 
[lol@localhost practice]$ 

Summarize

The above is the summary of the role of popen function in Linux introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!