Home Page > > Details

CSc 360: Operating Systems

 1 CSc 360: Operating Systems (Fall 2019)

Programming Assignment 2 (P2)
Multi-Thread Scheduling (MTS)
23 Spec Out: Oct 4, 2019
4 Design Due: Oct 18, 2019
5 Code Due: Nov 1, 2019
6 1 Introduction
7 In P1 (Simple Shell Interpreter, SSI), you have built a shell environment to interact with the host operating system.
8 Good job! But very soon you find that SSI is missing one of the key features in a real multi-process or multi-thread
9 operating system: scheduling, i.e., all processes or threads created by your SSI are still scheduled by the host operating
10 system, not yours! Interested in building a multi-thread scheduling system for yourself? In this assignment, you will
11 learn how to use the three programming constructs provided by the posix pthread library:
12 1. threads
13 2. mutexes
14 3. condition variables (convars)
15 to do so. Your goal is to construct a simulator of an automated control system for the railway track shown in Figure 1
16 (i.e., to emulate the scheduling of multiple threads sharing a common resource in a real operating system).
17 As shown in Figure 1, there are two stations (for high and low priority trains) on each side of the main track. At
18 each station, one or more trains are loaded with commodities. Each train in the simulation commences its loading
19 process at a common start time 0 of the simulation program. Some trains take more time to load, some less. After
20 a train is loaded, it patiently awaits permission to cross the main track, subject to the requirements specified in
21 Section 2.2. Most importantly, only one train can be on the main track at any given time. After a train finishes
22 crossing, it magically disappears. You will use threads to simulate the trains approaching the main track from two
23 different directions, and your program will schedule between them to meet the requirements in Section 2.2.
24 You will use C or C++ and the Linux workstation in ECS242 or ECS348 or the linux.csc.uvic.ca cluster to
25 implement and test your work.
26 2 Trains
27 Each train, which will be simulated by a thread, has the following attributes:
28 1. Number: an integer uniquely identifying each train.
east−bound West East
high−priority
low−priority
east−bound
east−bound
main track
west−bound
high−priority
west−bound
low−priority
west−bound
Figure 1: The railway system under consideration.
1
29 2. Direction:
30 • If the direction of a train is Westbound, it starts from the East station and travels to the West station.
31 • If the direction of a train is Eastbound, it starts from the West station and travels to the East station.
32 3. Priority: The priority of the station from which it departs.
33 4. Loading Time: The amount of time that it takes to load it (with goods) before it is ready to depart.
34 5. Crossing Time: The amount of time that the train takes to cross the main track.
35 Loading time and crossing time are measured in 10ths of a second. These durations will be simulated by having
36 your threads, which represent trains, usleep() for the required amount of time.
37 2.1 Step 1: Reading the input file
38 Your program (mts) will accept one parameter on the command line:
39 • The parameter is the name of the input file containing the definitions of the trains.
40 2.1.1 Input file format
41 The input files have a simple format. Each line contains the information about a single train, such that:
42 1. The first field specifies the direction of the train. It is one of the following four characters:
43 e, E, w, or W
44 e or E specify a train headed East (East-Bound): e represents an east-bound low-priority train, and E represents
45 an east-bound high-priority train;
46 w or W specify a train headed West (West-Bound): w presents a west-bound low-priority train, and W represents
47 a west-bound high-priority train.
48 2. Immediately following is an integer that indicates the loading time of the train.
49 3. Immediately following is an integer that indicates the crossing time of the train.
50 4. A newline (\n) ends the line.
51 Trains are numbered sequentially from 0 according to their order in the input file. You need to use strtok() to
52 handle the line. More efficiently, you can use fscanf()
53 2.1.2 An Example
54 The following file specifies three trains, two headed East and one headed West.
55 e 10 6
56 W 6 7
57 E 3 10
58 It implies the following list of trains:
Train No. Priority Direction Loading Time Crossing Time
0 low East 1.0s 0.6s
1 high West 0.6s 0.7s
2 high East 0.3s 1.0s
59
60 Note: Observe that Train 2 is actually the first to finish the loading process.
2
61 2.2 Step 2: Simulation Rules
62 The rules enforced by the automated control system are:
63 1. Only one train is on the main track at any given time.
64 2. Only loaded trains can cross the main track.
65 3. If there are multiple loaded trains, the one with the high priority crosses.
66 4. If two loaded trains have the same priority, then:
67 (a) If they are both traveling in the same direction, the train which finished loading first gets the clearance
68 to cross first. If they finished loading at the same time, the one appeared first in the input file gets the
69 clearance to cross first.
70 (b) If they are traveling in opposite directions, pick the train which will travel in the direction opposite of
71 which the last train to cross the main track traveled. If no trains have crossed the main track yet, the
72 Eastbound train has the priority.
73 (c) To avoid starvation, if there are three trains in the same direction traveled through the main track back
74 to back, the trains waiting in the opposite direction get a chance to dispatch one train if any.
75 2.3 Step 3: Output
76 For the example, shown in Section 2.1.2, the correct output is:
77 00:00:00.3 Train 2 is ready to go East
78 00:00:00.3 Train 2 is ON the main track going East
79 00:00:00.6 Train 1 is ready to go West
80 00:00:01.0 Train 0 is ready to go East
81 00:00:01.3 Train 2 is OFF the main track after going East
82 00:00:01.3 Train 1 is ON the main track going West
83 00:00:02.0 Train 1 is OFF the main track after going West
84 00:00:02.0 Train 0 is ON the main track going East
85 00:00:02.6 Train 0 is OFF the main track after going East
86 You must:
87 1. print the arrival of each train at its departure point (after loading) using the format string, prefixed by the
88 simulation time:
89 "Train %2d is ready to go %4s"
90 2. print the crossing of each train using the format string, prefixed by the simulation time:
91 "Train %2d is ON the main track going %4s"
92 3. print the arrival of each train (at its destination) using the format string, prefixed by the simulation time:
93 "Train %2d is OFF the main track after going %4s"
94 where:
95 • there are only two possible values for direction: "East" and "West"
96 • trains have integer identifying numbers. The ID number of a train is specified implicitly in the input file. The
97 train specified in the first line of the input file has ID number 0.
98 • trains have loading and crossing times in the range of [1, 99].
3
99 2.4 Manual Pages
100 Be sure to study the man pages for the various functions to be used in the assignment. For example, the man page
101 for pthread create can be found by typing the command:
102 $ man pthread create
103 At the end of this assignment you should be familiar with the following functions:
104 1. File access functions:
105 (a) atoi
106 (b) fopen
107 (c) feof
108 (d) fgets and strtok and more efficiently you can use fscanf
109 (e) fclose
110 2. Thread creation functions:
111 (a) pthread create
112 (b) pthread exit
113 (c) pthread join
114 3. Mutex manipulation functions:
115 (a) pthread mutex init
116 (b) pthread mutex lock
117 (c) pthread mutex unlock
118 4. Condition variable manipulation functions:
119 (a) pthread cond init
120 (b) pthread cond wait
121 (c) pthread cond broadcast
122 (d) pthread cond signal
123 It is absolutely critical that you read the man pages, and attend the tutorials.
124 Your best source of information, as always, is the man pages.
125 For help with the posix interface (in general):
126 http://www.opengroup.org/onlinepubs/007908799/
127 For help with posix threads:
128 http://www.opengroup.org/onlinepubs/007908799/xsh/pthread.h.html
129 A good overview of pthread can be found at: http://computing.llnl.gov/tutorials/pthreads/
130 3 Tutorial Schedule
131 In order to help you finish this programming assignment on time successfully, the schedule of this assignment has
132 been synchronized with both the lectures and the tutorials. There are four tutorials arranged during the course of
133 this assignment, including the one on pthread. NOTE: Please do attend the tutorials and follow the tutorial
134 schedule closely.
Date Tutorial Milestones
Oct 8/10/11 p2 spec go-thru, pthread, mutex and condition variable calls multi-threading programming
Oct 15/17/18 design review/hints design and code skeleton done
Oct 22/24/25 feedback on design and pthread programming code almost done
Oct 29/31 Nov 1 testing and last-minute help final deliverable
135 4 Submission: Deliverable A (Design Due: Oct 18, 2019)
136 You will write a design document which answers the following questions. It is recommended that you think through
137 the following questions very carefully before answering them.
138 Unlike P1, no amount of debugging will help after the basic design has been coded. Therefore, it is very important
139 to ensure that the basic design is correct. Answering the following questions haphazardly will basically ensure that
140 Deliverable B does not work.
141 So think about the following for a few days and then write down the answers.
142 1. How many threads are you going to use? Specify the work that you intend each thread to perform.
143 2. Do the threads work independently? Or, is there an overall “controller” thread?
144 3. How many mutexes are you going to use? Specify the operation that each mutex will guard.
145 4. Will the main thread be idle? If not, what will it be doing?
146 5. How are you going to represent stations (which are collections of loaded trains ready to depart)? That is, what
147 type of data structure will you use?
148 6. How are you going to ensure that data structures in your program will not be modified concurrently?
149 7. How many convars are you going to use? For each convar:
150 (a) Describe the condition that the convar will represent.
151 (b) Which mutex is associated with the convar? Why?
152 (c) What operation should be performed once pthread cond wait() has been unblocked and re-acquired the
153 mutex?
154 8. In 15 lines or less, briefly sketch the overall algorithm you will use. You may use sentences such as:
155 If train is loaded, get station mutex, put into queue, release station mutex.
156 The marker will not read beyond 15 lines.
Note: Please submit answers to the above on 8.5′′×11′′
157 paper in 10pt font, single spaced with 1” margins left,
158 right, top, and bottom. 2 pages maximum (cover page excluded), on Oct 18 through connex. The design counts for
159 5%.
160 5 Submission: Deliverable B (Code Due: Nov 1, 2019)
161 The code is submitted through connex. The tutorial instructor will give the detailed instruction in the tutorial.
162 5.1 Submission Requirements
163 Your submission will be marked by an automated script. The script (which is not very smart) makes certain
164 assumptions about how you have packaged your assignment submission. We list these assumptions so that your
165 submission can be marked thus, in a timely, convenient, and hassle-free manner.
166 1. The name of the submission file must be p2.tar.gz
167 2. p2.tar.gz must contain all your files in a directory named p2
168 3. Inside the directory p2, there must be a Makefile. Also there shall be a test input file created by you.
169 4. Invoking make on it must result in an executable named mts being built, without user intervention.
170 5. You may not submit the assignment with a compiled executable and/or object (.o) files; the script will delete
171 them before invoking make.
172 Note:
173 1. The script will give a time quota of 1 minute for your program to run on a given input. This time quota is
174 given so that non-terminating programs can be killed.
175 Since your program simulates train crossing delays in 10ths of a second, this should not be an issue, at all.
176 2. Follow the output rules specified in the assignment specification, so that the script can tally the output produced
177 by your program against text files containing the correct answer.
178 3. The markers will read your C code after the script has run to ensure that the pthread library is used as
179 required.
180 The code counts for 15%.
181 6 Plagiarism
182 This assignment is to be done individually. You are encouraged to discuss the design of the solution with your
183 classmates, but each student must implement their own assignment. The markers will submit your code to an
184 automated plagiarism detection service.
185 NOTE: Do not request/give source code from/to others; do not put/use code at/from public
186 repositories such as github or so.
 
Contact Us - Email:99515681@qq.com    WeChat:codinghelp
Programming Assignment Help!