[求助]有没有C语言高手啊,关于execv的用法

[求助]有没有C语言高手啊,关于execv的用法,第1张

execv用法介绍:

execv会停止执行当前的进程,并且以progname应用进程替换被停止执行的进程,进程ID没有改变。

如果应用程序正常执行完毕,那么execv是永远不会返回的;当execv在调用进程中返回时,说明这个应用程序应该出错了,此时它的返回值应该是-1,具体的错误代码可以通过全局变量errno查看,还可以通过stderr得到具体的错误描述字符串。

示例如下:

#include <stdlib.h>

#include <stdio.h>

#include <unistd.h>

#include <errno.h>

main(void)

{

  pid_t pid = fork()

   

  if( pid == 0 ) // this is the child process

  {

     execv("/bin/ls", NULL)

     // the program should not reach here, or it means error occurs during execute the ls command.

     printf("command ls is not found, error code: %d(%s)", errno, strerror(errno)

  }

fe函数名: exec...

功 能: 装入并运行其它程序的函数

用 法: int execl(char *pathname, char *arg0, arg1, ..., argn, NULL)

int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,

char *envp[])

int execlp(char *pathname, char *arg0, arg1, .., NULL)

int execple(char *pathname, char *arg0, arg1, ..., NULL,

char *envp[])

int execv(char *pathname, char *argv[])

int execve(char *pathname, char *argv[], char *envp[])

int execvp(char *pathname, char *argv[])

int execvpe(char *pathname, char *argv[], char *envp[])

程序例: /* execv example */

#include <process.h>

#include <stdio.h>

#include <errno.h>void main(int argc, char *argv[])

{

int i printf("Command line arguments:\n")

for (i=0i<argci++)

printf("[%2d] : %s\n", i, argv[i]) printf("About to exec child with arg1 arg2 ...\n")

execv("CHILD.EXE", argv) perror("exec error") exit(1)

}

多看书,多Google,百度,


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7874295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存