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
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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