0% found this document useful (0 votes)
41 views4 pages

Barlas Exercises Ch3

This document contains 23 exercises related to shared memory programming and threads in C++. The exercises cover topics like timing diagrams for bank account balances, fork bombs, producer-consumer problems, train scheduling simulations, desktop publishing simulations, bakery simulations, priority withdrawals from bank accounts, printer resource management, alphabet printing with threading rules, cigarette smoker problems, movie theater simulations, password cracking, prime number checking, 2D point searches, and bucket sorting. Students are asked to write programs to demonstrate their understanding of threading and synchronization concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views4 pages

Barlas Exercises Ch3

This document contains 23 exercises related to shared memory programming and threads in C++. The exercises cover topics like timing diagrams for bank account balances, fork bombs, producer-consumer problems, train scheduling simulations, desktop publishing simulations, bakery simulations, priority withdrawals from bank accounts, printer resource management, alphabet printing with threading rules, cigarette smoker problems, movie theater simulations, password cracking, prime number checking, 2D point searches, and bucket sorting. Students are asked to write programs to demonstrate their understanding of threading and synchronization concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

160 CHAPTER 3 Shared-memory programming: threads

EXERCISES
1. Enumerate and create the other timing diagrams that show the alternatives of
Figure 3.4 when it comes to the final balance of the bank account.
2. Research the term “fork bomb” and write a program that performs as such.
3. Modify the producer-consumer example shown in Listing 3.11 so that the
threads terminate after the number 100 is generated.
4. Suggest a modification to the program of Listing 3.12 so that the IntegrCalc
threads can use any function that returns a double and takes a double as a
parameter.
5. In a remote region of Siberia there are single tracks joining railroad stations.
Obviously only one train can use a piece of track between two stations. The
other trains can wait at the stations before they do their crossings. The
following graph indicates the track and station layout:
A B D F

C E G

Write a Qt program that simulates the journey of three trains with the following
schedules:
• A→B→E→C
• D→B→E→G
• C→E→B→D→F
As each trains arrives at a station, display a relative message. You can assume
that a station can hold any number of trains waiting.
6. Modify the program of the previous exercise so that each station can hold only
2 trains. Can this lead to deadlocks?
If you have not done so already, make sure that your program uses only one
thread class.
7. A desktop publishing application such as PageMaker has two threads running:
one for running the GUI and one for doing background work. Simulate this
application in Qt. Your implementation should have the thread corresponding
to the GUI send requests to the other thread to run tasks on its behalf. The tasks
should be (obviously just printing a message is enough for the simulation):
• Printing
• Mail merging
• PDF generation
After performing each requested task, the second thread should wait for a new
request to be sent to it. Make sure that the first thread does not have to wait for
the second thread to finish before making new requests.
8. A popular bakery has a baker that cooks a loaf of bread at a time and deposits it
on a counter. Incoming customers pick up a loaf from the counter and exit the
bakery. The counter can hold 20 loaves. If the counter is full, the baker stops
Exercises 161

baking bread. If it is empty, a customer waits. Use semaphores to solve the


coordination problem between the baker and the customers.
9. Because of customer demand, the bakery owner is considering the following
enhancements to his shop:
a. Increase the capacity of the counter to 1000
b. Hire three more bakers
Modify the solution of the previous exercise to accommodate these changes.
Which is the easiest option to implement?
10. A bank account class is defined as follows:
c l a s s BankAccount {
protected :
d o u b l e balance ;
string holderName ;
public :
d o u b l e getBalance ( ) ;
v o i d deposit ( d o u b l e ) ;
v o i d withdraw ( d o u b l e , i n t ) ; / / t h e h i g h e s t t h e s e c o n d ←!
argument , t h e h i g h e r t h e p r i o r i t y of t h e r e q u e s t
};

