ECE344 Fall 2026 (Sec 1) Lec 14 - Threading Models
Overview
Jon Eyolfson compares user, kernel, and many-to-many threading models, explaining how scheduling, blocking system calls, and parallelism differ. He then demonstrates many-to-one user threads with C's ucontext_t APIs, separately allocated stacks, and context-switch traces that show how Lab 4's thread creation and scheduling work.
Key takeaways
- Many-to-one user threads can switch without kernel calls, but they cannot run in parallel and one blocking system call stalls the process's only kernel thread.
- One-to-one Pthreads allow the kernel to schedule threads independently, so one thread can block while another runs and different threads can use separate CPU cores.
- Many-to-many threading can limit the number of kernel threads while supporting many user threads, but mapping and scheduling become more complex and blocking can stall threads assigned to the same kernel thread.
- A ucontext_t-based user thread needs both saved register state and its own stack; makecontext sets its entry function, while swapcontext saves the current thread and restores another.
- setcontext restores the saved program counter, registers, and stack pointer, but does not roll back values in stack memory, which is why local variables can retain state across context switches.
- In the lecture's two-thread trace, separate stacks plus a shared global counter produce the interleaving A1, B2, A3, illustrating concurrency on one kernel thread without parallel execution.
Chapters
- Each thread in a process has its own registers and stack, while threads share the process address space.
- A threading model determines where the thread table lives and who switches between threads: the kernel, a user-space library, or both.
- In many-to-one, a user-space library maps multiple user threads to one kernel thread, so the kernel cannot schedule those threads in parallel.
- User-thread creation and switching avoid system calls, but a blocking call such as read blocks the process's only kernel thread.
- In one-to-one, each user-created thread maps to a kernel thread; creation and switching use kernel support, but other threads can run when one blocks.
- Pthreads use the one-to-one model, allowing kernel scheduling and parallel execution on multiple CPU cores.
- Many-to-many can use a limited number of kernel threads while allowing the library to manage many user threads.
- The library must schedule user threads and map them to kernel threads, making its implementation more complex.
- If a kernel thread blocks in a call such as read, the user threads mapped to it may also stall.
- A user-level context switch saves and restores CPU registers; each user thread also needs its own allocated stack.
- The ucontext API provides getcontext, makecontext, setcontext, and swapcontext for saving, creating, restoring, and switching contexts.
- makecontext configures a context to start at a specified function and can pass integer arguments; the lecture's example passes 42.
- The Lab 4 stack helper also registers stacks with Valgrind so it recognizes stacks not allocated by the kernel.
- getcontext saves the current register state so restoring that context resumes as if getcontext had just returned.
- Calling setcontext on a context saved before a print statement repeats that statement, creating an infinite loop.
- Restoring a context resets the program counter and registers to the saved state rather than continuing from the setcontext call.
- The example initializes T1 with getcontext, assigns a new stack through uc_stack, and uses makecontext to start T1_run.
- setcontext to T1 runs T1_run with the integer argument 42 on T1's separate stack.
- These are user threads: the process still has one kernel thread, so this example does not use multiple CPU cores.
- Without a return context configured, returning from the user-thread function ends the process.
- swapcontext saves the running thread's context and restores the selected thread's context, allowing execution to switch between main, T1, and T2.
- The example switches from main to T1, then to T2, and back to T1 using separate saved contexts.
- Setting T1's uc_link to T2's context makes T2 run automatically when T1's function returns.
- T2 can then restore main's context, letting the program print its final message and exit normally.
- The exercise gives threads A and B independent stacks and contexts, while both access a shared global integer i.
- With context switches after each loop iteration, the output is A1, B2, A3, after which both threads reach their loop limit.
- A context restore resets the stack pointer, but values already stored in the stack memory remain, so each thread's local d retains its value.
- A plain getcontext followed by setcontext is not a complete swapcontext implementation because it needs logic to distinguish first entry from resumption.
- The lecture's user-thread model combines a ucontext_t register state, a separately allocated stack, and a library scheduler.
- Lab 4 asks students to create user threads, manage their stacks, and choose which thread to resume, for example with round-robin scheduling.
- The closing recap contrasts library-managed many-to-one threads with Pthreads' one-to-one kernel threads and notes that no practical is scheduled for the following week.
Transcript excerpt
3:10 All right. So let's restart at threading model. So this will be the fun part which you'll need for also lab four. So recall let's go back to an hour ago. So here we now have multiple threads. So a thread lives within a process should be its own registers and own stack. Again they're created independently whether or not since the stack lives in virtual memory you can give pointers to each other's stacks as we saw before not a good idea. Generally do not do that.
3:58 So there's multiple ways to implement threads in a process and you have to answer the question who keeps track of the table of threads. There'll be a table of threads just like there is a table of processes and then who switches between them and unlike processes that have to be managed by the kernel, threads can be managed by the kernel or a library or a combination of both. And libraries run in user mode. So they're called user threads or kernel threads. So we have either user threads or kernel threads. User threads are defined in a library. They live entirely in user space. So from the kernel's point of view, it only sees the one thread that it created with the process. Just like
4:48 any other process, it doesn't know about your threads within your process. So there's entirely a U thing. So that means the kernel cannot schedule and your user threads in parallel because it doesn't know about them. So it only schedules your one main thread that it knows about and nothing else. So the best you can do is have all of your user threads run concurrently. You will never get parallelism. Colonel only knows about the one main thread. On the other hand, there are kernel threads that as
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jon Eyolfson.