Detailed analysis of system functions in Linux

Source: Internet
Author: User
Tags call shell
Document directory
  • Detailed analysis of system functions in Linux
Detailed analysis of system functions in Linux

[Size = 2] The system () function is very powerful. 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 first look at the source code of the Linux system function: [Code] # include
# Include
# Include
# Include

INT system (const char * character string)
{
Pid_t PID;
Int status;

If (else string = 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;
} [/Code] first 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.

Reprinted from: http://mcuos.com/archiver/tid-411.html

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.