Pintos processes have only one thread
In Pintos, the TCB of a thread is stored within the same page as the thread’s stack, at the bottom(start) of the page. This is possible because each thread has its own stack; since there is no stack owned exclusively by the process (and not by any of its child threads), the PCB instead must be stored in the process’ heap via malloc
, where all of its child threads can access it via pointer.
Every thread struct occupies the beginning of its own single 4 KiB page of memory. The rest of the page is used for the thread’s stack, which grows downward from the end of the page. It looks like this:
4 kB +---------------------------------+
| kernel stack |
| | |
| | |
| V |
| grows downward |
| |
| |
| |
| |
| |
| |
sizeof (struct thread) +---------------------------------+ (TCB)
| magic |
| : |
| : |
| status |
| tid |
0 kB +---------------------------------+
This layout has two consequences. First, struct thread must not be allowed to grow too big. If it does, then there will not be enough room for the kernel stack. The base struct thread is only a few bytes in size. It probably should stay well under 1 kB.
The first symptom of either of these problems will probably be an assertion failure in thread_current(), which checks that the magic' member of the running thread's struct thread' is set to THREAD_MAGIC. Stack overflow will normally change this value, triggering the assertion. (because THREAD_MAGIC is at the end, right below the stack area)
To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit. reference
User virtual memory is per-process. When the kernel switches from one process to another, it also switches user virtual address spaces by changing the processor’s page directory base register (see pagedir_activate()
in userprog/pagedir.c
). struct thread
contains a pointer to a process’s page table.
current thread: Copy the CPU's stack pointer into esp', and then round that down to the start of a page. Because struct thread' is always at the beginning of a page and the stack pointer is somewhere in the middle, this locates the curent thread.