Skip to content

DinSanGun/XV6-RISCV-3

Repository files navigation

xv6 Assignment 3 — Shared Memory & Multi-Process Logging

📌 Overview

This assignment extends xv6 with shared memory support and builds a multi-process logging system on top of it.
It integrates virtual memory manipulation, page-table operations, and synchronization using atomic primitives.


🧠 1. Shared Memory Mechanism

🎯 Objective

Enable one process to map a region of its virtual memory into another process’s address space — similar to real OS IPC mechanisms.


🔧 Kernel Additions

map_shared_pages(src_proc, dst_proc, src_va, size)

  • Translates virtual→physical pages in the source.
  • Maps them into the destination process’s page table.
  • Handles alignment (page rounding) and offset correction.
  • Copies permission flags and adds a new shared-page marker (PTE_S).
  • Updates sz for the destination process.

unmap_shared_pages(p, va, size)

  • Safely removes mappings without freeing shared physical pages unless owned by the process.
  • Prevents double-frees during process termination.

🛠️ System Calls

sys_map_shared_pages();
sys_unmap_shared_pages();

🧪 Test Program: shmem_test.c

Validates:

  • Parent/child shared communication
  • Correct string exchange
  • Proper unmapping and allocation after unmap
  • Cleanup behavior when child terminates without unmapping

📝 2. Multi-Process Logging System

🎯 Objective

Use the shared memory abstraction to create a concurrent logging buffer written to by multiple children.


🔧 Buffer Layout

Each log entry has:

[ 32-bit header | message bytes ... ]

Header format:

  • Upper 16 bits → child index
  • Lower 16 bits → message length

⚙️ Synchronization

Used:

__sync_val_compare_and_swap()

for atomic header claims.

Alignment rules:

addr = (addr + 3) & ~3;

Ensures 4-byte alignment for atomic header writes.


🧪 Test Program: log_test.c

  • Forks multiple children.
  • Parent allocates and maps a shared buffer.
  • Children write messages until buffer is full.
  • Parent scans and prints messages.

🛠️ Skills Demonstrated

  • Kernel virtual memory manipulation
  • Page-table design and multi-process address space management
  • Atomic synchronization in shared memory
  • Concurrent system design in a minimal OS
  • Debugging low-level interactions between processes

📜 License

For educational and demonstration purposes only.

About

Memory Management mini-project in Operating Systems

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors