Skip to main content Link Menu Expand (external link) Document Search Copy Copied

CS 326 Lecture & Lab Meeting Summary – Spring 2025

Date: April 03, 2025, 02:46 PM Pacific Time (US and Canada)
Meeting ID: 813 3845 7711


Quick Recap

The meeting was led by the instructor, Greg, who guided the discussion on project testing, system calls, and kernel–user interactions. Key topics included:

  • Project Implementation:
    • Development of tests and system calls with a focus on kernel–user interactions and process management.
    • Handling keyboard input, process termination, and data transfer between kernel and user space.
  • Coding Practices & Debugging:
    • Discussion on coding practices, debugging strategies, and the use of AI tools.
    • Resolution of coding issues encountered during development.
  • Hackathon Announcement:
    • Encouragement for student participation in an upcoming hackathon.

Next Steps

Students are expected to:

  • PS Command Implementation:
    • Extrapolate from the demonstrated getkd() system call implementation.
    • Transfer required fields from each process entry (proc) into user space.
    • Implement looping and printing of process information within the user program.
  • Hackathon:
    • Attend the upcoming hackathon event.

Detailed Discussion Topics

Test Implementation and Project Deadline

  • Partial tests, including history tests, have been pushed to the repository.
  • Some tests (e.g., those related to free memory and PS) may yield different results based on configuration.
  • The project deadline was extended to Wednesday after a student’s request, despite an extra day already provided.

Kernel Allocator and PS Project

  • Kernel Allocator & Pipe Structure:
    • Greg discussed the complexities of designing a kernel allocator and managing the pipe structure.
    • Emphasis was placed on the need for synchronization when handling multiple scenarios.
  • PS Project Objectives:
    • The goal is to replace the control-P command with a more user-friendly command.
    • The user process will print the process table by transferring data from kernel space to user space.
    • This mechanism is similar to how the OS reads files or pipes and helps in crossing the kernel–user boundary.

Kernel Interpretation of Key Presses

  • Key Press Handling:
    • A key press generates an interrupt that the kernel processes.
    • Special Characters:
      • For example, a control-P key press triggers a process dump.
    • Regular Characters:
      • Regular keys are appended to the console (pipe) buffer.
  • Process Termination:
    • When a process is marked for termination, the kernel sets its state to “killed.”
    • The process checks its kill flag when returning to user space and exits cleanly if necessary.

A simplified Mermaid diagram illustrating this flow is provided below:

flowchart TD
    A[Key Press Event] --> B[Interrupt Generated]
    B --> C[Kernel Processes Input]
    C --> D{Key Type?}
    D -- Special Control-P --> E[Trigger Process Dump]
    D -- Regular --> F[Append to Console Buffer]
    F --> G[Process Continues Execution]
    E --> G

Killing Processes in the Kernel

  • Delayed Termination:
    • Setting a process to “kill” raises a flag rather than immediately terminating it.
    • If a process is sleeping, the kernel forces it into a runnable state so it can check this flag.
  • Exit and Cleanup:
    • When returning to user space, processes check the kill status and exit if flagged, entering a “zombie” state until the parent process calls wait.
  • Multiprocessor Considerations:
    • The termination process may not be immediate on multiprocessor systems due to scheduling delays.

New System Call for Data Transfer

  • Proposal: getkd() System Call:
    • Introduces a system call to copy data from a kernel-space structure to a user-space structure.
    • This mechanism allows the user to access kernel information (e.g., process table details).
  • Implementation Considerations:
    • Requires updating the system call entry and writing corresponding user-level code.
    • A simple example (akin to a “cat” command) is suggested to test the system call.

System Call Implementation Structure Setup

  • Struct Sharing Between Kernel and User:
    • A new header file is created in the kernel directory to define a structure accessible by both kernel and user space.
    • This technique parallels the approach used for other system calls.
  • Compilation and Debugging:
    • The group encountered a compilation error due to uninitialized variables, which was resolved.
  • Next Steps in Implementation:
    • Complete the kernel path by collecting data into the kernel struct and then copying this data to a user-level struct using copyout.

A Mermaid diagram to illustrate the system call flow:

flowchart TD
    A[User Process calls get_kd] --> B[Trap into Kernel]
    B --> C[Kernel Dispatch to sys_getkd]
    C --> D[Collect and Populate Data]
    D --> E[Copy Data to User Space using copyout]
    E --> F[Return Control to User Process]

Exploring Markdown, Mermaid, and AI Tools

  • Markdown & Mermaid:
    • The instructor discussed using markdown and Mermaid diagrams for documentation.
    • A specific extension, “Preview Enhance,” was mentioned as necessary to view Mermaid diagrams properly.
  • AI Assistance:
    • The conversation included experimenting with an AI assistant to refine responses and test creative prompts.
  • Other Topics:
    • Briefly touched on developing a coaching tool and updating a website, followed by casual discussions on weather and clothing.

Implementing getkd() System Call in xv6

Overview

The getkd() system call in xv6 facilitates data transfer from kernel memory to user space for displaying process information. The implementation involves:

  1. Kernel Function Setup:
    • Creating a function that allocates space for a kd structure on the stack.
    • Passing the address of this structure to a helper function in proc.c.
  2. Data Collection:
    • Iterating over the process table to collect information (e.g., counting active or free process slots).
    • Populating the kd structure with relevant data.
  3. Data Transfer:
    • Using copyout() to transfer the populated structure from kernel space to a user-space location.

Example Code Snippet

/* Example: Implementation of sys_getkd in xv6-riscv */

#include "types.h"
#include "riscv.h"
#include "defs.h"
#include "proc.h"

int sys_getkd(void) {
    uint64 addr;
    struct kd kd_data;

    // Retrieve the user-space address argument.
    if(argaddr(0, &addr) < 0)
        return -1;

    // Initialize or populate kd_data with process table information.
    // (Pseudocode: Iterate through the process table and fill kd_data)
    for(int i = 0; i < NPROC; i++) {
        if(proc[i].state != UNUSED) {
            // Populate kd_data with information from proc[i]
        }
    }

    // Transfer data from kernel space to user space.
    if(copyout(myproc()->pagetable, addr, (char *)&kd_data, sizeof(kd_data)) < 0)
        return -1;

    return 0;
}

Note: This snippet shows the conceptual flow. The actual implementation may require additional fields and error checking.


System Call 23 Kernel Code Issue

  • Issue Identified:
    • An unknown system call (number 23) was encountered.
  • Investigation Process:
    • Code in syscall.h, sysproc.c, and proc.c was reviewed.
  • Resolution:
    • The error was traced back to recent changes that were not properly saved, leading to the issue.

Resolving Code Mismatch and printf Issue

  • Code Mismatch:
    • The instructor identified an ordering issue while copying a struct from kernel to user space.
    • The solution involved ensuring the proper destination address was used when performing the copy.
  • Printf Data Type Issue:
    • The printf() function did not support the expected data type.
    • A workaround was developed and verified to pass the correct address.

Resolving Variable Scoping Bug

  • Problem:
    • A bug was introduced due to using the same variable name (p) in different scopes.
  • Solution:
    • Renaming the variable or using a confined local scope resolved the copy operation failure.
  • Implication for Future Work:
    • Students are encouraged to apply this fix when implementing kernel loops that copy process information into an array of structs for user space.

Conclusion

The meeting covered a broad range of topics from system call implementation in xv6 to practical debugging techniques. The emphasis was on clear data transfer between the kernel and user space, proper synchronization in process management, and hands-on problem-solving. Students were encouraged to follow the outlined next steps—particularly in implementing the PS command and actively participating in the upcoming hackathon.