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.

650) this.width=650; "class=" Size-full wp-image-33405 aligncenter "src=" http://www.linuxprobe.com/wp-content/ Uploads/2016/09/linux-crond-1.jpg "alt=" linux-crond-1 "height=" 384 "width="/>

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.

650) this.width=650; "class=" Alignnone size-full wp-image-14715 "src=" http://www.linuxprobe.com/wp-content/uploads/ 2016/05/systemd.gif "alt=" SYSTEMD "height=" 639 "width="/>

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 failure Returns 127, note that EXEC only returns to the current process if it fails,   The         process doesn't exist, ~~    }    else . Parent Process     {&nbsP;       while (Waitpid (pid, &status, 0)  < 0)         {             if (ERRNO&NBSP;!=&NBSP;EINTR)              {                status  = -1; //returns -1            if Waitpid is interrupted by a signal      break;            }         }    }    return  status; //return status of the child process if the waitpid is successful}

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, even though the system () function can handle null pointers {     return xxx;} Status = system (cmdstring); if (status < 0) {    printf ("cmd: %s\ t error: %s ",  cmdstring, strerror (errno)); //  It is important to export the errno information or       into log    return xxx;} if (wifexited (status)) {    printf ("normal termination, exit status =  %d\n ",  wexitstatus (status))  //obtained cmdstring execution result  }    else if ( Wifsignaled (status) {    printf ("abnormal termination,signal number =%d\n"),  wtermsig (status)  //if the cmdstring is signaled in     , the signal value}else if (wifstopped (status) is obtained. ) {    printf ("process stopped, signal number =%d\n",  WSTOPSIG ( Status),  //if Cmdstring is suspended by a signal      line, get signal value} 

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).

Originally from: http://www.linuxprobe.com/linux-system-constructors.html


This article is from the "Linux self-scholar" blog, so be sure to keep this source http://luckyone.blog.51cto.com/7263520/1859067

A summary of the system () functions under Linux

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.