Linux system functions

Source: Internet
Author: User
Tags call shell

The system () function is powerful, but many people know little about its principles, so there are so many replies above, I think if you know the specific implementation of system, you will not be confused about the functions that the main program cannot express in many compilers. I have a better understanding of the implementation in Linux. I will not go into details about the implementation in windows.

Now, let's look at the source code of the Linux system functions:

Code:

#include
#include
#include
#include

int system(const char * cmdstring)
{
  pid_t pid;
  int status;

  if(cmdstring == NULL){
     
      return (1);
  }

  if((pid = fork())<0){

Status =-1;
}
Else if (pid = 0 ){
Execl ("/bin/sh", "sh", "-c", character string, (char *) 0 );
-Exit (127); // The sub-process will not execute this statement if it is executed normally.
}
Else {
While (waitpid (PID, & status, 0) <0 ){
If (errno! = Einter ){
Status =-1;
Break;
}
}
}
Return status;
}

First, let's analyze the principle and then look at the above Code to understand it:

If the command received by system is null, the system returns directly. Otherwise, fork generates a sub-process, because fork returns both in two processes: parent process and child process. Check the returned PID here, fork returns 0 in the child process and PID of the child process in the parent process. The parent process uses waitpid to wait until the child process ends. The child process calls execl to start a program to replace itself, execl ("/bin/sh", "sh", "-c", character string, (char *) 0) is to call shell, the shell path is/bin/sh, the subsequent strings are all parameters, and then the sub-process becomes a shell process. The shell parameter is the accept string, which is the parameter accepted by system. In Windows, shell is a command. You must be familiar with what shell did after receiving the command.

If you do not understand the above, I will explain the fork principle: When process a calls fork, the system kernel creates a new process B, and copy the memory image of a to the process space of B. Because A and B are the same, how do they know whether they are parent or child processes, you can see the return value of fork. As mentioned above, fork returns 0 in the child process and the PID of the child process in the parent process.

In Windows, the situation is similar, that is, execl is changed to a smelly and long name, and the parameter name is also changed to make people dizzy. I found a prototype in msdn, let's take a look:

HinstanceShellExecute (
HwndHwnd,
LpctstrLpverb,
LpctstrLpfile,
LpctstrLpparameters,
LpctstrLpdirectory,
IntNshowcmd
);

The usage is as follows:
ShellExecute (null,"Open ","C: \ A. Reg ",
Null,Null,Sw_shownormal );

You may wonder that there is a parameter lpdirectory in ShellExecute that is used to pass the environment variable of the parent process, but execl does not exist in Linux, this is because execl is a compiler function (to some extent, hiding the specific system implementation). in Linux, it will then generate a Linux System Call execve. For the prototype, see:
Int execve (const char * file, const char ** argv, const char ** envp );

You will understand why system () accepts the environment variables of the parent process. However, after using system to change the environment variables, the system returns the primary function but does not change, this is what I repeatedly stressed on the 22nd floor. The cause can be seen from the implementation of system that it is implemented by generating a new process. From my analysis, we can see that there is no process communication between the parent process and the child process, child processes cannot change the environment variables of parent processes. I hope that we will not refute me with the call results of TC or the system in other compilers of the TC library. This is not a concept. DOS is too busy to play with Linux. Let's talk about it.

System (execute shell command)
Related functions
Fork, execve, waitpid, popen
Header file
# Include <stdlib. h>
Define functions
INT system (const char * string );
Function Description
System () calls fork () to generate sub-processes. The sub-process calls/bin/sh-C string to execute the command represented by the string parameter, after the command is executed, the original called process is returned. The sigchld signal is temporarily shelved during system () calls, while the SIGINT and sigquit signals are ignored.
Return Value
If system () fails to call/bin/sh, 127 is returned, and-1 is returned for other causes of failure. If the string parameter is a null pointer, a non-zero value is returned. If system () is successfully called, the return value after the shell command is executed is returned. However, the returned value may also be 127 returned when system () fails to call/bin/sh, therefore, it is best to check errno again to confirm the execution is successful.
Additional instructions
Do not use system () when writing programs with SUID/SGID permissions. System () inherits environment variables, which may cause system security problems.

If the command called by system is successfully executed, 0 is returned;
If it is an unrecognized command, a non-zero value is returned. I guess it seems to be its PID;
Others can be tested by yourself!

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.