Using the system () function under Linux must be cautious __oracle

Source: Internet
Author: User
Reproduced from: http://my.oschina.net/renhc/blog/53580
LinuxTry to avoid using system. Once, it was tortured by the system () function because the system () function was not well understood. Simply knowing how to execute a system command with this function is far from enough, its return value, the return value of the command it executes, and how the command execution failed, is the point. Because of the risk of this function is more, so discard the need to switch to other methods. Let's not say what I'm doing here, but I have to understand the system () function, because there are still a lot of people using the system () function, sometimes you have to face it.
Let's look at a brief introduction to the System () function:
1 #include <stdlib.h>
2 int system (const char *command);

System () executes a command specified in command by CALLING/BIN/SH-C command, and returns after the command has been COM Pleted. During execution of the command, SIGCHLD would be blocked, and SIGINT and Sigquit would be ignored. 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 is to tell the shell to read the command from the string command; SIGCHLD is blocked, like saying: Hi, the kernel, this will not send me a sigchld signal, and so I am busy to say, during the execution of the command, SIGINT and Sigquit were ignored, meaning that the process received the two signals without any action.
Let's take a look at the return value of the system () function: The value returned is-1 on error (e.g. fork (2) failed), and returns the command Otherwis E. This latter return status is in the format specified in Wait (2). Thus, the exit code of the command'll be Wexitstatus (status). In Case/bin/sh could is not executed, the exit status would be is that's of command that does exit (127). If the value of command is NULL, System () returns nonzero if the shell is available, and zero if not. For a better understanding of the system () function return value, you need to understand its execution, in fact the system () function performs three steps: 1.fork a subprocess; 2. Call the EXEC function in a subprocess to execute command; 3. Call wait in the parent process to await the completion of the child process. For fork failure, the system () function returns-1. Returns the value returned by the command through exit or return if exec execution succeeds, and the command completes successfully. (Note that command execution does not represent successful execution, such as command: "rm debuglog.txt", which executes smoothly regardless of whether the file exists or not) if Exec fails, the command does not execute smoothly, such as by a signal interruption, or the command command does not exist at all, the system () function returns 127. If command is NULL, the system () function returns a value other than 0, typically 1.
Look at the source code of the system () after reading these, I want to be sure that someone to the system () function return value is still not clear, see the source most clearly, the following gives a system () function implementation:

01 int system (const char * cmdstring)
02 {
03

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.