CSc -Programming Assignment 2 (P2) Multi-Thread Scheduling (MTS) Solution

$30.00 $24.00

6 1 Introduction In P1 (Simple Shell Interpreter, SSI), you have built a shell environment to interact with the host operating system. Good job! But very soon you find that SSI is missing one of the key features in a real multi-process or multi-thread operating system: scheduling, i.e., all processes or threads created by your…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

6 1 Introduction

  1. In P1 (Simple Shell Interpreter, SSI), you have built a shell environment to interact with the host operating system.

  1. Good job! But very soon you find that SSI is missing one of the key features in a real multi-process or multi-thread

  1. operating system: scheduling, i.e., all processes or threads created by your SSI are still scheduled by the host operating

  1. system, not yours! Interested in building a multi-thread scheduling system for yourself? In this assignment, you will

  2. learn how to use the three programming constructs provided by the POSIX pthread library:

12

13

14

  1. threads

  1. mutexes

  1. condition variables (convars)

  1. to do so. Your goal is to construct a simulator of an automated control system for the railway track shown in Figure 1

  1. (i.e., to emulate the scheduling of multiple threads sharing a common resource in a real operating system).

  1. As shown in Figure 1, there are two stations (for high and low priority trains) on each side of the main track. At

  1. each station, one or more trains are loaded with commodities. Each train in the simulation commences its loading

  1. process at a common start time 0 of the simulation program. Some trains take more time to load, some less. After

  1. a train is loaded, it patiently awaits permission to cross the main track, subject to the requirements specified in

  1. Section 2.2. Most importantly, only one train can be on the main track at any given time. After a train finishes

  1. crossing, it magically disappears. You will use threads to simulate the trains approaching the main track from two

  1. different directions, and your program will schedule between them to meet the requirements in Section 2.2.

  1. You will use C or C++ and the Linux workstation in ECS242 or ECS348 or the linux.csc.uvic.ca cluster to

  1. implement and test your work.

  1. 2 Trains

  1. Each train, which will be simulated by a thread, has the following attributes:

  1. 1. Number: an integer uniquely identifying each train.

east−bound

West

East

west−bound

high−priority

high−priority

west−bound

east−bound

main track

east−bound

west−bound

low−priority

low−priority

Figure 1: The railway system under consideration.

1

29

30

31

32

33

34

  1. Direction:

    • If the direction of a train is Westbound, it starts from the East station and travels to the West station.

    • If the direction of a train is Eastbound, it starts from the West station and travels to the East station.

  1. Priority: The priority of the station from which it departs.

  1. Loading Time: The amount of time that it takes to load it (with goods) before it is ready to depart.

  1. Crossing Time: The amount of time that the train takes to cross the main track.

  1. Loading time and crossing time are measured in 10ths of a second. These durations will be simulated by having

  2. your threads, which represent trains, usleep() for the required amount of time.

  1. 2.1 Step 1: Reading the input file

  1. Your program (mts) will accept one parameter on the command line:

  1. The parameter is the name of the input file containing the definitions of the trains.

  1. 2.1.1 Input file format

  1. The input files have a simple format. Each line contains the information about a single train, such that:

42

43

44

45

46

47

48

49

50

1. The first field specifies the direction of the train. It is one of the following four characters:

e, E, w, or W

e or E specify a train headed East (East-Bound): e represents an east-bound low-priority train, and E represents an east-bound high-priority train;

w or W specify a train headed West (West-Bound): w presents a west-bound low-priority train, and W represents

a west-bound high-priority train.

  1. Immediately following is an integer that indicates the loading time of the train.

  1. Immediately following is an integer that indicates the crossing time of the train.

  1. A newline (\n) ends the line.

  1. Trains are numbered sequentially from 0 according to their order in the input file. You need to use strtok() to

  2. handle the line. More efficiently, you can use fscanf()

  1. 2.1.2 An Example

  1. The following file specifies three trains, two headed East and one headed West.

55

56

57

e 10 6

W 6 7

E310

  1. It implies the following list of trains:

Train No.

Priority

Direction

Loading Time

Crossing Time

0

low

East

1.0s

0.6s

59

1

high

West

0.6s

0.7s

2

high

East

0.3s

