CS 354: Fall 2018
Lab 1: Creating and Synchronizing Processes in XINU
Due: Sunday, September 16th, 2018, before 11:59 PM
- Understand how to create processes in XINU and pass parameters to them
- Create multiple processes that all need to access a shared buffer
- Perform synchronization among processes using semaphores
- Understand process priority
We will use a different tar.gz file for this lab that includes two extra files and a different main.c. Create a directory for lab1, change to that directory, and then untar and unzip the tar.gz file in that directory using the following command:
tar zxvf /u/u3/cs354/Fall2018/lab1/xinu-x86.tar.gz
Read Chapters 1-2 of the textbook, and study the prototypes of the functions: create, resume, kill, semcreate, semdelete, wait, signal, sleep, sleepms, kprintf.
4. What to do?
In Lab 0, we experimented with creating multiple concurrent processes. In this lab, we will use process synchronization primitives (semaphores).
We will simulate a producer-consumer system that consists of one or more producer processes and one or more consumer processes and a single shared storage buffer that has a finite capacity, e.g., circular buffer.
Each producer repeatedly inserts a set of N items into the shared storage, and then delays for a specified period of time before inserting the next set.
- Each consumer repeatedly extracts a set of K items from the shared storage and then delays for a specified period of time before extracting the next K items.
- Each producer and each consumer is implemented as a XINU process.
Along with the main code for XINU, this tarball contains the following files:
- include/prod_cons.h: Contains parameter declarations for the producer/consumer code. Feel free to change these parameters during the testing of your code, but do not place any variables specific to your implementation in prod_cons.h as we will replace that file during grading.
- system/prod_cons.c: Contains the function declarations for the producer/consumer simulation that you must implement.
- system/main.c: The main XINU process that is different from the ones used in Lab 0.
In the file "system/prod_cons.c", there are function declarations for void insert_buffer(...) and uint8 remove_buffer().
Remember that we need mutual exclusion here!
- Complete void insert_buffer(...) to insert an item into the global shared buffer (declared as uint8 buffer). This function will be used by the producer.
- Complete uint8 remove_buffer() to remove an item from the global shared buffer.
This function will be used by the consumer.
In the file "system/prod_cons.c", there are function declarations for void producer(...) and void consumer(...). These functions are the producer process and the consumer process, respectively.
Complete the producer and consumer such that:
Each producer repeatedly inserts a set of items into the shared buffer and then delays for a specified time before inserting the next set of items.
Each consumer repeatedly extracts a set of items from the buffer and then delays for a specified amount of time before extracting the next set of items.
Each item that a producer generates contains the 1-character label assigned to the producer (which is how a consumer can tell which producer inserted a given item into the shared buffer).
Producers and consumers must synchronize using semaphores. We must ensure that producers and consumers can proceed concurrently (subject to mutual exclusion of any shared variable access), but a consumer does not try to consume an item that has not been produced yet, and a producer cannot insert into a full buffer.
Semaphores are defined in semaphore.h, and have type "sid32" (in kernel.h). You can use the wait(sid32) and signal(sid32) XINU system calls for synchronization with semaphores. The system calls semcreate and semdelete are used to create/delete semaphores. The system call sleep(int32) makes a process sleep for a given period of time in seconds, and sleepms works similarly in milliseconds. These system calls are defined under the system/ directory in files wait.c, signal.c semcreate.c, semdelete.c and sleep.c, respectively.
In the file "system/prod_cons.c", implement the function void start_prod_con(void) . This is an initialization function that creates all necessary processes and starts the producer/consumer simulation running. For now, set the process priority for all producers and consumers to the same value, 20.
In the file "system/prod_cons.c", complete the function void stop_prod_con(void). This is the function that stops the currently executing producer/consumer simulation.
For tasks 3 and 4, you will want to use the system calls create(), resume() and kill(), all defined in the systems/ folder.
In the file "system/prod_cons.c", complete the function void print_report(void). This function, at the end of the simulation, outputs a report of the counts along with the contents of the shared buffer.
The output should look like this:
Producer A: created xxx items
Producer B: created xxx items
Producer C: created xxx items
Consumer a: deleted XXX items from producer A, XXX items from producer B, and XXX items from producer C
Consumer b: deleted XXX items from producer A, XXX items from producer B, and XXX items from producer C
Consumer c: deleted XXX items from producer A, XXX items from producer B, and XXX items from producer C
The shared buffer contains: xxx items from producer A, xxx items from producer B, and xxx items from producer C
At the end of the simulation, the number of items created and deleted should add up accordingly to ensure that extra items were not produced nor were extra items consumed. For example, the number of he items created by producer A minus the number of items left in the shared buffer from producer A should equal the sum of all items deleted by consumers from producer A.
Make sure not to put any dependent code in main.c or any variable declarations specific to your implementation in prod_cons.h. Put all dependent code only in prod_cons.c.
Task 6 (10 pts Extra Credit):
In your initial implementation, all processes are created with a priority of 20. Experiment with changing the process priorities:
Compare the results by looking at the statistics from print_report. Does changing the process priorities of producers and consumers affect "fair" access to the shared buffer? If so, how? Be sure to give a description using experimental results and make sure you explain your definition of "fair".
- The producers have higher priority than the consumers.
- The consumers have higher priority than the producers.
- Each consumer has a unique priority value.
- Each producer has a unique priority value.
Turn-in Instructions and Written Report
Submit using the turnin command (see below) your complete source code (all of XINU) including the any files you added to complete the lab. In the system/ directory include a PDF file called lab1_analysis.pdf with a report (15 pts) discussing:
- (5 pts) How does your solution guarantee that only one process will attempt to delete an item at a given time?
- (5 pts) In your solution, when a producer needs to insert an item in the shared buffer, are consumers also excluded, or can consumers continue concurrently? Explain.
- (5 pts) When a consumer starts to extract items, does your system guarantee that the consumer will receive contiguous locations in the shared buffer, or do consumers contend for items? Explain.
- Your results from the extra credit experiment if you completed it.
Please be concise! Make sure you put your name on your report, that the file is named exactly as specified (lab1_analysis.pdf), and the file located in the directory specified (system/).
Before submitting any lab assignment throughout the semester, make sure to double-check
to ensure that all requirements and instructions have been followed.
i) Go to the xinu-x86/compile
directory and run "make clean".
ii) Go to the directory of which
your xinu-x86 directory is a subdirectory. (Note: please do not rename xinu-x86
or any of its subdirectories.)
e.g., if /homes/bob/lab1/xinu-x86 is your directory structure, go to
iii) Type the following command
turnin -c cs354 -p lab1 xinu-x86
Remember that re-submitting erases your old submission and its timestamp. If you submit even one second late, it is deducted from your four flexible (late) days. No assignments will be accepted beyond the remaining late days you have.
Please make sure to disable all debugging output before submitting your code for any assignment, and make sure your code is well-commented.