0
点赞
收藏
分享

微信扫一扫

vscode连接服务器报错Bad permissions.

让父子进程来执行不相干的操作

能够替换进程地址空间的代码.text

执行另外的程序,不需要创建额外的的地址空间

当前程序中调用另外一个应用程序

指定执行目录下的程序 int execl(const char *path, const char *arg,/* (char *) NULL */); 

/* path : 要执行程序的路径(最好是绝对路径) 变参 arg : 要执行的程序需要的参数 第一位 arg: 占位 后边的 arg: 命令的参数, 参数写完之后: null 一般执行自己写的程序 */

执行PATH环境变量能够搜索到的程序 int execlp(const char *file, const char *arg, ...

/* file : 执行的命令名字 第一个 arg: 占位 后边的 arg: 命令的参数 参数写完之后: NULL 执行系统自带的程 序:/bin/xx */ ps aux

执行指定路径, 指定环境变量下的程序 int execle(const char *path, const char *arg, ...

int execv(const char *path, char *const argv[]); /* path: = /bin/ps char *argv[] = {"ps,"aux",NULL}

execv("/ps/aux",args); */ int execvp(const char *file, char *const argv[]); int execvpe(const char *file, char *const argv[],

返回值:

如果函数运行成功不返回

如果执行失败,打印错误信息,退出子进程

它们的一般规律如下:

l (list) 命令行参数列表

p (path) 搜素 file 时使用 path 变量

v (vector) 使用命令行参数数组

e (environment) 使用环境变量数组 , 不使用进程原有的环境变量,设置新加载程序运行的环境变量

#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
int i=200;
int main()
{

pid_t pid;
pid = fork();
if (pid>0)
{
        i+=400;
printf("this is father process %d\n",getpid());

printf("i=%d\n",i);
}
else if(pid==0)
{
execl("/bin/ls","ls","-l",NULL);//绝对路径,一般和函数名一样,函数功能,NULL
i+=200;
printf("this is son  process %d,ppid is%d\n",getpid(),getppid());
printf("i=%d\n",i);


}

for(int i=0;i<3;i++)
{

printf("------i=%d\n",i);
}
return 0;
}

能够替换进程地址空间的代码.text段,执行exec的内容,不执行之前的子进程的内容 

举报

相关推荐

0 条评论