是否有一种方法可以在c中以线程的形式运行exec族函数

时间:2021-01-01 03:58:04

I think the question is self-explanatory.

我认为这个问题不言自明。

I know exec runs on another process, so if I wanted to ls a directory I would do something like that.

我知道exec在另一个进程上运行,所以如果我想要ls目录,我就会这样做。

int pid;
if((pid = fork()) != -1) {
     if(!pid) {
         execl("/bin/ls", "-a", "-l", (char *) 0);
     } else {
         wait(&status);
     }
} else //error

That's fine if you want to create a process, but what about threads?. I though firs to create the thread and then the exec function but that would make the thread I just created have two process

如果您想要创建一个进程,这没问题,但是线程呢?我虽然先创建了线程,然后创建了exec函数,但这会让我创建的线程有两个进程。

3 个解决方案

#1


6  

This wouldn't make sense. Threads share the same address space as each other (which includes program code); it wouldn't be possible for two separate executables to co-exist in the same process, as they'd simply destroy each other.

这不会是有意义的。线程之间共享相同的地址空间(包括程序代码);两个独立的可执行文件不可能在同一个过程*存,因为它们只会互相破坏。

#2


4  

This is not possible, exec will simply replace the current process with the specified executable, so your original process no longer exists. Also, a process is a higher unit of execution than a thread, so it doesn't make sense to "run a process inside another thread".

这是不可能的,exec将简单地用指定的可执行文件替换当前进程,因此原始进程不再存在。而且,进程比线程的执行单元要高,所以“在另一个线程中运行进程”是没有意义的。

#3


3  

You should step back and tell us the real requirements. You should be asking how to achieve what you want, not how to achieve it in a specific way. By doing the latter, you limit the possibilities, which is rarely wise.

您应该后退一步,告诉我们真正的需求。你应该问的是如何实现你想要的,而不是如何以特定的方式实现。通过后者,你限制了可能性,这很少是明智的。

For what it's worth, that code will work under POSIX, as only the calling thread is suspended until the child finishes.

值得注意的是,该代码将在POSIX下工作,因为只有调用线程被挂起,直到子线程结束。

If you want the subprocess to run as a thread in the current process, that's not going to work. You're trying to intermix two different abstraction levels and, the instant you exec, the current process has an entire new program loaded into it. That won't affect the parent (since you forked) but you can't run ls (or any other program) in a thread of an existing process.

如果希望子进程在当前进程中以线程的形式运行,这是行不通的。您正在尝试混合两个不同的抽象级别,并且,当您执行时,当前进程已经加载了一个完整的新程序。这不会影响父进程(因为您已经放弃了),但是您不能在一个现有进程的线程中运行ls(或任何其他程序)。


Based on your comments:

基于你的评论:

... because my main process has a lot of variables and stuff and I didn't want to duplicate that memory space because of the fork.

…因为我的主进程有很多变量和东西,我不想因为fork而复制那个内存空间。

Modern operating systems won't duplicate everything on a fork, they're rather smarter than that.

现代操作系统不会在叉子上复制所有东西,它们比叉子更聪明。

Linux, for example, will only copy page tables and create a new task structure. All other memory is marked copy-on-write so it can be split only when needed.

例如,Linux只复制页表并创建一个新的任务结构。所有其他内存都被标记为“写时复制”,因此只有在需要时才能对其进行分割。

In addition, Linux has a vfork that won't even duplicate the page tables, at least until you call exec but the parent is suspended until that point (or _exit) to prevent cross-process corruption, and you're very limited in what the child is allowed to do.

此外,Linux有一个vfork,它甚至不会复制页表,至少在您调用exec之前是这样的,但是父类被挂起,直到那个点(或_exit),以防止跨进程损坏,并且您在允许子类做什么方面非常有限。

#1


6  

This wouldn't make sense. Threads share the same address space as each other (which includes program code); it wouldn't be possible for two separate executables to co-exist in the same process, as they'd simply destroy each other.

这不会是有意义的。线程之间共享相同的地址空间(包括程序代码);两个独立的可执行文件不可能在同一个过程*存,因为它们只会互相破坏。

#2


4  

This is not possible, exec will simply replace the current process with the specified executable, so your original process no longer exists. Also, a process is a higher unit of execution than a thread, so it doesn't make sense to "run a process inside another thread".

这是不可能的,exec将简单地用指定的可执行文件替换当前进程,因此原始进程不再存在。而且,进程比线程的执行单元要高,所以“在另一个线程中运行进程”是没有意义的。

#3


3  

You should step back and tell us the real requirements. You should be asking how to achieve what you want, not how to achieve it in a specific way. By doing the latter, you limit the possibilities, which is rarely wise.

您应该后退一步,告诉我们真正的需求。你应该问的是如何实现你想要的,而不是如何以特定的方式实现。通过后者,你限制了可能性,这很少是明智的。

For what it's worth, that code will work under POSIX, as only the calling thread is suspended until the child finishes.

值得注意的是,该代码将在POSIX下工作,因为只有调用线程被挂起,直到子线程结束。

If you want the subprocess to run as a thread in the current process, that's not going to work. You're trying to intermix two different abstraction levels and, the instant you exec, the current process has an entire new program loaded into it. That won't affect the parent (since you forked) but you can't run ls (or any other program) in a thread of an existing process.

如果希望子进程在当前进程中以线程的形式运行,这是行不通的。您正在尝试混合两个不同的抽象级别,并且,当您执行时,当前进程已经加载了一个完整的新程序。这不会影响父进程(因为您已经放弃了),但是您不能在一个现有进程的线程中运行ls(或任何其他程序)。


Based on your comments:

基于你的评论:

... because my main process has a lot of variables and stuff and I didn't want to duplicate that memory space because of the fork.

…因为我的主进程有很多变量和东西,我不想因为fork而复制那个内存空间。

Modern operating systems won't duplicate everything on a fork, they're rather smarter than that.

现代操作系统不会在叉子上复制所有东西,它们比叉子更聪明。

Linux, for example, will only copy page tables and create a new task structure. All other memory is marked copy-on-write so it can be split only when needed.

例如,Linux只复制页表并创建一个新的任务结构。所有其他内存都被标记为“写时复制”,因此只有在需要时才能对其进行分割。

In addition, Linux has a vfork that won't even duplicate the page tables, at least until you call exec but the parent is suspended until that point (or _exit) to prevent cross-process corruption, and you're very limited in what the child is allowed to do.

此外,Linux有一个vfork,它甚至不会复制页表,至少在您调用exec之前是这样的,但是父类被挂起,直到那个点(或_exit),以防止跨进程损坏,并且您在允许子类做什么方面非常有限。