Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How is a thread's initial context (registers state) determined on thread creation?

For example: Current thread context eax=0x4, ebx=0x9000, ecx=0xfff, etc... Create a thread is called to begin executing at 0xbfbfbf. It seems obvious that the thread's eip will be set to 0xbfbfbf, but how about the other registers. Are they copied from the current thread? Are they set to 0x00000000? Is the stack pointer the same? I want to know what happens to the initial thread context at thread creation. I've checked google and OS internals books and haven't found what I'm looking for.

Also, is it the same process across other OS, in particular Linux and Windows?

share|improve this question

closed as off-topic by Simon Sheehan, and31415, harrymc, RedGrittyBrick, Kevin Panko May 28 at 16:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question is not about computer hardware or software, within the scope defined in the help center." – and31415, Kevin Panko
If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

1 Answer

up vote 0 down vote accepted

When a new thread begins executing, its registers are loaded from its stored context just like a thread that has been running already. The thread that creates the thread can fill in the new thread's context structure however it likes before it adds it to the operating system's table of running threads.

Basically it works like this:

  1. A new task structure is allocated.

  2. The fields in that new task structure are filled in however desired. This includes registers, the stack pointer, and so on.

  3. The task structure is marked ready-to-run and added to the scheduler's list of tasks.

  4. The scheduler decides to schedule the new thread, loads its context from the task structure, and switches to it.

  5. The thread now has whatever registers, stack, and instruction pointer the creating thread filled in for it.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.