NACHOS Laboratory Assignment 1: Threads and SynchronizationOverviewThis assignment covers:
NACHOS uses three synchronization primitives; semaphores, locks and conditions. The semaphore is the most basic primitive and you find it already implemented in NACHOS. Completing the other two primitives is part of this assignment. You should fully understand their function before you start implementing them. The course book provides only little information about Locks and Conditions. However, Locks and Conditions together form the same functionality as monitors and you can find some information about that in the book. But you might want to look at the DIGITAL report: An Introduction to Programming with Threads (remark: The report uses the term "mutex" where NACHOS uses "lock"). To test your implementation of the primitives you will construct your own synchronized datatype - a bounded buffer. This gives you valuable insight into constructing such datatypes, which you will benefit from in the following labs. You will use primarily locks and conditions, the primitives you have implemented. To run the test, you need to have more than one thread, so this is a good time to have a first look at the NACHOS thread and scheduling subsystem. Threads are not very hard to use in NACHOS, and the scheduling system is already implemented for you to use. Preparation
Answer these questions before starting with the assignment:
The NACHOS frameworkNACHOS is a relatively complex simulator. It simulates three things:
For this assignment you don't have to deal with the full complexity of NACHOS, but rather test some of the subsystems of the NACHOS OS kernel (synchronization, threads and scheduling). Also, you will not use the MIPS emulator, but rather have some threads running in the NACHOS OS kernel. Usually an OS would run on the processor of the system. This is however NOT the case in NACHOS, the OS kernel does NOT run on the emulated MIPS processor, but rather as "ordinary" code in the NACHOS program. This might be confusing at times, but this is because you want to use a debugger to see that your code works properly. NACHOS has a scheduler in place, using a fixed-priority pseudo-preemptive scheme. The first thread on the run queue is run until the thread yields control voluntarily. Later on you will do real preemptive multitasking, but for now the multitasking thus will be non-preemptive (like Microsoft Windows 3.x). However, which thread is taken from the run queue can be randomized (see below). FilesThe working directory for this lab is the /code/threads directory. The following files are the central files for this lab:
Read the following files to understand how to work with threads and scheduling of threads:
You can find an example of a data structure that uses synchronization:
The following files are central files of NACHOS in general, read them to understand how NACHOS works internally:
Assignments in detail
Before you start, you should look at the semaphore class. You are free to use semaphores when implementing the lock and condition classes. In threads/synch.h you shouldn't change any member function definitions, but you are free to add member variables to the classes.
The files threads/synchlist.h and threads/synchlist.cc contain an example of this, implementing a synchronized list, using a lock and a condition if the list is empty. Remember that you must handle both, the case of an empty buffer and a full buffer.
The code already contains an example of how to start up two threads, so read and understand this first. Running and debuggingFor instructions on compiling and running nachos, please have a look at NACHOS: Introduction and installation. To test the synchronization primitives under more realistic conditions, NACHOS can be started with the command: nachos -rs 17 This will give the number 17 as a seed to the random generator, which will then switch between the threads more randomly. Giving the same number in two different executions of the program, will cause NACHOS to switch in the same places both times. To see in detail what your threads do, you may also use the flag -d: nachos -rs 17 -dThis will print out information about task switches, interrupts being turned on and off etc. For the following assignments it will be necessary to use a debugger. So take this lab assignment as a good opportunity to get familiar with the use of a debugger. The GNU debugger ("gdb") works fine with NACHOS. There is a graphical interface to the debugger called "ddd". You execute it by: ddd ./nachos The SWITCH function (switches between threads; part of the scheduling subsystem) is written in assembler. You don't have to understand it fully, it is sufficient to know what it does in general. Observe that when running NACHOS in a debugger, the SWITCH call will probably confuse the debugger. Test programA test program for testing your bounded buffer code (replace your threadtest.cc) : threadtest.cc. Uses
Helpful Information
Next Laboratory work |