system function return value __ function

Source: Internet
Author: User
Cases: [CPP]View Plain Copy status = System ("./");
1, first unified two saying: (1) System return value: Refers to the return value after invoking the system function, for example, the status in the previous example is the system return value (2) Shell return value: The return value of the shell command called by system, for example, The value returned in is the shell return value.
2, how to correctly determine whether correctly executed. Judge only whether the status is ==0. Or is the status only!=-1?
are wrong.
3. Description of System in man
Return VALUE
The value returned is-1 on error (e.g. Fork () failed), and the return
Status of the command otherwise. This latter return status is in the
Format specified in wait (2). Thus, the exit code of the command would
Be Wexitstatus (status). In Case/bin/sh could is not executed, the
Exit status would be is that of the command that does exit (127). You look dizzy.
The processing of the return value by the system function involves 3 stages: Phase 1: Preparing for the creation of a subprocess. If it fails, return-1. Phase 2: Call/bin/sh to pull up the shell script, and if pull fails or the shell does not finish properly (see note 1), the reason value is written to the low 8~15 bit of the status. The system's man only says it will write a value of 127, but the measured findings also write a 126 equivalent. Phase 3: If the shell script finishes properly, fill the shell return value into the low 8~15 bit of status. Note 1: As long as you can call to/bin/sh, and the execution of the shell process is not interrupted by other signals, the normal end. For example: No matter what reason value returned in the shell script, is 0 or not 0, is the normal execution end. Even if the shell script does not exist or does not have execute permissions, it is the normal execution end. This is an abnormally close case if the shell script is forced to kill during execution.
How do you determine if the shell script is finished properly in phase 2? The system provides macros: wifexited (status). If wifexited (status) is true, the normal end is indicated. How to get the shell return value in Phase 3. You can do this directly by moving the 8bit to the right, but it is safe to use a system-supplied macro: wexitstatus (status).

Since we generally judge whether this script executes correctly through the return value in the shell script, if 0 is successfully returned, the failure returns a positive number. So in summary, the way to judge whether a system function calls a shell script to end normally is the following 3 conditions: (1)-1!= status (2) wifexited (status) is True (3) 0 = = Wexitstatus (status)
Note: According to the above analysis, when the shell script does not exist, there is no execution permissions and other scenarios, the first 2 conditions will still be set up, at this time Wexitstatus (status) is 127,126 values. Therefore, we cannot define a value of 127,126 as a return in a shell script, or we cannot distinguish between the return value of the shell or the reason for calling the shell script exception. The return value in the shell script is better than 1 to start incrementing.
The sound code to determine the end of the shell script execution is as follows:
[CPP]  View plain copy #include  <stdio.h>   #include  <stdlib.h>   #include  <sys/wait.h>   #include  <sys/types.h>      int main ()    {       pid_t status;              status = system ("./");          if   ( -1 == status)        {            printf ("system error!");        }       else        {           printf ("exit status value =  [0x%x]\n ",  status);              if  (wifexited (status))            {               if   (0 == wexitstatus (status))                 {                    printf ("run shell script successfully.\n");                }                else                {                    printf ("run shell script fail, script exit code: %d\n",  Wexitstatus (status));                           }           else            {                printf ("exit status = [%d]\n",  wexitstatus (status));            }       }           return 0;  }  

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