Write the implementation of the three methods given above so that withdraw
operations are prioritized: If there are not enough funds in the account for all,
the withdrawals must be done in order of priority, regardless of whether there
are some that can be performed with the available funds. You can assume that
the priority level in the withdraw method is by default equal to 0, and that it is
upper bounded by a fixed constant MAXPRIORITY.
11. The IT department of a big corporation is equipped with five high-speed
printers that are used by a multitude of threads. The threads are part of the
same accounting process. Each of the threads is supposed to perform the
following (pseudocode) sequence in order to print any material:
...
printerID = get_available_printer ( ) ;
/ / p r in t to printerID p ri n t e r
releasePrinter ( printerID ) ;
...

Write an appropriate implementation for the two functions listed above using
semaphores. You can assume that the available printer IDs are stored in a
shared buffer.
12. Create three threads, each printing out the letters A, B, and C. The printing
must adhere to these rules:
• The total number of Bs and Cs that have been output at any point in the
output string cannot exceed the total number of As that have been output at
that point.
• After a C has been output, another C cannot be output until one or more Bs
have been output.
Use semaphores to solve the problem.
162 CHAPTER 3 Shared-memory programming: threads

13. Modify the previous exercise so that the printing is governed by this set of
rules:
• One C must be output after two As and three Bs are output.
• Although there is no restriction on the order of printing A and B, the
corresponding threads must wait for a C to be printed when the previous
condition is met.
Use a monitor to solve the problem.
14. Address the termination problem in the previous exercise. How can the three
threads terminate after, for example, a fixed number of As have been output?
Or when a fixed total number of characters have been output?
15. Create four threads, each printing out the letters A, B, C, and D. The printing
must adhere to these rules:
• The total number of As and Bs that have been output at any point in the
output string cannot exceed the total number of Cs and Ds that have been
output at that point.
• The total number of As that have been output at any point in the output
string cannot exceed twice the number of Bs that have been output at that
point.
• After a C has been output, another C cannot be output until one or more D
have been output.
Solve the problem using (a) semaphores and (b) a monitor.
16. Use semaphores to solve the typical cigarette smokers’ problem, where the
agent directly signals the smoker missing the two ingredients placed on the
table.
17. Solve the cigarette smokers’ problem as described in Section 3.6.2 using
semaphores.
18. Model the movie-going process at a multiplex cinema using a monitor. Assume
the following conditions:
• There are three different movies playing at the same time in three theaters.
The capacities of each theater are 4, 5, and 7, respectively.
• One hundred customers are waiting to see a randomly chosen movie.
• A cashier issues the tickets.
• If a theater is full, a movie begins to play.
• A customer cannot enter a theater while a movie is playing or while the
previous viewers are exiting the theater.
• A movie will play for the last customers, even if the corresponding theater
is not full.
19. Write a multithreaded password cracker based on the producer-consumer
paradigm. The producer should generate plaintext passwords according to a set
of rules, and the consumers should be hashing each password and checking
whether it matches a target signature. All the threads should terminate upon the
discovery of a matching password. You can use the MD5 cryptographic hash
function for this exercise.
Exercises 163

20. Write a multithreaded program for finding the prime numbers in a


user-supplied range of numbers. Compare the following design approaches:
a. Split the range in equal pieces and assign each one to a thread.
b. Have a shared QAtomicInt variable that holds the next number to be
checked. Threads should read and increment this number before testing it.
c. Have a shared “monitor” object that returns, upon request, a range of
numbers to be tested. This can be considered a generalization of the
previous design.
Which of the designs is more efficient? Explain your findings.
21. Use the QtConcurrent functionality to implement a prime number checker.
Compare it in terms of speed, efficiency, and programming effort to your
QThread-based attempt of the previous exercise.
22. Create a big array of randomly generated 2D coordinates (x, y). Each of the
coordinates should be a number in the range [-1000, 1000]. Use appropriate
QtConcurrent functions to find the points that are in a ring of distances
between 100 and 200 from the point of origin. Compare the performance of
your solution against a sequential implementation.
23. Use the QtConcurrent functionality to implement a parallel bucketsort. Does
the number of buckets play a significant role in your implementation’s
performance?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy