Operating Systems, CSCE 4413
Homework Assignment 3
As before, this assignment is to be done on your own, and with the
help of the textbook or other legitimate resources.
Part 1: Written Exercises
Write the answers to these in plain text and submit the file to the
Homework Submission Page.
- Suppose that in a real-time system that P1 has a computation
requirement of 1 second with a period of 6 seconds, P2 has a
computation requirement of 1 second with a period of 4 seconds,
and P3 has a computation requirement of 1 second with a period of
3 seconds.
- Does the Rate Monotonic Scheduling Algorithm guarantee that a
feasible schedule can be built? Show your calculations.
- Use the RMS algorithm to develop a real-time schedule for
processes P1, P2, and P3.
- What is the processor utilization for your scheduled system?
- From the text, do exercises 7.1, 7.2, 7.4, 7.7, and 7.18.
Part 2: Programming Exercise
The goal of this assignment is to
write a program in which multiple threads
write to a common shared resource.
You may think of your threads as processing values that are being read
from several real-time sensors.
You will
need to do this exercise in Linux. You may use either C or C++ and
the pthreads library, or you may do your assignment in Java. If you
do your assignment in Java, you may need to look up some background
material on your own.
To Prepare:
- Read the text and on-line information on pthreads.
- Study the class notes on how to use the thread
library calls, including: pthread_create,
pthread_join,
pthread_exit,
pthread_mutex_init, pthread_mutex_lock, and
pthread_mutex_signal.
Overview:
Your program will create up to four
threads. Each thread is responsible for calculating a sum of integer
values from a distinct disk file.
You can think of each value in the file as a value returned from a
real-time sensor.
Each thread should access its own disk file which is separate from the
files accessed by other threads.
The running total should be maintained in
memory that is shared between the threads. Access to the total
should be synchronized with a mutual exclusion lock.
Once EOF is reached, each thread should
output its subtotal and count of lines read. The main thread should
output the final total at the end.
Specifications:
Write a C or C++ program using the pthread library that consists of
code for main, a user thread, and supporting
functions as needed. The code for main must do the following:
- Accept a command line parameter which is the number of sensors to
monitor and threads that the program will create. Ensure that the parameter is a
valid integer between 1 and 4. If the command line parameter is
invalid, print out a usage message and exit.
- The main program should open a file for each thread, from 1 up to
4. The filenames should default to
"sensor1.dat", "sensor2.dat", "sensor3.dat", and "sensor4.dat".
Sample data files are located at
sensor1.dat,
sensor2.dat,
sensor3.dat, and
sensor4.dat.
The main program should open the files before any thread
operations are performed.
- Main should declare an initial global sum with a value
of 0.
- The main program should create a thread for each "sensor". Pass as
a parameter to the thread a thread identifier (1, 2, 3, or 4).
You may also optionally pass as a parameter the open file pointer
of the file that
represents the values that the thread will process. Thread 1
should read the sensor1.dat file, thread 2 should read the
sensor2.dat file, and so on.
- The main program should wait for the user threads to complete
before it exits. Before exiting it should close the open files.
- The final global sum should be printed before main exits.
The code for sensor thread must do the following:
- Initialize a local sum variable that is local to the thread. The
initial value should be 0.
- Read from the file
according to its thread identifier until all lines
in the file are read. Assume that each line contains a separate
integer value.
- For each value read, add it to the local sum,
keep a count of the number of values read, and add the value to the running
total, being sure to protect access to the running total with a mutex lock.
- When EOF is reached, each thread should print out its local
subtotal and it local count
of the number of lines read. Ensure that
the output from the various threads does not interleave on the
screen.
- The thread should call pthread_exit() to terminate.
As always, ensure that you use good
programming practice (e.g., put in a sufficient number of comments to explain
what your program does, use good formatting, and so on).
Submit: Your source file and Makefile to the
homework submission page. Be sure
that your source file compiles and executes on Linux.
Enjoy!
Send comments or suggestions to
aapon@uark.edu