3 First Process: Stop and Continue
Winter Semester 2026/2027
Lecture 2 ended with a requirement: run an execution, regain control, and continue that same execution later. Two questions follow: what must survive the pause, and how do we build that?
Our working definition is:
A process is an execution that can be managed.
In BVS1, our minimal operating system will do the managing. For our first process, managing means:
create -> run -> yield -> resume -> exit
This is the simplified lifecycle used for Checkpoint 1, rather than a general definition of every process’s lifetime. To stop and continue the same execution, our process needs an entry function, its own stack, saved CPU register state (its context), and lifecycle information: state and exit status.
This chapter builds those parts into one resumable execution. Creation prepares it without running its code; run starts or resumes it; yield returns control while preserving the ability to continue; exit finishes it.
3.1 C pointer notation: a reminder
The process representation will be a C struct. These expressions distinguish an object, its address, and a pointer to it:
struct process p;
struct process *q = &p;Here p is the object, &p is its address, and q stores that address. In the declaration, * makes q a pointer; in an expression, *q dereferences the pointer to access the object.
p.state accesses the object’s state directly. (*q).state accesses the same member through the pointer; q->state is shorthand for that expression. The same notation lets us access a nested member such as q->context.sp once the context has been added.
For function pointers, storing a function and calling it are different operations:
p.entry = worker;
p.entry();In the assignment, worker converts to a pointer to that function, so the first line stores where to call. It does not call worker. The second line calls it through the stored pointer; worker() would call the function directly.
3.2 Building the process object
We first give the execution a concrete representation. The following struct fragments add one part at a time; the complete definition follows after we establish what must be saved in a context.
3.2.1 Entry function
An entry function is where a new process begins its work. For example:
void worker(void) { /* process-specific work */ }The process must remember which function to enter. Its first field is therefore a function pointer:
struct process {
void (*entry)(void);
};Read void (*entry)(void) as: entry is a pointer to a function taking no arguments and returning void. Repeating that declaration is awkward, so a typedef gives the pointer type a name:
typedef void (*process_entry_t)(void);The field can now be written process_entry_t entry;. This names the same function-pointer type; it does not declare or call an entry function.
3.2.2 Owned stack storage
The process also reserves memory for its own call stack:
#include <stdint.h>
#define STACK_SIZE 4096
struct process {
process_entry_t entry;
uint8_t stack[STACK_SIZE];
};uint8_t, defined by <stdint.h>, is an unsigned 8-bit integer type. A char stack[STACK_SIZE] array would also be a viable choice for raw byte storage in our RV64 environment: a character array need not contain text. We use uint8_t to make the byte-storage role explicit and keep our treatment of integer types focused on <stdint.h>. This header is available in our freestanding RISC-V toolchain; including it does not require linking a C library.
At this point, treat the array simply as memory owned by the process; we will explain its use below.
3.2.3 Lifecycle information
The manager needs to know whether the process may run and, once it finishes, which exit status it produced:
enum process_state { READY, RUNNING, FINISHED };
struct process {
process_entry_t entry;
uint8_t stack[STACK_SIZE];
enum process_state state;
int exit_status;
};An enum supplies named alternatives for the state. READY means the process may run, RUNNING means it is executing, and FINISHED means it must not run again. exit_status records the result once it finishes.
3.2.4 What is still missing?
We can now remember where to start, where call data can live, whether the process may run, and how it finished. But suppose execution pauses here:
helper();
process_yield();
/* continue here later */These fields do not yet tell us how to continue at exactly that point. We need to understand both the stack’s runtime data and the CPU register values that make that data usable.
3.3 Understanding the process stack
The process needs owned stack storage: memory that remains associated with the process while the operating system executes elsewhere.
The process’s machine instructions are not copied onto that stack. They remain in the program’s .text section. The stack contains runtime data associated with active calls—for example saved register values, return information, some local variables, and temporary values when needed. Not every local variable is stored on the stack; the compiler may keep values in registers.
The array reserves storage; sp, the CPU’s stack-pointer register, selects the current position in that storage. Array indices increase toward higher addresses, while the RV64 C stack grows toward lower addresses:
stack grows this way
<-----
lower addresses higher addresses
&stack[0] sp &stack[STACK_SIZE]
v v v
+-----------------------------------------------+
| | -- used stack ------------|
+-----------------------------------------------+
&stack[STACK_SIZE] is the address just past the array’s last byte. A fresh process starts with an appropriately aligned sp at the high end; stack use then extends toward lower addresses. Reserving the array alone does not change the CPU’s active stack pointer.
Nested calls belong to the same execution and use the same process stack. For example:
void helper(int *x) { (*x)++; }
void worker(void) { int n = 1; helper(&n); }Conceptually, while helper() is active:
sp
lower addresses v higher addresses
+-----------------------------------------------+
| | helper frame | worker frame | - |
+-----------------------------------------------+
The helper frame and worker frame labels denote runtime data associated with the active calls. The stack does not contain the machine instructions of helper() or worker(): the CPU executes those instructions from the .text section of the program’s memory layout.
The newer call uses space toward the lower-address end of the used stack. This is a schematic picture, not an exact compiler-generated layout: the compiler may inline calls or keep values in registers.
If the process yields from a nested call, that call chain’s stack data must remain intact while the OS uses its own stack. Resuming requires the process’s saved sp as well as the preserved storage.
3.4 Continuation: where should execution return?
Lecture 2 showed the startup fragment:
call mainA RISC-V call remembers the location to which execution should later return. That location is stored in ra, the return-address register.
An ordinary return therefore already contains a useful idea:
call function
|
v
function executes
|
v
ret
|
v
continue at address stored in ra
If a process yields and is later resumed, the operating system must preserve some representation of the place where execution should continue. Preserving ra makes that idea concrete.
The stack data must survive too, and the process must recover the stack position recorded in sp. Restoring a continuation with the OS stack would not continue the same call chain.
3.5 Context
At a pause, some execution state is still in CPU registers. Another execution can overwrite those values even when the process stack remains intact. The saved CPU register state needed to continue the execution is its context.
For the cooperative CP1 mechanism, the provided representation is:
struct context {
uintptr_t ra; // continuation address
uintptr_t sp; // current stack position
uintptr_t s[12]; // saved s0 through s11
};uintptr_t, defined by <stdint.h>, is an unsigned integer type, not a pointer type. It can hold the integer representation of a pointer value and later be converted back without losing that value. In our RV64 toolchain it is 64 bits wide. We use it for saved register-sized values such as ra and sp, which contain addresses during normal execution. Students are not expected to study the complete family of fixed-width integer types here.
The array s holds the RISC-V callee-saved registers s0 through s11: s[0] stores s0, through s[11] storing s11. A calling convention is the agreement that lets separately compiled functions cooperate about how registers are used. A callee-saved register is one whose value a called function must preserve for its caller. Preserving those required registers allows an ordinary C function call to cross the context-switch boundary safely.
Students do not need to memorize that register list or derive the complete assembly routine from the ABI. For CP1 they should be able to explain why ra and sp matter and why some additional register state must also survive.
3.6 The complete process object
The saved context supplies the missing part. Together, entry, stack, context, and lifecycle information represent our managed execution.
The public representation used by Lecture 3, Exercise 2, and Checkpoint 1 is:
struct process {
process_entry_t entry;
uint8_t stack[STACK_SIZE];
struct context context;
enum process_state state;
int exit_status;
};The five fields have distinct roles:
entry: where a new process begins;stack: process-owned raw byte storage for its call stack;context: saved CPU register state needed to continue execution;state: whether the process is READY, RUNNING, or FINISHED;exit_status: the result recorded when the process finishes.
Exercise 2 uses this exact field order because its precompiled process library was built against this representation. Checkpoint 1 keeps the same interface and representation while students provide the implementation.
3.7 The provided context-switch boundary
The course provides a function implemented in RISC-V assembly, with this C declaration:
void context_switch(
struct context *current_context,
const struct context *next_context);current_context points to storage where the running execution’s registers will be saved. next_context points to the saved registers to restore. The const qualifier says the function does not modify the context object through next_context. Either context may belong to the OS execution or the process execution.
A context switch in this part of BVS1 means:
- save the current execution context;
- restore another saved context;
- continue the restored execution.
The assembly below shows the matching save and restore operations. The ellipses stand for the corresponding operations on s1 through s10:
sd ra, 0(a0)
sd sp, 8(a0)
sd s0, 16(a0)
...
sd s11, 104(a0)
ld ra, 0(a1)
ld sp, 8(a1)
ld s0, 16(a1)
...
ld s11, 104(a1)
retsd (store doubleword) copies a 64-bit register value into memory; ld (load doubleword) copies a 64-bit value from memory into a register. In sd s0, 16(a0), the destination is the address in a0 plus 16 bytes. The offsets follow the context layout: ra at byte 0, sp at byte 8, and the twelve saved s registers at bytes 16 through 104.
After the loads, ret uses the newly restored ra. The switch therefore changes both the stack that is active and the location at which execution continues.
The stored fields and the restored fields match: ra, sp, and all twelve entries of s. The process needs both its stack memory and these saved register values to continue the same execution.
The function arguments current_context and next_context arrive in the RISC-V argument registers a0 and a1. These registers are caller-saved: their previous values are not part of the state that an ordinary C call promises to preserve. In this cooperative switch they are used to locate the two context structures; the persistent saved context is ra, sp, and s0-s11.
The same operation works in both directions:
OS running process running
| |
| save OS context | save process context
| restore process context | restore OS context
v v
process running OS running
There is no scheduler here. CP1 contains only one process.
3.8 Implementation boundary
The course provides the machinery whose complexity is incidental to the CP1 competence:
- existing startup and linker support;
- UART output already available in the starter;
- the context representation;
- the RV64 context-switch assembly boundary.
Students construct the process abstraction and lifecycle in C:
- process representation;
- owned stack;
- first-run context;
- entry handling;
- run;
- yield;
- exit;
- observable resume behaviour.
CP1 does not require timer interrupts, preemption, several runnable processes, a scheduler, privilege separation, virtual memory, dynamic loading, or dynamic allocation.
3.9 Public process interface
The first-process abstraction uses this public C interface:
void process_create(struct process *process,
process_entry_t entry);
void process_run(struct process *process);
void process_yield(void);
void process_exit(int status);Exercise 2 links this interface against a provided implementation. Checkpoint 1 keeps the same interface but replaces that implementation with student-written C code.
3.10 The lifecycle in C
Lecture 2 introduced:
create -> run -> yield -> resume -> exit
Lecture 3 now connects each semantic step to the minimum implementation work.
3.10.1 Create / prepare
Preparing a process means constructing a valid first-run state:
remember entry function
|
choose process stack top
|
prepare initial context
|
state = READY
No process code has executed yet.
A process that has already yielded has a saved continuation. A new process does not.
The restore mechanism nevertheless expects a context containing a stack pointer and a continuation. The solution is to prepare an initial context before the process first runs.
Its initial stack pointer is placed at the aligned high end of its owned stack array.
The initial continuation is a small bootstrap function, called process_start(): an internal function used to enter a process for the first time. It is an implementation detail, not another public process operation.
Conceptually:
initial sp -> aligned high end of process.stack
initial ra -> process_start -> process.entry()
The first restore followed by ret therefore enters process_start() while using the process’s own stack.
process_start() calls the process entry function. If that function returns normally, process_start() calls process_exit(0). Returning from the entry function therefore finishes the process with status 0 and transfers control back to the OS.
Later resumes do not use the prepared bootstrap address again. A yield saves the real continuation from the running process, so the next restore continues after the context-switch call inside the yield path.
This is an important simplification:
First start and later resume use the same low-level restore mechanism.
3.10.2 Run and resume
The OS keeps a pointer named current_process to identify the process whose execution currently owns the CPU. process_yield() and process_exit() take no process argument: they use this pointer to find the object whose state and context they must update.
process_run(p) performs these steps:
reject p if its state is FINISHED
set current_process to p
set p's state to RUNNING
context switch from OS to p
The FINISHED check belongs to process_run(): a saved context does not by itself grant permission to run again.
The same run operation can support two cases:
- a READY process that has never run starts through its prepared bootstrap context;
- a READY process that yielded earlier resumes through the context saved by that yield.
The required behaviour is resume. A separate process_resume() API is not necessary.
3.10.3 Yield
For CP1:
yield means that the running process voluntarily returns control to the operating system while remaining able to resume later.
Using current_process, yield marks the process READY, saves its current context, and restores the OS context.
When the operating system later runs that process again, execution continues after the context-switch call inside the yield path.
3.10.4 Exit
For CP1:
exit means that the process finishes, records a result, and returns control to the operating system.
Using current_process, exit records the status in the process object, marks it FINISHED, and switches from the process context back to the OS context.
A finished process must not be resumed.
Yield and exit can use the same low-level control-transfer boundary, but their lifecycle meaning is different.
3.11 The complete CP1 observation
Exercise 2 and Checkpoint 1 use the same small permutation program. Its observable behaviour is:
process: before yield: VBS1
os: process yielded
process: result: VB1S
os: process exited
The interesting evidence is not the strings themselves. The output demonstrates that:
- the process starts using its own execution context;
worker()callspermute(), which modifies process-local data and yields before returning;- the operating system regains control while that call chain remains suspended;
- the same
permute()invocation later continues afterprocess_yield(); - the local array progresses from
VBS1toVB1S, showing that the same execution state survived; - the process exits with status 0 and becomes FINISHED;
- the operating system regains control again and must not resume the finished process.
3.12 Why CP1 deliberately uses yield
CP1 needs a control transfer that gives the operating system control without finishing the process.
A timer interrupt would already introduce the preemption mechanism needed later for CP2. Blocking or sleeping would require an additional condition that makes the process runnable again. An ordinary C return would not expose the requirement to preserve and later restore a continuation.
A cooperative yield is therefore the smallest mechanism that makes resumability observable.
It also creates the next operating-system problem cleanly:
What if a process never yields?
That question motivates timer-driven preemption on the way to CP2.
3.13 Check your model
Use these questions to check the distinction between restarting, resuming, and finishing:
- What breaks if we call the entry function again after yield instead of restoring the saved context?
- What breaks if we restore
rabut notspands0-s11? - What breaks if
process_run()allows a FINISHED process to execute again?
A successful explanation should connect each failure to the process representation: calling entry again restarts its work, restoring only part of the context cannot preserve the same execution, and restoring a finished process violates its lifecycle state.
3.14 Key ideas
- A process is a managed execution.
- Our process representation combines an entry function, owned stack storage, CPU context, lifecycle state, and exit status.
- The stack is process-owned memory for the runtime data of active function calls. Function instructions remain in
.text. - A context is the saved CPU register state needed to continue the same execution.
- Lifecycle state says whether an execution may run; context says how it continues.
- A context switch saves the current CPU register state and restores the next one. The same mechanism transfers control in both directions between OS and process.
References
- RISC-V International, RISC-V ABIs Specification. https://riscv-non-isa.github.io/riscv-elf-psabi-doc/
- Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, especially the process chapters. https://pages.cs.wisc.edu/~remzi/OSTEP/
- Russ Cox, Frans Kaashoek, Robert Morris et al., xv6: a simple, Unix-like teaching operating system and xv6-riscv source, used here only as a comparative reference for low-level context switching. https://pdos.csail.mit.edu/6.S081/2020/xv6/book-riscv-rev1.pdf