通过execve简单的调用,下面我们要调用 “ls”来显示我当前目录下的文件 :

/*
 * execve
 */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
	char *args[] = {"/bin/ls", NULL};

	if(execve ("/bin/ls", args, NULL) == -1)
	{
		perror ("execve");
		exit(EXIT_FAILURE);
	}

	puts("shouldn't get here");
	
	exit (EXIT_SUCCESS);
}

execve接受3个参数:path,  argv 和 envp。 path就是要执行的二进制文件或者脚本的完整路径。argv是要传递给程序的完整参数列表,它一般是执行程序的名字。envp是指向执行execed程序的专门环境指针。

在上面的例子中,没有执行puts语句,为什么呢?     因为exec完全用新进程替换了调用进程,所以调用进程就没有了,不再有能返回的调用进程了。

相关文章:

  • 2021-06-08
  • 2021-09-30
  • 2021-11-26
  • 2021-08-06
  • 2021-08-13
  • 2021-09-29
  • 2021-05-18
  • 2021-07-16
猜你喜欢
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2021-11-29
  • 2021-05-18
  • 2022-02-01
相关资源
相似解决方案