Functions that execute shell commands--system (), Popen ()

Source: Internet
Author: User

1, file* popen (const char* cmd,const char* type);

int Pclose (file* stream);

Popen () function fork () A child process that creates a pipeline for parent-child interprocess communication, which either reads from the pipe or writes to the pipe, executes a shell to run the command to open a process

Compared to the system () is also the use of simple, popen () return only two values, successfully returned the status of the child process, failed to return 1



2, int system (const char* CMD);

Handles the details of fork (), Execl (), Waitpid (), and some signals


1. This library function uses fork () to create a sub-process;

2. The child process calls/bin/sh-c cmd to execute the specified parameter command (/bin/sh is typically a soft connection, pointing to a specific shell, such as the BASH,-C option tells the shell to read the command from string cmd), and then returns the call to the original process after execution;

3. The parent process calls Waitpid to wait for the child process to end.

SIGCHLD will be blocked while executing the command, and SIGINT and Sigquit will be ignored in the process calling system ().


return value:

If CMD is null, returns non 0, typically 1;

If the fork () fails, i.e. the child process cannot be created, return-1;

If the shell cannot be replaced in a child process, that is, execl () fails, returns 127;

If all system calls succeed, the child process executes the cmd command, but the cmd command does not necessarily perform successfully, returning the value returned by CMD by exit or return;


System () Source code:

int system (const char * cmdstring) {pid_t pid; int status; if (cmdstring = = NULL) {return (1);//If cmdstring is empty, return non 0 value, typically 1 } if ((PID = fork ()) <0) {status =-1;//fork failed, return 1} else if (PID = = 0) {execl ("/bin/sh", "sh", "-C", Cmdstring, (ch AR *) 0); _exit (127);  exec execution failed to return 127, note that EXEC only returns the current process if it fails, and if successful, the current process does not exist ~ ~} else//parent process {while (Waitpid (PID, &status, 0) < 0) {if (errno ! = eintr) {status =-1;//If waitpid is interrupted by signal, 1 break is returned;}} } return status; Returns the return status of the child process if the Waitpid succeeds}

System () Inherits environment variables and does not use this function when writing programs with Suid/sgid permissions

It is recommended that system () be used only to execute shell commands

It is recommended that you monitor the errno value after the system () function finishes executing

It is recommended to use Popen () instead of system ()



Http://blog.sina.com.cn/s/blog_8043547601017qk0.html


Finish

This article from "Zero Egg" blog, declined reprint!

Functions that execute shell commands--system (), Popen ()

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.