A summary of the system () functions under Linux

Source: Internet
Author: User

Guide Once, it was tortured by the system () function, because the system () function was not well understood. You have to understand the system () function here, because sometimes you have to face it.

Let's take a look at a brief introduction to the System () function:
#includeint System (const char *command)

The system () function calls/bin/sh to execute the command specified by the parameter,/bin/sh is typically a soft connection, pointing to a specific shell, such as the BASH,-C option, which tells the Shell to read the command from the String command, and during the command execution, SIGCHLD is blocked, like saying: Hi, kernel, this will not send me sigchld signal, and so on I am busy to say; During the command execution, SIGINT and sigquit are ignored, meaning that the process receives both signals without any action.

look again at the system () function return value:

In order to better understand the system () function return value, you need to understand its execution, actually the system () function performed three steps:

    1. Fork a sub-process;
    2. Call the EXEC function in the child process to execute the command;
    3. Call wait in the parent process to wait for the child process to end. For fork failure, the system () function returns-1. If exec executes successfully, that is, command executes successfully, returns the value returned by command via exit or return. (Note that command smooth execution does not mean execution succeeds, such as command: "rm debuglog.txt", regardless of whether the file does not exist, the command is executed successfully) if Exec fails, that is, command is not executed smoothly, such as by signal interruption, or command commands do not exist at all, the system () function returns 127. If command is NULL, the system () function returns a value other than 0, typically 1.

Take a look at the source code of the system () function

After reading these, I want to be sure that someone to the system () function return value is still unclear, see the source of the clearest, the following gives a system () function implementation:

int system (const char * cmdstring) {    pid_t pid;    int status;    if (cmdstring = = NULL)    {        return (1);//If cmdstring is empty, returns a value other than 0, typically 1    }    if (pid = fork ()) <0)    {        status =-1; Fork failed, return 1    }    else if (pid = = 0)    {        execl ("/bin/sh", "sh", "-C", Cmdstring, (char *) 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 the waitpid is interrupted by a signal, then return-1                break;< c24/>}}} return    status,//If Waitpid succeeds, returns the return state of the child process}

After reading through the simple implementation of the system () function, the return value of the function is clear, so when does the system () function return 0? Returns 0 o'clock only in command commands.

take a look at how to monitor the system () function execution State Here's what I'm doing:
int status;if (NULL = = cmdstring)//If the cmdstring is empty, it will flash back, although the system () function can handle null pointer {    return XXX;} Status = System (cmdstring), if (status < 0) {    printf ("cmd:%s\t Error:%s", Cmdstring, Strerror (errno)); No information output or    log    return XXX;} if (wifexited (status)) {    printf ("Normal termination, exit status =%d\n", Wexitstatus (status));//Get cmdstring execution Results}< C5/>else if (wifsignaled (status)) {    printf ("Abnormal termination,signal number =%d\n", Wtermsig (status));// If the cmdstring is broken by the signal    , get the signal value}else if (wifstopped (status)) {    printf ("Process stopped, signal number =%d\n", Wstopsig (status)); If the cmdstring is paused by    the signal, the signal value is obtained.}

The system () function is easily error-prone, returns too many values, and the return value can easily be confused with the command's return value. It is recommended to use the Popen () function instead of the simple use of the Popen () function to check the data yourself.

The advantage of the Popen () function over the system () function is that the simple, popen () function returns only two values: the status of the child process is returned successfully, and the command's return result is obtained using the wifexited related macro; failure returns-1, We can use the Perro () function or the strerror () function to get useful error information.

This article deals only with the simple use of the system () function, and does not talk about the effects of SIGCHLD, SIGINT, and Sigquit on system () functions, and in fact, this article was written today because the system was used by someone in the project () The function caused a very serious accident. Now, as the system () function executes, an error occurs: "No child Processes". Call My_system () to perform the function of the system function (My_system function is implemented using the Popen () function), test the day, no recurrence of the program suddenly die (before the modification of a continuous loop call system () function test, Every 10 times it causes the program to hang up at least once. Continuous non-stop calls).

This address: http://www.linuxprobe.com/linux-system-constructors.html

A summary of the system () functions under Linux

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.