Process Control of Embedded Linux C Programming (2)

Posted by andylyon87 on Sun, 30 Jun 2019 21:14:07 +0200

Following what was written in the previous section, we first create a child process through fork() and vfork() functions, which execute the same code as the parent process. Usually a process is created in order to perform different operations from the parent process and realize unused functions. Exc () function family is introduced incorrectly.
1. Exc() function family:
1. Functions with six exec s at the beginning
  int execl(const char *path,const char *arg,...);
  int execlp(const char *file,const char *arg,...);
  int execle(const char *path,const char *arg,...,char *const envp[]);
  int execv(const char *path,const char *argv[]);
  int execve(const char *path,const char *argv[],char *const envp[]);
  int execvp(const char *file,const char *argv[]);
These functions are defined in function libraries, including header files <sys/types.h> and <unistd.h> before use, and an external global variable extern char** environ is defined when predefined.
Its function is to define this command and execute system programs in the current directory, just like vim.
The functions in exec() function family all realize the function of replacing data, code and stack segments of subprocesses. If the call is successful, the new program is loaded without return value. Return - 1 if an error occurs.
2. How to remember the difference between exec functions:
(1) Function name with p: representing the absolute path of the file (or relative path), when the function with p, you can not write the relative path of the file, just write the file name.
(2) Function name with l: means that every command-line parameter of the new program is passed to it as a parameter, the number of parameters is variable, and finally a NULL parameter is input to indicate the end of the parameter input.
(3) Function name with v: means that this kind of function supports the use of parameter arrays, and the last pointer in the array also needs to input NULL parameters as the closing flag, similar to the main() function's formal parameter argv [].

(4) Function names end with e: This class of functions means that a new list of function variables can be passed to it.

	Examples: execve function
	//new2.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
extern char **environ;
int main(void)
{
	puts("welcome to mrsoft");
	return 0;
}
//execve.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
extern char **environ;
int main(int argc,char* argv[])
{
	execve("new",argv,environ);//This calls the executable new, which is the compiled executable of new2.c.
	//At this point, the code segment, data segment and process segment in the process are modified so that the newly created process only executes the code of the newly loaded program.
	puts("Normally this information cannot be output");
}
//Execution result: welcome to mrsoft!

//Modified execve.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
extern char **environ;
int main(int argc,char* argv[])
{
	pid_t pid;
	if((pid=fork())<0)
		printf("create child process failed!\n");
	else if(pid==0)
	    execve("new",argv,environ);
	else 
		puts("Normally output this information!\n");
}

//Running results: Normally output this information!
//			welcome to mrsoft!
//Modify execve.c again
//When called in that process, the PID is still in that process.
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int gvar=2;//global variable
int main(void)
{
	pid_t pid;
	int var=5;//local variable
	printf("process id:%ld\n",(long)getpid());//Output parent process PID
	printf("gvar=%d var=%d\n",gvar,var);
	if((pid=fork())<0)//Failure to create subprocesses
	{
		perror("error!");
		return 1;
	}
	else if(pid==0)//If the following program is executed in a subprocess
	{
		gvar--;
		var++;
		//Output Subprocess PID and Modified Variable Value
		printf("the child process id:%ld,gvar=%d var=%d\n",(long)getpid(),gvar,var);
		return 0;
	}
	else
	{   //Output Parent Process PID and Variable Value
		printf("the parent process id:%ld,gvar=%d var=%d\n",(long)getpid(),gvar,var);
		execve("new",argv,environ);
		return 0;
	}
}
//Modified new.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
extern char **environ;
int main(void)
{
	puts("welcome to mrsoft");
	printf("newPID=%ld\n",(long)getpid());
	return 0;
}
/*Running result: process id:4043
		   gvar=2 var=5
		   the parent process id:4043,gvar=2,var=5 
		   the child process id:4044,gvar=1,var=6 
		   welcome to mrsoft
		   newPID=4043*/

3. Other exec functions

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[])
{
  //A pointer to an array of strings ending in NULL, suitable for exec function parameters containing v
  char *arg[] = {"ls", "-a", NULL};
  
  /**
   * Create subprocesses and call the function execl
   * execl We want to receive a comma-separated parameter list and end with a NULL pointer
   * Including l1 shows that it is necessary to add an executable program as the second parameter. Of course, the third and fourth waiting parameters can be added.
   *          ② Note that you can't add parameters, because it can't add parameters argv, so you can't add parameters.
   * No NULL endings for parameters without l
   */
  if( fork() == 0 )
  {
    // in clild 
    printf( "1------------execl------------\n" );
    if( execl( "/bin/ls", "ls","-a", NULL ) == -1 )
    {
      perror( "execl error " );
      exit(1);
    }
  }
  
  /**
   *Create subprocesses and call function execv, including the characteristics of v 
   *execv I want to receive a pointer to an array of strings ending in NULL.
   *It can also be said that an additional parameter such as:. / A-L is equivalent to ls-l.
   */
  if( fork() == 0 )
  {
    // in child 
    printf("2------------execv------------\n");
	//Note: execv ("/bin/ls", "l", "arg) is incorrectly written. Other parameters should be added.
    if( execv( "/bin/ls",arg) < 0)
    {
      perror("execv error ");
      exit(1);
    }
  }
  /**
   *Create subprocesses and call execlp
   *execlp Similar to execl, there is an additional p
   *l You want to receive a comma-separated parameter list with NULL pointers as closing flags
   *p Is a NULL-terminated string array pointer, the function can DOS PATH variable to find subroutine files
   */
  if( fork() == 0 )
  {
    printf("3------------execlp------------\n");
    if( execlp( "ls", "ls", "-a", NULL ) < 0 )
    {
      perror( "execlp error " );
      exit(1);
    }
  }
  
  /**
   *Create a program and call execvp
   *v Looks for a pointer to receive an array of strings ending in NULL
   *p Is a NULL-terminated string array pointer, the function can DOS PATH variable to find subroutine files
   */
  if( fork() == 0 )
  {
    printf("4------------execvp------------\n");
    if( execvp( "ls", arg ) < 0 )
    {
      perror( "execvp error " );
      exit( 1 );
    }
  }
  
  /**
   *Create subprocesses and call execle
   *l You want to receive a comma-separated parameter list with NULL pointers as closing flags
   *e The function passes the specified parameter envp, which allows the environment of the child process to be changed. Without the suffix e, the child process uses the environment of the current program.
   */
  if( fork() == 0 )
  {
    printf("5------------execle------------\n");
    if( execle("/bin/ls", "ls", "-a", NULL, NULL) == -1 )
    {
      perror("execle error ");
      exit(1);
    }
  }
  
  /**
   *Create subprocesses and call execve
   * v Want to receive a pointer to an array of strings ending in NULL
   * e The function passes the specified parameter envp, which allows the environment of the child process to be changed. Without the suffix e, the child process uses the environment of the current program.
   *Add additional parameters at runtime, such as:. / a -l as execution: ls -l 
   */
  if( fork() == 0 )
  {
    printf("6------------execve-----------\n");
    if( execve( "/bin/ls", arg, NULL ) == 0)
	//A path, absolute path or relative path must be added, otherwise the program to be executed cannot be found.
    {
      perror("execve error ");
      exit(1);
    }
  }
  return EXIT_SUCCESS;
}

Topics: vim