Download presentation
Presentation is loading. Please wait.
Published byAnissa Townsend Modified over 8 years ago
1
Threads Lab Keys to Success
2
Basics Create 4 functions that run forever, blinking the 4 lights – Test them by calling them from main Allocate an array of 5 Process Control Blocks (PCBs) (one for main, and one for each thread) – Each PCB will have space for all of the registers, the stack and “active” status. Write a save_registers and restore_registers function that will save registers in the PCB and copy them out of the PCB for your thread. – Test these functions by calling them every time you get a watchdog timer interrupt (At first there will be only one process – main() Write a pthread_create function that will take a function as a parameter – This function will point the stack pointer to the stack for the thread, then insert the PC and the current status register Modify the WDT interrupt handler to get the next runnable process, save the current registers into the current PCB, then restore registers from the next runnable process into the process registers. You are now done!!
3
Overview Processor Registers R0 R1 R15 Current PCB R0 R1 Current Stack PC SR Next PCB R0 R1 Next Stack PC SR
4
In pthread_create(red_light) Processor Registers R0 R1 R15 Current PCB R0 R1 Current Stack PC SR Unused PCB R0 R1 unused Stack PC SR Return PC Set R0(PC) to red_light Set R2(SR) to the current status register _get_SR_register() Point R1(SP) at the PCB stack red_light R2 _get_SR_register()
5
In Save Registers Function Processor Registers R0 R1 R15 Current PCB R0 R1 Current Stack PC SR Next PCB R0 R1 Next Stack PC SR Return PC Copy Registers R2 Copy PCCopy PC Copy PCCopy PC Copy SRCopy SR Copy SRCopy SR Copy the PC from the stack into the PCB register R0, Copy the SR from the stack into PCB register R2 Point R1 in the PCB at the SR in the stack
6
In Restore Registers Function Processor Registers R0 R1 R15 Current PCB R0 R1 Current Stack PC SR Next PCB R0 R1 Next Stack PC SR Return PC Restore Registers Copy Return PC Return PC R2 Copy PCCopy PC Copy PCCopy PC Copy SRCopy SR Copy SRCopy SR Copy the registers from the new PCB into the real registers Copy R0(PC) onto the stack along with R2(SR) Copy the return PC to the new stack so that restore registers will return to the correct place Point the Stack Pointer (R1) at the return address.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.