开发者

is there a way to run any exec family function as a thread in c

I think the question is self-e开发者_开发问答xplanatory.

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

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


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.


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".


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.

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.


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.

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.

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.


When any thread calls one of the exec() functions, the calling program is completely replaced. All threads, except the one that called exec(), vanish immediately. None of the threads executes destructors for thread-specific data or calls cleanup handlers. All of the (process-private) mutexes and condition variables belonging to the process also disappear. After an exec(), the thread ID of the remaining thread is unspecified. Michael KerrisK "The Linux Programming Interface"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