Detailed description of Linux system functions

Source: Internet
Author: User

The system () function is powerful, I know more about the implementation of Linux, the specific analysis of this, the similar in Windows is an unknown solution. OK, first look at the Linux version of the system function source code: #include #include#include#includeintSystemConst Char*cmdstring)  {pid_t pid; intstatus; if(Cmdstring = =NULL) {            return(1); }  if(PID = fork ()) <0) {Status= -1; }  Else if(PID = =0) {execl ("/bin/sh","SH","- C", Cmdstring, (Char*)0); -exit (127);//This statement is not executed if the child process is performing normally    }  Else{         while(Waitpid (PID, &status,0) <0){          if(errno! =einter) {Status= -1;  Break; }        }    }    returnstatus;} First analyze the principle, and then look at the code above you can understand: when the system accepts the command is NULL to return directly, otherwise fork out a child process, because fork in two processes: parent process and child processes are returned, here to check the returned PID, Fork returns 0 in the child process, returns the PID of the child process in the parent process, the parent process waits for the child process to end with Waitpid, and the child process calls Execl to start a program instead of itself, EXECL ("/bin/sh","SH","- C", Cmdstring, (Char*)0) is the calling shell, the path to the shell is/bin/sh, followed by the string is the argument, then the child process becomes a shell process, the shell parameter is cmdstring, is the system accepts parameters.  The shell in Windows is the command, and presumably everyone is familiar with what the shell has done after accepting the command. If you do not understand the above, then I explain the principle of fork: When a process a calls fork, the system kernel creates a new process B, and the memory image of a is copied to the process space of B, because A and B are the same, then how do they know that they are the parent or child process, The return value of the fork knows that it also says that fork returns 0 in the child process, and the PID of the child process is returned in the parent process.          The situation in Windows is similar, that is, EXECL changed a smelly and long name, the name of the parameter also changed to see the dizzy, I found the prototype in MSDN, to show you: HInstance ShellExecute (HWND hwnd, LPCTSTR Lpverb, LPCTSTR lpfile, LPCTSTR lpparameters, LPCTSTR lpdirectory, in   T nshowcmd); Usage is as follows: ShellExecute (NULL,"Open","C:\\a.reg", NULL, NULL, SW_SHOWNORMAL); You might wonder if there's a parameter in ShellExecute that passes the parent process environment variable Lpdirectory,linux EXECL is not, because EXECL is the compiler's function (to some extent, hides the specific system implementation), In Linux it will then generate a call to the Linux system Execve, the prototype is shown below:intExecve (Const Char* File,Const Char**ARGV,Const Char**envp); See here you will understand why system () will accept the environment variables of the parent process, but after changing the environment variable with system, the system returns the main function or not, this is what I repeatedly stressed on the 22 floor. The reason from the implementation of the system can be seen, it is achieved by generating a new process, from my analysis can see that there is no process communication between the parent process and the child process, the child process naturally cannot change the parent process environment variables. I hope the side dishes do not take the TC or the use of the TC library in the other compilers of the system's call results to refute me, this is not a concept, dos too early to die, play Linux. That's it. System (execute shell command) Related function fork,execve,waitpid,popen header file #include<stdlib.h>Defining FunctionsintSystemConst Char*stringfunction Description System () calls fork () to produce a child process to invoke/bin/sh-C string to execute the command represented by the argument string string, which returns the previously invoked process. The SIGCHLD signal is temporarily shelved while the system () is being called, and the SIGINT and sigquit signals are ignored. The return value if system () is called in the/BIN/SH when the failure returns 127, other failure causes return-1。 If the argument string is a null pointer (null), a value other than 0 is returned. If the system () call succeeds, the return value after executing the shell command is returned, but this return value may also be called for System ()/bin/SH failed to return 127, so it would be better to check the errno again to confirm the success of the execution. Additional instructions are written with the suid/do not use System () when Sgid permission programs, System () will inherit environment variables, and the environment variables may cause security problems. The return value of the system () function is as follows: Success returns the status value of the process; Returns 127 when SH cannot execute;-1; http://blog.csdn.net/yankai0219/article/details/6730121

Detailed description of Linux system functions

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.