这篇文章主要介绍了linux 进程替换(exec函数)实现代码的相关资料,需要的朋友可以参考下
Linux 进程替换(exec函数)实现代码
# include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
pid_t id=fork();
if(id==0)
{
printf("child is running\n");
sleep(1);
char* env[]={"MYENV=/A/B/C/D/AAAA",NULL};
char* argv[]={"ls","-l","-n","-i",NULL};
// execl("/bin/ls","ls","-l","-n","-i",NULL);
// execlp("ls","ls","-l","-n","-i",NULL);
// execv("/bin/ls",argv);
// execvp("ls",argv);
// execle("./myenv","myenv",NULL,env);
execve("./myenv",argv,env);
printf("child id done\n"); //exec成功的话不执行此语句
exit(1);
}
else
{
pid_t ret=waitpid(id, NULL,0);
if(ret>0)
{
printf("father wait success\n"); }
else{
printf("child quit not normal\n");
} } return 0; }下图就是execl execlp execv execvp的结果图

下图是execle execve的结果图

以上就是Linux使用exec函数实现进程替换的代码分享的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号