Some questions on threads and processes

0

1

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 ?

shiv

Posted 2011-02-02T05:28:16.297

Reputation: 1

for ques. 1 and 4 answer is YES. – Harry Joy – 2011-02-02T06:04:33.437

Answers

1

  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.

aqua

Posted 2011-02-02T05:28:16.297

Reputation: 473

start="0">

  • The question states windows, yet you have UNIX calls
  • clone() is Linux specific, it is not generic UNIX.
  • fork() is not just hte most common, but the only way to spawn a process on UNIX (vfork() just calls fork on most systems)
  • yes, a thread can call fork, but you confuse the issue kernel vs user threads.
  • < – Rich Homolka – 2011-02-02T17:16:26.460

    I don't remember the Windows tag being there when I answered. I might have missed it inadvertently, sorry. – aqua – 2011-02-03T01:19:21.577

    0

    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.

    user541686

    Posted 2011-02-02T05:28:16.297

    Reputation: 21 330

    "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. – a CVn – 2015-09-16T11:00:39.493

    @MichaelKjörling: Way to completely take the answer out of context. – user541686 – 2015-09-16T21:00:27.577

    0

    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.

    Jamie Hanrahan

    Posted 2011-02-02T05:28:16.297

    Reputation: 19 777