Summary of system of function usage in C language

  • 2020-04-02 01:56:12
  • OfStack

The system() function is powerful, but many people use it to know little about the principle of the first look at the Linux version of the system function source:


#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
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", cmdstring, (char *)0);
        -exit(127); //The child process will not execute this statement if it executes normally
        }
    else{
           while(waitpid(pid, &status, 0) < 0){
                if(errno != EINTER){
                    status = -1;
                    break;
                }
            }
        }
        return status;
}

Analyze the principle and estimate to be able to understand:    

Returns directly when the command received by system is NULL, otherwise a child process is produced by fork, because fork is in two processes: Parent and child process in return, here to check the returned pid, the fork in the child process returns 0, return the child in the parent pid, the parent use waitpid end waiting for the child, the child process is called execl to start a program in place, his execl ("/bin/sh ", "sh", "- c," cmdstring, 0) (char *) is to call the shell, this is the path of the shell/bin/sh, at the back of the string parameters, The child then becomes a shell process that takes a cmdstring as an argument to the system. In Windows, the shell is command, and you're probably familiar with what the shell does when it receives a command.

To explain the principle of the fork: when A process calls fork, A system to create A new kernel process B, and the memory image of copying A to B in the process of space, because A and B are the same, so how do they know that their is A parent or child, see how the return value of A fork, it also said the fork returns 0 in the child process, in the parent process returns the pid of the child process.

The situation in Windows is similar, is execl changed a smelly and long name, parameter name also changed to see the confusing, I found the prototype in MSDN, to show you:


HINSTANCE   ShellExecute(
               HWND   hwnd,
               LPCTSTR   lpVerb,
               LPCTSTR   lpFile,
               LPCTSTR   lpParameters,
               LPCTSTR   lpDirectory, 
               INT   nShowCmd 
   );   

Usage:
ShellExecute (NULL,     "Open",     "C: \ \ a.r eg."     NULL,     NULL,     SW_SHOWNORMAL);    

You might be surprised that ShellExecute has a parameter called lpDirectory that passes the parent process's environment variable, but not execl in Linux. This is because execl is a function of the compiler (to some extent hiding the specific system implementation).
Int execve(const char * file,const char **argv,const char **envp);

You can see why system () accepts the environment variable of the parent process, but after changing the environment variable with system, the main function of system returns unchanged. The reason can be seen from the implementation of system, which is realized by generating a new process. From my analysis, it can be seen that there is no process communication between the parent process and the child process, and the child process naturally cannot change the environment variables of the parent process.

The system function is used to execute DOS instructions.


#include <stdio.h>
#include <stdlib.h>
xiaoyu()
{
char *a;
int n=0;
FILE *f;
f=fopen("file.bat","w+");
if(f==NULL)
exit(1);
    a="echo"; 
    for(n=65;n<=90;n++)
    fprintf(f,"%s %cn",a,n);
    fclose(f);
    system("file.bat");
}
main()
{
    char *string;
    xiaoyu();
    string="echo C The language of the system function n";
    system(string);
    system("pause");
}

C can use the DOS command, later programming by calling DOS command many operations much easier.


Related articles: