0

I have a situation as following:

main()
{
 create a thread executing function thread_func();

 another_func();

}

another_func()
{
  //check something and do something.
  // To do something, create a child process.
  // after creating child process, current thread goes in checking state again
  // child process independently running.
}

thread_func()
{
 infinite loop(); // checking something and doing something
}

thread is created using pthread. Please tell: is it good to start a child process like above in a thread? Also what happens if this is done.

Does child process creates its own another copy thread executing thread_func()?

Thanks

  • You might be interested in reading [What happens to other threads when one thread forks()?](https://stackoverflow.com/questions/10080811/what-happens-to-other-threads-when-one-thread-forks) – Some programmer dude Jun 01 '17 at 04:31
  • This link also might be useful. https://stackoverflow.com/questions/39890363/what-happens-when-a-thread-forks – Tony Tannous Jun 01 '17 at 18:44

2 Answers2

0

It depends on what you want to do... Here your thread will never go in another_func() because it is stuck in thread_func() because of the infinite_loop(). So it's your main program that will create the child process.

nachiketkulk
  • 1,141
  • 11
  • 22
drumz
  • 65
  • 7
0

It's not very clear from your question what you want to do. the pthread_create() API takes a pointer to a function of type void* ()(void). The actual thread execution is on that thread, so if you want to create a child process on a thread, you have to do it inside thread_func()

If you are creating another process with the fork() API, which is the standard way on linux, that will create a separate process with a copy of the entire memory space of the parent process. But the memory will be virtual and marked as copy on write, so in reality the memory will not be copied unless you try to write to it.

If after you call fork() you also call exec() or any of the other APIs in the exec() family, then you don't need to concern yourself with the copied memory. After the exec() API the child process will have it's own memory space separate from the parent.

AdrianRK
  • 127
  • 6