context switch threads goal of today s class
play

Context-switch & Threads Goal of Todays Class Understand the - PowerPoint PPT Presentation

Context-switch & Threads Goal of Todays Class Understand the concepts of context, context-switch and threads Understand the related functions in assignment P1 thread_init, thread_create, thread_yield, thread_exit ctx_entry,


  1. Context-switch & Threads

  2. Goal of Today’s Class • Understand the concepts of context, context-switch and threads • Understand the related functions in assignment P1 • thread_init, thread_create, thread_yield, thread_exit • ctx_entry, ctx_start, ctx_switch

  3. Review: the minimal requirement of program execution is code & stack segments in memory address space.

  4. Assume 2 programs in memory stack end … program #1 stack … … stack start … … … code end … program #1 code … … OS puts the code & stack of both code start … programs in the memory so that … … they can take turns to execute. stack end … program #2 stack … … stack start … … … code end … program #2 code … … code start …

  5. Review: context defines which program the CPU is executing; context = memory address space + stack pointer + instruction pointer

  6. CPU in the context of program #1 program #1 stack end … … … program #1 stack start … CPU … … program #1 code end … … … Stack pointer register program #1 code start … … … program #2 stack end … Instruction pointer register … … program #2 stack start … … … program #2 code end … … … program #2 code start …

  7. CPU in the context of program #2 program #1 stack end … … … program #1 stack start … CPU … … program #1 code end … … … Stack pointer register program #1 code start … … … program #2 stack end … Instruction pointer register … … program #2 stack start … … … program #2 code end … … … program #2 code start …

  8. Context-switch CPU switches to the context of program #2

  9. Context-switch CPU switches to the context of program #1

  10. Question: when does context- switch happen?

  11. When does context-switch happen? • Program terminates. • Program calls yield system call. (next slide) • CPU receives a timer interrupt. (later in assignment P2) • CPU receives an I/O interrupt. (later in assignment P5)

  12. yield is a noble behavior A car can occupy the road, but it decides to stop and let others to use the road first. int noble_a() { …… A program can occupy the CPU, but it decides yield(); to stop and let others to use the CPU first. …… }

  13. Two noble functions int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); …… …… yield(); yield(); printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… } }

  14. One possible schedule int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 1 3 …… …… yield(); yield(); 2 4 printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… 5 6 } } Output: Noble A does some work Noble B does some work Noble A works some more Noble B works some more

  15. Another possible schedule int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 3 1 …… …… yield(); yield(); 4 2 printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… 6 5 } } Output: Noble B does some work Noble A does some work Noble B works some more Noble A works some more

  16. Question: how do we run two functions at the same time? Let’s review some knowledge of stack.

  17. Review of stack … int noble_a() { printf(“Noble A does some work”); stack frame of main …… yield(); printf(“Noble A works some more”); stack stack frame of noble_a …… } int main() { stack frame of yield noble_a(); return 0; } main() calls noble_a() calls yield()

  18. Review of stack … stack frame of main Only continue when noble_a returns For a single stack, there is only one currently running function, so that noble_a stack frame of noble_a Only continue when yield returns and noble_b cannot run in the same stack at the same time. stack frame of yield Currently running function main() calls noble_a() calls yield()

  19. Two stacks for two nobles … … stack frame of noble_a stack frame of noble_b Both are currently running functions! How does yield do context-switch between the two nobles?

  20. Noble A does some work int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 1 …… …… yield(); yield(); printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… } } Output: Noble A does some work

  21. CPU in context of noble_a … … CPU stack frame of noble_a Stack pointer Instruction code of code of code of noble_a yield noble_b

  22. Noble A yields int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 1 …… …… yield(); yield(); 2 printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… } } Output: Noble A does some work

  23. noble_a calls yield … … CPU stack frame of noble_a Stack pointer stack frame of yield Instruction code of code of code of noble_a yield noble_b

  24. yield switches context to noble_b … … CPU stack frame of noble_b stack frame of noble_a Stack pointer stack frame of yield Instruction code of code of code of noble_a yield noble_b

  25. Noble B does some work int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 1 3 …… …… yield(); yield(); 2 printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… } } Output: Noble A does some work Noble B does some work

  26. Noble B yields int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 1 3 …… …… yield(); yield(); 2 4 printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… } } Output: Noble A does some work Noble B does some work

  27. noble_b calls yield … … CPU stack frame of noble_b stack frame of noble_a Stack pointer stack frame of yield stack frame of yield Instruction code of code of code of noble_a yield noble_b

  28. yield switches context to noble_a … … CPU stack frame of noble_b stack frame of noble_a Stack pointer stack frame of yield stack frame of yield Instruction code of code of code of noble_a yield noble_b

  29. yield returns to noble_a … … CPU stack frame of noble_a stack frame of noble_b Stack pointer stack frame of yield Instruction code of code of code of noble_a yield noble_b

  30. Noble A works some more int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 1 3 …… …… yield(); yield(); 2 4 printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… 5 } } Output: Noble A does some work Noble B does some work Noble A works some more

  31. noble_a terminates (implicit yield) … … CPU stack frame of noble_b Stack pointer stack frame of yield Instruction code of code of code of noble_a yield noble_b

  32. yield switches context to noble_b … … CPU stack frame of noble_b Stack pointer stack frame of yield Instruction code of code of code of noble_a yield noble_b

  33. yield returns to noble_b … … CPU stack frame of noble_b Stack pointer Instruction code of code of code of noble_a yield noble_b

  34. Noble B works some more int noble_a() { int noble_b() { printf(“Noble A does some work”); printf(“Noble B does some work”); 1 3 …… …… yield(); yield(); 2 4 printf(“Noble A works some more”); printf(“Noble B works some more”); …… …… 5 6 } } Output: Noble A does some work Noble B does some work Noble A works some more Noble B works some more

  35. How does yield do context-switch? Context-switch from noble_b to noble_a • The answer is simple: change the stack pointer! • when switching from noble_a to noble_b , the yield function needs to record the stack pointer of noble_a • when switching back to noble_a, the yield function restores the stack pointer of noble_a

  36. Recall: when does context-switch happen? ✅ • Program terminates. ✅ • Program calls yield system call. • CPU receives a timer interrupt. (later in assignment P2) • CPU receives an I/O interrupt. (later in assignment P5)

  37. Lesson: A thread owns a stack running a given function.

  38. A thread owns a stack running a given function int main() { stack of noble_b thread thread_create(noble_b); noble_a(); return 0; } stack of main thread int noble_a() { …… } code int noble_b() { code of code of code of …… noble_a yield noble_b }

  39. Context-switch between threads int main() { stack of noble_b thread thread_create(noble_b); noble_a(); return 0; } CPU stack of main thread int noble_a() { Stack pointer …… } Instruction code int noble_b() { code of code of code of …… noble_a yield noble_b }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend