The real system call in Linux-exec function family

exec function family

Generally, in the process of using, a new process is usually created by fork to execute a new and different program immediately, and a new address space is created by calling the exec function family, and the new program is loaded.
Finally, the execution is exited through the exit() system call. Mentioning this function can think of several similar functions, as follows:

  1. exit() Terminate the current process and release the resources it occupied.
  2. _exit() and _Exit() terminate the current process but will not release the occupied resources.
  3. return ends the current function and brings out the return value.

The prototype of the exec function family is as follows:

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, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);

The above function can be simply understood as using a new process to replace the current process.

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

/*
*打印时戳 date+%s
*/
int main(int argc, char *argv[])
{
    
    
	puts("begin");
	
	fflush(NULL);   //刷新缓冲区
	
	//date命令通过which可查到其位置在/bin/date下
	//参数1:新进程路径
	//参数2:新进程的名字
	//参数3:选项,所有选项必须以NULL结尾
	if(execl("/bin/date","date","+%s",NULL) < 0)   
	{
    
    
		perror("execl()");
		exit(0);
	}

	puts("End");

    return 0;
}

The usage of other functions is similar to the usage of the above functions, so I won't repeat them here.

Guess you like

Origin blog.csdn.net/qq_40996117/article/details/107433366