Linux process 6: system function, better than exec (commonly used)

system function

system function:

Name:
System: execute a shell oommand------执行一个shell指令
#include <stdlib.h> 
int system(const char *command);//参数是指针/字符串

The return value of the System() function is as follows: if it
succeeds, it returns the status value of the process;
when sh cannot be executed, it returns 127; if it
fails, it returns -1;

The system() function is powerful, and many people use it but know little about its principle

The following is the source code of the linux version system function:

#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,NULL);//sh -c ./cmdstring====./cmdstring
        -exit(127); //子进程正常执行则不会执行此语句
    }else{
    
    
            while(waitpid(pid, &status, 0) < 0){
    
    
                if(errno != EINTER){
    
    
                    status = -1;
                    break;
                }
            }
        }
        return status;
}

The system() function calls /bin/sh to execute the command specified by the parameter.
/bin/sh is generally a soft connection and points to a specific shell. For
example, the bash -c option tells the shell to read commands from the string command;
During the execution of the command, SIGCHLD is blocked,
like saying: hi, the kernel, will this not send me the SIGCHLD signal, wait
until I am finished; during the execution of the command, SIGINT and SIGQUIT are ignored, meaning The process does not act after receiving these two signals.

Return value:
If the system() function fails when calling /bin/sh, it will return 127. For
other reasons, it will return -1 (fork failed and the parent process will return).
If the parameter string is a null pointer (null), it will return a non-zero value, generally set to 1.
If the call is successful, the return value after the shell command (exit, return) is finally returned,
but this return value may also be 127, so finally you can check errno to determine the success of the execution

Note

When writing programs with SUID/SGID permissions, please do not use system(), it will inherit environment variables, which will cause system security problems.

Case demonstration:

When the parent process detects that the input is 1, create a child process to change the field value of the configuration file

 #include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
    
    
 pid_t pid;
 int data=10;
 while(1)
 {
    
    
  printf("请输入数据:\n");
  scanf("%d",&data);
  if(data == 1)
  {
    
    
   pid=fork();
   if(pid>0)
   {
    
    
    wait(NULL);//防止变成僵尸进程
   }
   if(pid==0)
   {
    
    
    //execl("./changdata","changdata","config.txt",NULL);//执行别的
    system("./changdata config.txt",);//系统下执行的,可以直接敲打
    exit(0);
   }
  }
  else
  {
    
    
   printf("神码也不做\n");
  }
 }
 return 0;
}

Need to add a changdata and config.txt
System System () can replace exec, simple and crude System ("ps");
Difference: System() returns to the source program after execution, and continues with the following code

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108998724