《王保明老师----Linux开发学习笔记》------讲09:进程 -----vfork()+execve()拉起一个可执行程序

#include<stdio.h>


#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<signal.h>


int main()
{
printf("hello ....\n");
exit(0);
}




/*
现在我用vfork()函数来拉起 hello 这个进程。
程序名为:vim   dm06.c


*/


#include<stdio.h>


#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<signal.h>


int main()
{
pid_t pid;
printf("before fork pid:%d \n",getpid());
int abc=10;
int ret;

pid=vfork();
if(pid==-1)
{
perror("title");
return -1;
}
if(pid>0)
{
printf("parent:pid :%d \n",getpid());

}
else
if(pid==0)
{
printf("child:%d ,parent:%d \n",getpid(),getppid());
//把自己写的应用程序拉起来。
ret= execve("/home/code/hello",NULL,NULL);// 要深刻明白这一句话的意思???
//或者写成:
//通过一个应用程序来执行命令
char *const argv[]={"ls","-1",NULL};
ret=execve("/bin/ls",argv,NULL);



if(ret==-1)
{
perror("execve:");

}
/*
利用execv()函数来拉起来一个我们在上面已经编译好的可执行程序 hello
所以,我们看这个整个过程的具体步骤和内部机制:
1.在我们运行dm06程序的时候,走到vfork()调用时,vfork()会创建一个空进程
2.之后,当执行execv()函数的时候,execv()函数就会自动的把指定的hello可执行文件,加载到刚才新创建的空进程的进程空间中。
3.
*/

printf("execve 测试有没有执行\n");



exit(0);

}

printf("fork after....\n");
return 0;


}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值