Advanced Programming in UNIX environment-system functions

Source: Internet
Author: User

System Functions

Function: Call fork to generate a sub-process, which is called by the sub-process;/bin/sh-C command to execute the command represented by the command parameter, blocking the current process until the command is killed

The execution is complete.

IntSystem ( Const
Char
* Command );

Because system calls fork, exec, and waitpid in its implementation, there are three return values:

(1) If fork fails or waitpid returns an error other than eintr, system returns-1 and errno sets the error type value.

(2) If exec fails (the shell cannot be executed), the return value is the same as that of exit (127) executed by shell, that is, 127 is returned.

(3) otherwise, all three functions (fork, exec, and waitpid) are successfully executed, and the return value of system is the termination state of shell.

For example:

Status = system (". /test. sh "); 1. Two statements are unified: (1) system return value: the return value after the system function is called. For example, in the above example, status is the system return value (2) Shell return value: the Return Value of the shell command called by system, such as test. sh returns the shell return value. 2. How to correctly determine whether test. Sh is correctly executed? Only determine whether status is = 0? Or, you can only determine whether the status is correct! =-1? All errors! 3. Return Value for System Description in man
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
Format specified in wait (2). Thus, the exit code of the command will
Be wexitstatus (Status). In case/bin/sh cocould not be executed,
Exit status will be that of a command that does exit (127). Are you dizzy? 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 is not interrupted by other signals during shell 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) True (3) 0 = wexitstatus (Status) Note: Based on the above analysis, when the shell script does not exist, there is no execution permission, and so on, the first two conditions will still be true. In this case, wexitstatus (Status) is a numerical value such as 127,126. Therefore, in shell scripts, values such as 127,126 cannot be defined as return values. Otherwise, it cannot be used to identify whether it is the return value of shell or the cause value of the shell script exception. The return value in the shell script should start to increase at 1. The following is a sound code used to determine whether a shell script is successfully executed:

# Include
<Stdio. h>
# Include
<Stdlib. h>
# Include
<Sys/Wait. H>
# Include
<Sys/types. h>

IntMain ()
{
Pid_t status;

Status = system ("./test. Sh ");

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;
}

System Functions

Function: Call fork to generate a sub-process, which is called by the sub-process;/bin/sh-C command to execute the command represented by the command parameter, blocking the current process until the command is killed

The execution is complete.

IntSystem ( Const
Char
* Command );

Because system calls fork, exec, and waitpid in its implementation, there are three return values:

(1) If fork fails or waitpid returns an error other than eintr, system returns-1 and errno sets the error type value.

(2) If exec fails (the shell cannot be executed), the return value is the same as that of exit (127) executed by shell, that is, 127 is returned.

(3) otherwise, all three functions (fork, exec, and waitpid) are successfully executed, and the return value of system is the termination state of shell.

For example:

Status = system (". /test. sh "); 1. Two statements are unified: (1) system return value: the return value after the system function is called. For example, in the above example, status is the system return value (2) Shell return value: the Return Value of the shell command called by system, such as test. sh returns the shell return value. 2. How to correctly determine whether test. Sh is correctly executed? Only determine whether status is = 0? Or, you can only determine whether the status is correct! =-1? All errors! 3. Return Value for System Description in man
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
Format specified in wait (2). Thus, the exit code of the command will
Be wexitstatus (Status). In case/bin/sh cocould not be executed,
Exit status will be that of a command that does exit (127). Are you dizzy? 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 is not interrupted by other signals during shell 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) True (3) 0 = wexitstatus (Status) Note: Based on the above analysis, when the shell script does not exist, there is no execution permission, and so on, the first two conditions will still be true. In this case, wexitstatus (Status) is a numerical value such as 127,126. Therefore, in shell scripts, values such as 127,126 cannot be defined as return values. Otherwise, it cannot be used to identify whether it is the return value of shell or the cause value of the shell script exception. The return value in the shell script should start to increase at 1. The following is a sound code used to determine whether a shell script is successfully executed:

# Include
<Stdio. h>
# Include
<Stdlib. h>
# Include
<Sys/Wait. H>
# Include
<Sys/types. h>

IntMain ()
{
Pid_t status;

Status = system ("./test. Sh ");

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