1.0s

  1. Note: Observe that Train 2 is actually the first to finish the loading process.

2

  1. 2.2 Step 2: Simulation Rules

  1. The rules enforced by the automated control system are:

  1. 1. Only one train is on the main track at any given time.

  1. 2. Only loaded trains can cross the main track.

  1. 3. If there are multiple loaded trains, the one with the high priority crosses.

  1. 4. If two loaded trains have the same priority, then:

67

68

69

70

71

72

  1. If they are both traveling in the same direction, the train which finished loading first gets the clearance to cross first. If they finished loading at the same time, the one appeared first in the input file gets the clearance to cross first.

  1. If they are traveling in opposite directions, pick the train which will travel in the direction opposite of which the last train to cross the main track traveled. If no trains have crossed the main track yet, the Eastbound train has the priority.

73 2.3 Step 3: Output

  1. For the example, shown in Section 2.1.2, the correct output is:

75

76

77

78

79

80

81

82

83

00:00:00.3 Train

2

is ready to go

East

00:00:00.3 Train

2

is ON the main

track going East

00:00:00.6 Train

1

is ready to go

West

00:00:01.0 Train

0

is ready to go

East

00:00:01.3 Train

2

is OFF the main track after going East

00:00:01.3 Train

1

is ON the main track going West

00:00:02.0 Train

1

is OFF the main track after going West

00:00:02.0 Train

0

is ON the main track going East

00:00:02.6 Train

0

is OFF the main track after going East

  1. You must:

  1. 1. print the arrival of each train at its departure point (after loading) using the format string, prefixed by the

  1. simulation time:

  1. “Train %2d is ready to go %4s”

  1. 2. print the crossing of each train using the format string, prefixed by the simulation time:

  1. “Train %2d is ON the main track going %4s”

  1. 3. print the arrival of each train (at its destination) using the format string, prefixed by the simulation time:

  1. “Train %2d is OFF the main track after going %4s”

  1. where:

93

94

95

96

  • there are only two possible values for direction: “East” and “West”

  • trains have integer identifying numbers. The ID number of a train is specified implicitly in the input file. The train specified in the first line of the input file has ID number 0.

  • trains have loading and crossing times in the range of [1, 99].

97 2.4 Manual Pages

98 Be sure to study the man pages for the various functions to be used in the assignment. For example, the man page

99 for pthread create can be found by typing the command:

100 $ man pthread create

101 At the end of this assignment you should be familiar with the following functions:

102 1. File access functions:

3

103

104

105

106

107

  1. atoi

  1. fopen

  1. feof

  1. fgets and strtok and more efficiently you can use fscanf

  1. fclose

  1. 2. Thread creation functions:

109

110

111

  1. pthread create

  1. pthread exit

  1. pthread join

  1. 3. Mutex manipulation functions:

113

114

115

  1. pthread mutex init

  1. pthread mutex lock

  1. pthread mutex unlock

  1. 4. Condition variable manipulation functions:

  1. (a) pthread cond init

  1. (b) pthread cond wait

  1. (c) pthread cond broadcast

  1. (d) pthread cond signal

  1. It is absolutely critical that you read the man pages, and attend the tutorials.

  2. Your best source of information, as always, is the man pages.

  1. For help with the POSIX interface (in general):

  1. http://www.opengroup.org/onlinepubs/007908799/

  1. For help with POSIX threads:

  1. http://www.opengroup.org/onlinepubs/007908799/xsh/pthread.h.html

  1. A good overview of pthread can be found at: http://computing.llnl.gov/tutorials/pthreads/

  1. 3 Tutorial Schedule

  1. In order to help you finish this programming assignment on time successfully, the schedule of this assignment has

  1. been synchronized with both the lectures and the tutorials. There are four tutorials arranged during the course of

  1. this assignment, including the one on pthread. NOTE: Please do attend the tutorials and follow the tutorial

  1. schedule closely.

Date

Tutorial

Milestones

Jan 30/31

pthread, mutex and condition variable calls

multi-threading programming

Feb 6/7

P2 spec go-through, design review/hints

design and code skeleton

Feb 13/14

reading break, no tutorials

design finished

Feb 20/21

feedback on design and pthread programming

code done

Feb 27/28

