System calls return values to determine whether the command is successfully executed. system calls

Source: Internet
Author: User

System calls return values to determine whether the command is successfully executed. system calls

The system function processes the returned values in three stages:

Phase 1: Create Sub-processes and other preparations. If it fails,-1 is returned. Phase 2: Call/bin/sh to pull the shell script. If the pulling fails or the shell fails to run normally (see note 1), the cause value is written to the lower status 8 ~ 15 bits. System man only indicates that the value 127 is written, but the actual test shows that the value 126 is also written. Phase 3: If the shell script runs normally and ends, fill in the shell return value as low as 8 ~ 15 bits. Note 1: as long as it can be called to/bin/sh and the shell is not interrupted by other signals during execution, it is considered normal. For example, no matter what reason value is returned in the shell script, whether it is 0 or not 0, the execution ends normally. Even if the shell script does not exist or has no execution permission, the execution ends normally. If the shell script is forced to kill during execution, the exception ends. How can I determine whether the shell script is successfully executed in Stage 2? The system provides a macro: WIFEXITED (status ). If WIFEXITED (status) is true, the process ends normally. How to obtain the shell return value in stage 3? You can achieve this directly by shifting 8 bits to the right, but the security method is to use the macro WEXITSTATUS (status) provided by the system ). Generally, in shell scripts, the return value is used to determine whether the script is executed normally. If 0 is returned successfully, a positive number is returned for failure. To sum up, the method to determine whether a system function calls the shell script normally ends should be the following three conditions: (1)-1! = Status (2) WIFEXITED (status) is true (3) 0 = WEXITSTATUS (status). Therefore, the following code checks whether the command is executed normally and returns the result: 1 bool mySystem (const char * command) 2 {3 int status; 4 status = system (command); 5 6 if (-1 = status) 7 {8 printf ("mySystem: system error! "); 9 return false; 10} 11 else 12 {13 if (WIFEXITED (status) 14 {15 if (0 = WEXITSTATUS (status) 16 {17 return true; 18} 19 printf ("mySystem: run shell script fail, script exit code: % d \ n", WEXITSTATUS (status); 20 return false; 21} 22 printf ("mySystem: exit status = [% d] \ n", WEXITSTATUS (status); 23 return false; 24} 25} 26View Code

 

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.