Popen&pclose Pipeline Operation Shell Command

Source: Internet
Author: User

Popen, Pclose-pipe stream to or from a process

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

int Pclose (FILE *stream);

Describe

The Popen () function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument could specify only reading or writing, not both; The resulting stream is correspondingly read-only or write-only.

The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to/bin/sh using THE-C flag; Interpretation, if any, was performed by the shell. The type argument is a pointer to a null-terminated string Whichi must contain either the letter ' R ' for reading or the LE Tte ' W ' for writing. Since glibc 2.9, this argument can additionally include the letter ' E ', Whichi causes the CLOSE-ON-EXEC flag (fd_cloexec) t O is set on the underlying file descriptor; See the description of the O_CLOEXEC flag in open for resons what this is may usefull.

The return value from Popen () was a normal standard I/O stream in all respects save that it must being closed with pclose () RA Ther than fclose (). Writing to such a stream writes to the standard input of the command; The command ' s standard output is the same as that of the process, which call Popen (), unless this is altered by the command itself. Conversely, reading from a "popened" stream reads the command ' s standard output, and the command's standard input Is the same as, the process the called Popen ().

Note that output popen () streams is fully buffered by default.

The Pclose () function waits for the associated process to terminate and returns the exit status of the Comman as returned by WAIT4.

return value

The Popen () function returns NULL if the fork or pipe calls fail, or if it cannot allocate memory.

The Pclose () function returns-1 if WAIT4 returns an error, or some other error is detected. In the event of a error, these functions set errno to indicate the cause of the error.

BUGS
Since the standard input of a command opened for reading shares it seek offset with the process that called popen (),  If the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can avoided by calling Fflush (3) before Popen ().

Popen&pclose Pipeline Operation Shell Command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.