linux_c_learn/exec/exec.c

39 lines
848 B
C
Raw Permalink Normal View History

2024-02-27 12:28:51 +00:00
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
extern char **environ;
int main(int argc ,char *argv[])
{
char pwd[1024];
getcwd(pwd,1024);
printf("master \n");
printf("path %s\n",pwd);
char path[1024];
snprintf(path,sizeof(path),"%s/app.bin",pwd);
char *exec_arg[]={path,NULL};
char *envp[] = {NULL};
int err = execve(path,exec_arg,envp);
printf("error happen %d\n",err);
return 0;
}
// int main() {
// char *args[] = {"/bin/ls", "-l", NULL};
// char *envp[] = {"PATH=/bin:/usr/bin", NULL};
// execve("/bin/ls", args, envp);
// // 如果 execve 失败,会执行到这里
// perror("execve failed");
// return 1;
// }