A fork of the xv6 kernel with shared memory components.
- Semaphores:
sem.csem.h - Shared memory management:
shm.cshm.h - In
argptr(syscall.c), a check was added to handle the case where the address is in the shared memory space.argptris used insysproc.cfor bothshmgetandshmrem, assh_key_tis simply a pointer tosh_key_ttused by the kernel. - In
allocproc(proc.c), the bitmap for free shared pages positions is initialized to 0. Infork, theshmcopyfunction is called to map the parent's shared pages to the same positions in the child. Inwait, when an exited child is found,shmfreeis called to free any shared pages that only this process had.
The key for each page is a char[16].
There is a shmtable structure protected by a spinlock that keeps track of the following:
- the page key
- whether the page is in use
- the address returned by
kalloc - processes that share the page, and where in the shared space.
For each process we keep a bitmap (char[4]) with its mappings.
- work1:
Forks a child that writes its PID to a shared buffer. The parent then reads the correct data from the buffer. If the child performs
shmremand then tries to write, a trap 14 occurs as expected since the mapping for the child no longer exists. - work2: Forks 15 processes that each increment one of the 1024 values in each of the 32 shared pages. The master (the one that did the fork) calculates the sum. Using semaphores, the sum is correct, whereas without them, a race condition is observed.
- work3: Implements the bounded buffer problem with 3 semaphores. The program runs continuously, with consumers trying to write and producers trying to read from a buffer with 30 shared pages. Without semaphores, a race condition is observed here as well.
- work4:
Performs various
shmget,shmrem, andforkoperations to test extreme conditions (e.g., attempting toshmrema page that does not exist).