xv6 machine problem 2: Kernel threads


For this machine problem you will be adding more system calls to xv6 that:

  1. Support kernel-level threading, so that concurrency within a single user-level process is possible.
  2. Provide a basic synchronization mechanism -- the mutex lock -- for said threads to use.

Threading API

You will implement the following system calls to support thread creation and management:

int thread_create(void (*tmain)(void *), void *stack, void *arg);

int thread_join(void **stack);


To allow your newly minted threads to coordinate critical sections and safely share data, you will implement the following system calls that expose a mutex lock. The lock will be implemented internally using a spinlock.

int mtx_create(int locked);

int mtx_lock(int lock_id);

int mtx_unlock(int lock_id);

Note that you can build arbitrarily more sophisticated synchronization mechanisms (e.g., semaphores) in userspace by leveraging this mutex lock.


To test your code, you should write a program named pctest that implements the producer/consumer problem by sharing a bounded buffer between a parent and child (or sibling) threads, using the mutex as a primitive for implementing synchronization. The bounded buffer itself may be as simple as a fixed sized array (with global indices to mark the in/out positions), or a (non-thread-safe) data structure that you implement separately.

Your program should run for sufficiently long to demonstrate concurrency (i.e., the consumer and producer should preempt each other), and you should also output the values as they are produced and consumed so as to demonstrate that they are communicated wholly, and in order. To keep things simple, the values may just be ordinal values; e.g., sequential integer values.

Add this test program (pctest.c) to your git repository, and be sure to commit and push it as part of your submission.


As before, start by adding any new source files you created with "git add FILENAME", committing your work with "git commit -am "Your commit message"", then pushing your commits ot the private shared repository with "git push origin master".