execv函数linux,linux 中 execve fork system 三个函数的区别

#include

#include

#include

int main()

{

execve("/bin/ls",NULL,NULL);

printf("test!\n");

perror("execve failed!");

return 0

;

}

output:

# ./execve

execve execve.c

从上面的输出可以发现,执行execve语句之后,如果执行成功,则跳转到第三方程序,不再返回。如果我们将"/bin/ls"故意写作,重新编译执行,输出为:

# ./execve

test!

execve failed!: No such file or directory

失败原因放在errno中。

#include

#include

#include

int main()

{

pid_t child ;

int ans ;

printf("1 copy 2 no copy\n");

scanf("%d",&ans);

if(ans ==

1)

{

child = fork();

if(child == -1)

{

perror("fork

error");

exit(1);

}

}

if(child == 0)

{

puts("it is child.....");

printf("child pid = %d\n",getpid());

printf("parent pid = %d\n\n\n",getppid());

exit(0);

}

else

{

puts("it is parent.....");

printf("child pid = %d\n",getpid());

printf("parent pid = %d\n\n\n",getppid());

exit(0);

}

return 0 ;

}

#include

#include

int main()

{

int myret

;

printf("return value of system is  %d\n",myret =

system("ls -l"));

printf("the main process is still alive!\n");

return 0

;

}

output:

# ./system

total 40

-rwxr-xr-x 1 root root 5040 2010-12-14 17:30 execve

-rw-r--r-- 1 root root 179 2010-12-14 17:30

execve.c

-rwxr-xr-x 1 root root 5084 2010-12-14 17:40 system

-rw-r--r-- 1 root root 208 2010-12-14 17:40

system.c

return value of system is 0

the main process is still alive!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值