当前位置: 首页 > news >正文

unix高级编程-fork和execve

fork和vfork
vfork是老的实现方法又很多问题
在这里插入图片描述

vfork

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    pid_t pid;printf("befor fork pid: %d\n",getpid());int a = 10;pid = vfork();if(pid == -1){perror("失败!\n");return -1;       }if(pid > 0){printf("parent:pid: %d\n",getpid());    printf("parent:a:%d\n",a);    }else if(pid == 0){    printf("child:%d,parent:%d\n",getpid(),getppid());   printf("child:a:%d\n",a);                         }return 0;}

在这里插入图片描述
可以看到这里是有问题的
vfork常和 execve() 一起使用

就像Python中的os.system(cmd)这个函数,我们可以用这个函数来执行我们的shell脚本,单独的shell命令,或者是调用其他的程序,我们的execve()这个函数就和Python中的os.system函数类似,可以调用其他程序的执行,执行shell命令,,调用脚本等等功能。

int execve(const char *filename, char *const argv[], char *const envp[]); 

execve()执行程序由 filename决定。
filename必须是一个二进制的可执行文件,或者是一个脚本以#!格式开头的解释器参数参数。如果是后者,这个解释器必须是一个可执行的有效的路径名,但是不是脚本本身,它将调用解释器作为文件名。

argv是要调用的程序执行的参数序列,也就是我们要调用的程序需要传入的参数。

envp 同样也是参数序列,一般来说他是一种键值对的形式 key=value. 作为我们是新程序的环境。

注意,argv 和envp都必须以null指针结束。 这个参数向量和我们的环境变量都能够被我们的main函数调用,比如说我们可以定义为下面这个形式:

#include <sys/types.h>
#include <unistd.h>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    pid_t pid;printf("befor fork pid: %d\n",getpid());pid = vfork();if(pid == -1){perror("失败!\n");return -1;       }if(pid > 0){printf("parent:pid: %d\n",getpid());    wait(NULL);  //这里不等子进程会变孤儿进程  }else if(pid == 0){    printf("child:%d,parent:%d\n",getpid(),getppid());   int r = 0;r =  execve("./hello", NULL,NULL);                         if(r == -1){    perror("失败!\n");}exit(0);}return 0;}

hello文件包含的东西

#include <sys/types.h>#include <stdlib.h>
#include <stdio.h>int main(void)
{    printf("你好!");return 0;}

在这里插入图片描述
我们调用系统的命令

#include <sys/types.h>
#include <unistd.h>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    pid_t pid;printf("befor fork pid: %d\n",getpid());pid = vfork();if(pid == -1){perror("失败!\n");return -1;       }if(pid > 0){printf("parent:pid: %d\n",getpid());    wait(NULL);  //这里不等子进程会变孤儿进程  }else if(pid == 0){    printf("child:%d,parent:%d\n",getpid(),getppid());   int r = 0;r =  execve("/bin/ls", NULL,NULL);                         if(r == -1){    perror("失败!\n");}exit(0);}return 0;}

在这里插入图片描述

这里最好还是用fork函数

#include <sys/types.h>
#include <unistd.h>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    pid_t pid;printf("befor fork pid: %d\n",getpid());pid = fork();if(pid == -1){perror("失败!\n");return -1;       }if(pid > 0){printf("parent:pid: %d\n",getpid());    wait(NULL);  //这里不等子进程会变孤儿进程  }else if(pid == 0){    printf("child:%d,parent:%d\n",getpid(),getppid());   int r = 0;r =  execve("/bin/ls", NULL,NULL);                         if(r == -1){    perror("失败!\n");}exit(0);}return 0;}
http://www.lryc.cn/news/16720.html

相关文章:

  • Vue3+Ts+Vite开发插件并发布到npm
  • CAN TP层函数介绍
  • Spring架构篇--2.5 远程通信基础Select 源码篇--window--Select.open()
  • WEB静态交互展示【数据mock】
  • (4)C#传智:分支Switch与循环While(第四天)
  • Stable-Baselines 3 部分源代码解读 2 on_policy_algorithm.py
  • 15. Qt中OPenGL的参数传递问题
  • 注意,这本2区SCI期刊最快18天录用,还差一步录用只因犯了这个错
  • Could not find resource jdbc.properties问题的解决
  • 【面试题】==与equals区别、Hashcode作用、hashcode相同equals()也一定为true吗?泛型特点与好处
  • Flex布局中的flex属性
  • SpringBoot + Ant Design Pro Vue实现动态路由和菜单的前后端分离框架
  • robotframework自动化测试环境搭建
  • 尚硅谷《Redis7》(小白篇)
  • 并非从0开始的c++ day6
  • PMP考前冲刺2.22 | 2023新征程,一举拿证
  • RxJava的订阅过程
  • 【2.22】MySQL、Redis、动态规划
  • 2年手动测试,裸辞后找不到工作怎么办?
  • Leetcode6. N字形变换
  • 将Nginx 核心知识点扒了个底朝天(十)
  • GPU显卡环境配置安装
  • CIMCAI super unmanned intelligent gate container damage detect
  • web概念概述
  • 编译原理笔记(1)绪论
  • MySQL(八)
  • steam搬砖项目,小投入高回报,可放大操作,(内附教学资料)
  • 华为OD机试真题Python实现【最多提取子串数目】真题+解题思路+代码(20222023)
  • day32 多线程(上)
  • 【flink】 各种join类型对比