Barlas Exercises Ch3
Barlas Exercises Ch3
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
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