4

I'm very new to threads, processes, execv, etc. I have researched and found that when you execute an execv, it takes the space of the calling process. I am wondering what happens when you create a thread in main, and then call execv, directly after the thread (not before it finishes but right after the thread is created). I know execv will replace main but does this mean that it will also replace the thread or will the thread be able to execute and complete like normal?

Small example of what I'm asking:

  int main(){
      printf("hello from main!);
      char *buffer = "some data";

    if(pthread_creat(&mythreadpid, NULL, thread1, buffer){
        printf("ERROR!!");
     }

     execv(...) //do execv here

}

void *thread1(void *buffer){
  printf("calling from my thread!");

 //do something else

}

I have tested this and I did experience strange behavior as my thread wouldn't complete so I want to know if this is the reason for it

8
  • execv replaces the whole process, not just one thread. Commented Oct 28, 2016 at 1:17
  • so it will replace my thread created from main AND main? Commented Oct 28, 2016 at 1:17
  • 1
    Yes, all the memory and state of the current process is overwritten by exec, except for the environment and open file descriptors. Commented Oct 28, 2016 at 1:19
  • okay, will this be the same if I were to create a process instead of a thread or will that new process not get replaced? Commented Oct 28, 2016 at 1:20
  • the child process wild execute the new code but not his parent. use fork() in C Commented Oct 28, 2016 at 1:23

1 Answer 1

7

All the exec functions replace the entire process with the program being executed. All threads are destroyed.

If you want to execute another program without affecting the current process, you should use fork() first to create a new process, and call execv() in the child process. See Is it safe to fork from within a thread? for some caveats to be aware of when using fork() in a multi-threaded program.

Sign up to request clarification or add additional context in comments.

6 Comments

Far more state than just environment and open file descriptors are maintained across exec: eg. current working directory; the disposition of ignored signals and signal masks; umask; ptrace state...
@caf Thanks, I had a feeling I was missing some details. I just removed that since it's not really important to this question.
@caf, as I tell in another comment to the question, environment is specified in the call to exec*(2), so it's also replaced. Environment is saved in the virtual space of a process, and the only preserved things are resources like open descriptors, that are not stored in the process virtual address space. The calls that don't ask for a new environment, just use the environment received as parameter to the new exec*(2) call. Only open descriptors that don't have the O_CLOEXEC flag are preserved. The ones that have it will be closed. You also loose all the mmap(2)ed segments.
@LuisColorado The environment is only specified in execve and execle. But all of this is irrelevant to my answer, that's why I took out any mention of what's retained.
@Barmar, I only stated that the environment is mapped on the virtual space of a process, and as such, it cannot be preserved. No unix saves the environment in a system place, as accesing it would require system calls. The other execl(2) and execv(2) and their path counterparts take the environment from the environ global variable. Indeed you can pass NULL and then you get no environment at all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.