testing and last-minute help

final deliverable

4

  1. 4 Submission: Deliverable 1 (Design Due: Feb 19, 2018)

  1. You will write a design document which answers the following questions. It is recommended that you think through

  1. the following questions very carefully before answering them.

  1. Unlike P1, no amount of debugging will help after the basic design has been coded. Therefore, it is very important

  1. to ensure that the basic design is correct. Answering the following questions haphazardly will basically ensure that

  1. Deliverable 2 does not work.

  1. So think about the following for a few days and then write down the answers.

  1. 1. How many threads are you going to use? Specify the work that you intend each thread to perform.

  1. 2. Do the threads work independently? Or, is there an overall “controller” thread?

  1. 3. How many mutexes are you going to use? Specify the operation that each mutex will guard.

  1. 4. Will the main thread be idle? If not, what will it be doing?

  1. 5. How are you going to represent stations (which are collections of loaded trains ready to depart)? That is, what

  1. type of data structure will you use?

  1. 6. How are you going to ensure that data structures in your program will not be modified concurrently?

  1. 7. How many convars are you going to use? For each convar:

148

149

150

151

  1. Describe the condition that the convar will represent.

  1. Which mutex is associated with the convar? Why?

  1. What operation should be performed once pthread cond wait() has been unblocked and re-acquired the mutex?

  1. 8. In 15 lines or less, briefly sketch the overall algorithm you will use. You may use sentences such as:

153

154

If train is loaded, get station mutex, put into queue, release station mutex.

The marker will not read beyond 15 lines.

  1. Note: Please submit answers to the above on 8.5′′×11′′ paper in 10pt font, single spaced with 1” margins left,

  1. right, top, and bottom. 2 pages maximum (cover page excluded), on Feb 19 through connex. The design counts for

  1. 5%.

  1. 5 Bonus Features

  1. Only a simple control system simulator with limited scheduling and synchronization features is required in this assign-

  1. ment. However, students have the option to propose bonus features to include more scheduling and synchronization

  1. functions (e.g., to address scheduling fairness issues).

  1. If you want to design and implement a bonus feature, you should contact the course instructor for permission

  1. before the due date of Deliverable 1, and clearly indicate the feature in the submission of Deliverable 2. The credit

  1. for the correctly implemented bonus feature will not exceed 20% of the full marks for this assignment.

  1. 6 Submission: Deliverable 2 (Code Due: March 5, 2018)

  1. The code is submitted through connex. The tutorial instructor will give the detailed instruction in the tutorial.

  1. 6.1 Submission Requirements

  1. Your submission will be marked by an automated script. The script (which is not very smart) makes certain

  1. assumptions about how you have packaged your assignment submission. We list these assumptions so that your

  1. submission can be marked thus, in a timely, convenient, and hassle-free manner.

  1. 1. The name of the submission file must be p2.tar.gz

5

  1. 2. p2.tar.gz must contain all your files in a directory named p2

  1. 3. Inside the directory p2, there must be a Makefile. Also there shall be a test input file created by you.

  1. 4. Invoking make on it must result in an executable named mts being built, without user intervention.

  1. 5. You may not submit the assignment with a compiled executable and/or object (.o) files; the script will delete

  2. them before invoking make.

  1. Note:

  1. 1. The script will give a time quota of 1 minute for your program to run on a given input. This time quota is

  1. given so that non-terminating programs can be killed.

  1. Since your program simulates train crossing delays in 10ths of a second, this should not be an issue, at all.

  1. 2. Follow the output rules specified in the assignment specification, so that the script can tally the output produced

  1. by your program against text files containing the correct answer.

  1. 3. The markers will read your C code after the script has run to ensure that the pthread library is used as

  1. required.

  1. The code counts for 15%.

  1. 7 Plagiarism

  1. This assignment is to be done individually. You are encouraged to discuss the design of the solution with your

  1. classmates, but each student must implement their own assignment. The markers will submit your code to an

  1. automated plagiarism detection service.

  1. NOTE: Do not request/give source code from/to others; do not put/use code at/from public

  1. repositories such as github or so.

6

CSc -Programming Assignment 2 (P2) Multi-Thread Scheduling (MTS) Solution
$30.00 $24.00