Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was asked these questions in interview (C++ interview). I'm not confident of the answers and would like to know more.

  1. Can thread create another thread ?
  2. Can process create another process ?
  3. Can thread create a process ?
  4. Can process create a thread ?
share|improve this question
for ques. 1 and 4 answer is YES. – Harry Joy Feb 2 '11 at 6:04
  1. Yes, a thread can create another thread. On UNIX this is done (at the low level) through the clone() command, and at the high(er) level, in C coding, through the pthread library.

  2. Yes, a process can launch another process. This happens all the time. The most common type of process -> process is through fork().

  3. This, I'm unsure. At this point I'd like to mention there are two major types of threads: kernel-level threads and user-level threads. Kernel level threads (in general) are tied to major processes, while user level threads are spawned by these processes (so by kernel level threads). There are exceptions Maybe someone can give a better answer to thread -> process.

  4. Yes. See the comment in 3.

share|improve this answer
0) The question states windows, yet you have UNIX calls 1) clone() is Linux specific, it is not generic UNIX. 2) fork() is not just hte most common, but the only way to spawn a process on UNIX (vfork() just calls fork on most systems) 3) yes, a thread can call fork, but you confuse the issue kernel vs user threads. – Rich Homolka Feb 2 '11 at 17:16
I don't remember the Windows tag being there when I answered. I might have missed it inadvertently, sorry. – aqua Feb 3 '11 at 1:19

Any code that executes can do anything.

That said, processes aren't code -- threads are the things that are actually run, and processes are their enclosing environments. Hence (1) and (3) are true, the rest false.

share|improve this answer
"Any code that executes can do anything." is provably false on modern architectures even with trivial examples. For example, if a memory page is set as read-only, then no running code can write to that page. If a memory page is set as no-execute (NX), then no running code can execute a jump into that memory as if it were code. These techniques greatly help alleviate certain styles of attacks on software. – Michael Kjörling Sep 16 '15 at 11:00
@MichaelKjörling: Way to completely take the answer out of context. – Mehrdad Sep 16 '15 at 21:00

Yes to all.

It might help you to understand that processes don't actually "run" in Windows. Threads run. Every process has at least one thread. So, the real questions are:

  • can executing code create another thread? Yes.

On Windows this is done with the CreateThread API, or one of its cousins. The long version would be "can code that's running in the context of a thread create another thread?" Sure, but since all code runs in the context of some thread, there's no need for the extra verbiage. CreateThread takes an argument that is the address of the function that becomes the top-level function for the new thread - essentially the new thread's "main".

  • can executing code create another process? Yes.

Similarly to previous: when you're running in the context of a thread, you're also running in the context of the process that owns the thread. So, yes, there is a CreateProcess API and you can use it to create another process.

You'll also be, automatically and without further effort, creating a thread in your new process... because (since processes don't run) a process without a thread can't do anything. That first thread in the new process will start running the program's "main" function - main() or WinMain() or whatever.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .