execvp返回后,为什么我的程序没有在中断处继续执行? [英] After execvp returns, why doesn't my program pick up where it left off?

查看:160
本文介绍了execvp返回后,为什么我的程序没有在中断处继续执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段像这样的代码作为子线程运行:

I have a block of code like this that runs as a child thread:

if(someVar == 1){
doSomeStuff;

_exit(0)
}
else
   execvp(*(temp->_arguments), temp->_arguments);
printf("I'm done\n");

当我使用someVar == 1运行程序时,我知道_exit(0)调用会杀死我的线程.但是,将其设置为0时,为什么在execvp()调用之后程序不继续运行,而执行printf语句呢?

When I run the program with someVar == 1, I understand that the _exit(0) call kills my thread. However, when it's set to 0, why doesn't the program continue after the execvp() call and do the printf statement?

推荐答案

如果您 exec* (调用exec系列中的任何exec函数),然后将新程序的代码加载到当前进程中,并继续执行其主要功能及其内容.成功执行这些功能后,它们将永远不会返回,因为您的printf不再存在于内存中.

If you exec* (call any exec function from the exec family), then the code of a new program is loaded into your current process and execution continues with its main function and its stuff. On a successful execution of those functions, they will never return because your printf does not exist anymore in memory.

我认为您将exec* fork 混淆了功能.这将拼接出一个新的子进程,该子进程将运行与父进程相同的代码.

I think you confuse exec* with the fork function. That will splice off a new child process which will run the same code as the parent.

如果要创建一个与主线程共享数据和地址空间的新线程,则应使用

If what you want is to create a new thread, that shares data and the address space with the main thread, you should use the pthread_create function. A new process will not share data and you will have to communicate with the other process using other mechanisms, like pipes or shared memory.

这篇关于execvp返回后,为什么我的程序没有在中断处继续执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