首页 > > 详细

调试Web开发|讲解Java程序|解析Java程序|解析C/C++编程

DUE Before: Before the end of your lab session
Page 1 of 5
CSE-381: Systems 2
Exercise #12
Max Points: 40
Objective: The objective of this exercise is to:
• Practice for Exam #2
• Self-assess your preparedness on concepts and programming skills for Exam #2
• Build self-confidence and skills for technical part of job interviews
Submission: This exercise consists must be submitted separately:
• Programming part – submit via the Canvas CODE plug-in.
Have an exam like setting for yourself in this exercise. Only when you are really stuck, seek
help from your instructor or TA.
When working on these programming problems try to stick to information
included in CommonMethodsAndCommands.pdf on Canvas. This will help
you prepare for the exam and importantly for interviews.
Quick review
Estimated time to complete: < 15 minutes
It would be helpful for you to review the following video that
summarizes key threading concepts in this exercise and Exam #2:
https://youtu.be/kOP2wr7XSiE. Please watch this video at 1.5´ or 2´
speed.
Setup
Estimated time to complete: < 5 minutes
1. Via NetBeans, on the Linux server os1.csi.miamioh.edu, create a Miami
University C++ Project named exercise12.
2. Download the supplied files and scp them to your NetBeans project folder on the server.
Of course, you can scp all of the files in one shot as shown below –
scp main.cpp exercise12.cpp os1.csi.miamioh.edu:NetBeansProject/exercise12
3. Add source files to your NetBeans project.
4. Note: You should not be modifying main.cpp (or submitting main.cpp)
DUE Before: Before the end of your lab session
Page 2 of 5
Testing
You can test your solution for each one of the questions by setting the following command-line
arguments in NetBeans as shown below:
In the command-line arguments above, change q1, to q2, q3 or q4 for testing the other
questions.
Q1: Coordinating threads (sleep-wake up)
Estimated time to complete: 15 minutes
The threadMain method is called on many threads, with id (i.e., 0, 1, 2, …) indicating the
logical number of the thread. The method has been implemented to print data in order of id
value, as shown in the adjacent sample output. Rewrite the method to use a sleep-wakeup approach
(instead of the busy-wait) by modifying
the starter code below. [10 points]



// Assume all necessary headers are included.
using namespace std;
// Add global variables as needed.

void printInOrder(const int id, const std::string data) {
// Of course, you should be typing the solution in
// exercise1.cpp. Testing it to ensure it works correctly.
// Of course, you can always upload your complete/partial
// solutions to the CODE plug-in for extra testing.
}
id 0: data 0
id 1: data 1
id 2: data 2

void printInOrder (int id, std::string data) {
static std::atomic turn;
// Wait for my turn
while (turn != id) {}
std::cout << "id " << id
<< ": " << data << std::endl;
turn++; // Next thread's turn
}
Expected output:
$ ./exercise12 q1 3
id 0: q1: 0
id 1: q1: 1
id 2: q1: 2
id 0: data 0
id 1: data 1
id 2: data 2

In a 300-level course full points are reserved for concise/precise solutions.
DUE Before: Before the end of your lab session
Page 3 of 5
Q2: Working with unordered_map
Estimated time to complete: 20 minutes
The following method printMTprocs is given a map of
thread-ids or TID (key in the map) and the corresponding
process-id or PID (value in the map). The method must
suitably process the given tidPid map and print all
processes with at least n (i.e., >= n) threads. For example,
given the adjacent map and n==2, the printMTprocs
method should generate the adjacent example output. Note:
This method does not require multithreading. [10 points]
// Assume all necessary headers are included.
using namespace std;
// Add global variables as needed
using IntIntMap = std::unordered_map

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!