Download - CS414 Minithreads project overview
![Page 2: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/2.jpg)
2CS414 Minithreads overview
What you have to do
Implement Minithreads, a user-level threads package on Windows NT
Includes semaphores and queues Non-preemptive The hardest parts (context switching,
stack initialisation) are done for you For the adventurous: add preemption
![Page 3: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/3.jpg)
3CS414 Minithreads overview
What we’ll cover
What order to do things in How context switching works How yielding between threads
works Minithread implementation hints
![Page 4: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/4.jpg)
4CS414 Minithreads overview
Minithreads structure
machineprimitives_x86.c
machineprimitives.h
machineprimitives.c
minithread.h
minithread.c
synch.h
synch.c
queue.h
queue.c
interrupts.h
interrupts.c
![Page 5: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/5.jpg)
5CS414 Minithreads overview
Minithreads, step-by-step
Implement queues Define struct minithread Implement minithreads operations
fork and yield system initialisation termination and cleanup start, stop
Implement semaphores
![Page 6: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/6.jpg)
6CS414 Minithreads overview
Queue alternatives
Implement by using enclosing structs:
Or implement by using pointers in element types:
head
tail
head
tail
![Page 7: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/7.jpg)
7CS414 Minithreads overview
Defining a minithread
What’s in a struct minithread (thread control block)? stack top pointer stack base pointer numerical identifier (int) thread status anything else you think necessary
![Page 8: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/8.jpg)
8CS414 Minithreads overview
Minithread operationsminithread_t minithread_fork(proc, arg)
create thread and make it runnableminithread_t minithread_create(proc, arg)
create a thread but don’t make it runnablevoid minithread_yield()
stop this thread and run a new one from the run queue (make the scheduling decisions here)
void minithread_start(minithread_t t)void minithread_stop()
start another thread, stop yourself
![Page 9: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/9.jpg)
9CS414 Minithreads overview
Threads and their stacks
NT gives you an initial stack
Subsequent minithread stacks are allocated on the process’s heap using malloc
0
2000
code
stack
heap
50000
![Page 10: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/10.jpg)
10CS414 Minithreads overview
Context switching
minithread_switch(old_thread_sp_ptr, new_thread_sp_ptr) is provided
Swap execution contexts with a thread from the run queue registers program counter stack pointer
![Page 11: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/11.jpg)
11CS414 Minithreads overview
Context switching
old_thread_sp_ptr new_thread_sp_ptr
ESP
?
new thread’s registers
old thread TCB new thread TCB
![Page 12: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/12.jpg)
12CS414 Minithreads overview
Push on old context
old_thread_sp_ptr new_thread_sp_ptr
ESP
?
old thread’s registers
new thread’s registers
old thread TCB new thread TCB
![Page 13: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/13.jpg)
13CS414 Minithreads overview
Change stack pointers
old_thread_sp_ptr new_thread_sp_ptr
ESP
old thread’s registers
new thread’s registers
old thread TCB new thread TCB
![Page 14: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/14.jpg)
14CS414 Minithreads overview
Pop off new context
old_thread_sp_ptr new_thread_sp_ptr
ESP
old thread’s registers
old thread TCB new thread TCB
![Page 15: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/15.jpg)
15CS414 Minithreads overview
Thread yield
Use minithread_switch to implement minithread_yield
What does a yield do? Where does a yielding thread
return to when it’s rescheduled?
![Page 16: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/16.jpg)
16CS414 Minithreads overview
Thread yield
ESP
void thread1_proc() {
printf("Running thread 1\n");
minithread_yield();
0x40164 printf("Running thread 1\n");
...
}thread 1 thread 2
STOPPEDRUNNING
0x85522
registers
![Page 17: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/17.jpg)
17CS414 Minithreads overview
Push return address and call yield
ESP
thread 1 thread 2STOPPEDRUNNING
0x85522
registers
0x40164
void thread1_proc() {
printf("Running thread 1\n");
minithread_yield();
0x40164 printf("Running thread 1\n");
...
}
![Page 18: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/18.jpg)
18CS414 Minithreads overview
Switch to new thread
ESP
void thread2_proc() {
int x;
for (;;) {
minithread_yield();
0x85522 printf("x is now %d.\n", x++);
}
}
thread 1 thread 2STOPPED RUNNING
0x85522
registers
0x40164
![Page 19: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/19.jpg)
19CS414 Minithreads overview
Return from yield into new context
ESP
thread 1 thread 2STOPPED RUNNING
registers
0x40164
void thread2_proc() {
int x;
for (;;) {
minithread_yield();
0x85522 printf("x is now %d.\n", x++);
}
}
![Page 20: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/20.jpg)
20CS414 Minithreads overview
Implementation details
How do we switch to a newly-created thread?
Where do the stacks come from? How do we create a thread? How do we initialise the system?
![Page 21: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/21.jpg)
21CS414 Minithreads overview
Minithread creation
Two methods to choose from minithread_create(proc, arg)
minithread_fork(proc, arg)
proc is a proc_t typedef int (*proc_t)(arg_t) e.g. int run_this_proc(int* x) could cast any pointer to (int *)
![Page 22: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/22.jpg)
22CS414 Minithreads overview
Minithread creation
Allocate a struct minithread (TCB) Allocate and initialise a new stack minithread_allocate_stack(stackbase,
stacktop) minithread_initialize_stack(stacktop, body_proc, body_arg, final_proc, final_arg)
Set the initial thread status Whatever else is appropriate
![Page 23: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/23.jpg)
23CS414 Minithreads overview
An initialised stack
root_proc addr
final_arg
final_proc addr
body_arg
body_proc addrstack_top
stack_base
Stack must look as though minithread_switch has been called
![Page 24: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/24.jpg)
24CS414 Minithreads overview
How a new thread starts
root_proc is popped off the stack after “return” from minithread_switch
It runsbody_proc(body_arg)
final_proc(final_arg)
To execute the user-provided function and allow thread cleanup
root_proc doesn’t return
![Page 25: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/25.jpg)
25CS414 Minithreads overview
When your program starts
NT has kernel threads When your program
starts: one kernel thread of
control NT-provided execution
stack
0
2000
50000
code
stack
heap
![Page 26: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/26.jpg)
26CS414 Minithreads overview
Code example
int proc(int* arg) {
printf("Hello, world!\n");
return 0;
}
main() {
minithread_system_initialize(proc, NULL);
}
![Page 27: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/27.jpg)
27CS414 Minithreads overview
Initialising the system
minithreads_system_initialize (proc_t mainproc,arg_t mainarg)
Starts up the system First user thread runs
mainproc(mainarg) Should the initial minithread be the
same as the kernel thread?
![Page 28: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/28.jpg)
28CS414 Minithreads overview
Initialising the system
If main() returns, it will terminate the entire process!
Make minithread_system_init() not return
It should create the first user thread, which runs mainproc(mainarg)
Your kernel thread needn’t terminate
![Page 29: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/29.jpg)
29CS414 Minithreads overview
Cleaning up threads
A minithread terminates when it reaches the end of its body_proc
A good solution will clean up its stack: minithread_free_stack(stackbase)
But a thread can’t destroy its stack itself: minithread_switch won’t work!
![Page 30: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/30.jpg)
30CS414 Minithreads overview
Minithread destruction
A terminating thread T runs its final_proc “notifies the system that it wants to
finish” relinquishes the processor
Some other thread sees T needs to be cleaned up frees T ’s stack, etc
![Page 31: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/31.jpg)
31CS414 Minithreads overview
A word of warning
The NT kernel thread has a stack You can make a minithread out of
it But you can’t free the NT stack!
![Page 32: CS414 Minithreads project overview](https://reader035.vdocument.in/reader035/viewer/2022081514/56814f89550346895dbd4232/html5/thumbnails/32.jpg)
32CS414 Minithreads overview
Summary
Implement queues Fork, initialisation, yield for 1 thread Yielding between multiple threads Thread cleanup Semaphores Implement “food services” problem
concurrently if you like, or later