Tasks – semaphores

advertisement
Tasks – semaphores
Task 1 (Precedence graph).
A precedence graph is a directed, acyclic graph. Nodes represent tasks and arcs indicate the order in which the
tasks are to be accomplished. In particular, a task can execute as soon as all its predecessors have been
completed. Assume that the tasks are processes and that the body of each process has the following outline:
begin
wait for predecessors, if any;
the body;
signal successors, if any;
end
Using semaphores, show how to synchronize five processes whose permissible execution order is specified by the
following precedence graph:
Minimize the number of semaphores that you use.
Task 2 (Atomic broadcast with the buffer of size 1).
Assume one producer process and n consumer processes share a buffer. The producer deposits messages into the
buffer, consumer fetch them. Every message deposited by the producer has to be fetched by all n consumers
before the producer can deposit another message into the buffer. Develop solution using semaphores for
synchronization.
Assume that the buffer can store one message at a time.
Task 3 (Atomic broadcast with the buffer of size >= 1).
Assume one producer process and N consumer processes share a buffer. The producer deposits messages into
the buffer, consumer fetch them. Every message deposited by the producer has to be fetched by all N consumers
before the producer can deposit another message into the buffer. Develop solution using semaphores for
synchronization.
Assume that the buffer can store B messages at a time. Every message has to be received by all n consumers
before it is deleted from the buffer. Furthermore, each consumer is to receive the messages in the order they
were deposited. However, different consumers can receive messages at different times. For example, one
consumer can receive up to B messages more than another if the second consumer is slow.
Task 4 (Cigarette smokers problem).
Suppose there are three smoker processes and one agent process. Each smoker continuously makes a cigarette
and smokes it. Making a cigarette requires three ingredients: tobacco, paper and a match. One smoker process
has tobacco, the second paper, and the third matches. Each has infinite supply of these ingredients. The agent
places random two ingredients on the table. The smoker who has the third ingredient picks up the other two,
makes a cigarette, then smokes it. The agent waits for the smoker to finish. The cycle then repeats. Develop a
solution to this problem using semaphores for synchronization.
Task 5 (The unisex bathroom with unlimited capacity).
Suppose there is one bathroom in your department. It can be used by both men and women, but not at the same
time. Develop solution to this problem using semaphores for synchronization. Allow any number of men or
women to be in the bathroom at the same time.
Task 6 (The unisex bathroom with limited capacity).
Suppose there is one bathroom in your department. It can be used by both men and women, but not at the same
time. Develop solution to this problem using semaphores for synchronization. Ensure that at most b people are in
the bathroom at the same time.
Task 7 (The one-lane bridge).
Cars coming from the north and the south arrive at one-lane bridge. Cars heading in the same direction can cross
the bridge at the same time, but cars heading in opposite directions cannot. Develop a solution to this problem
using semaphores for synchronization.
Task 8 (The hungry birds).
Given are n baby birds and one parent bird. The baby birds eat out of a common dish that initially contains F
portions of food. Each baby repeatedly eats one portion of food at a time, sleeps for a while, and then comes back
to eat. When the dish becomes empty, the baby birds who empties the dish awakens the parent bird. The parent
refills the dish with F portions, then waits for the dish to become empty again. This pattern repeats forever.
Represent the birds as processes and develop code that simulates their actions. Use semaphores for
synchronization.
Task 9 (The bear and the honeybees).
Given are n honeybees and a hungry bear. They share a pot of honey. The pot is initially empty; its capacity is H
portions of honey. The bear sleeps until the pot is full, then eats all the honey and goes back to sleep. Each bee
repeatedly gathers one portion of honey and puts it in the pot; the bee who fills the pot awakens the bear.
Represent the bear and honeybees as processes and develop code that simulates their actions. Use semaphores
for synchronization.
Task 10 (The savings account problem).
A savings account is shared by several people (processes). Each person may deposit or withdraw funds from the
account. The current balance in the account is the sum of all deposits to date minus the sum of all withdrawals to
date. The balance must never become negative. A deposit never has to delay (except for mutual exclusion), but a
withdrawal has to wait until there are sufficient funds. Resolve described problem and use semaphores for
synchronization.
Task 11 (The post office).
The post office has three cash desks that perform the same range of benefits. Each of the cash desks at a time can
operate only one client. All cash desks have one common queue of waiting customers. The first customer in the
queue is served by any free cash desk. You should take into account that some customers are served out of turn.
Resolve the problem using semaphores.
Task 12 (Producer and consumer).
Suppose there are single consumer and N producers numbered from 0 to N-1. Processes share the cyclic buffer of
size N. The i-th producer puts own message into the buffer at i-th position. The consumer takes messages
according to round-robin algorithm (when it try to take message from i-th position and it is empty then it try to
take from the next position). The task is to write processes of producers and consumer.
Task 13 (Cyclic buffers).
Suppose there exists two cyclic buffers B1 and B2 and N processes P1, ..., PN. Processes generate random integer
values and put them into B1. The S process takes a pair of values from B1, sums them and puts the result into B2.
The K process waits until B2 is full and takes all values from B2. The task is to write processes P1, …, PN, S, K and
synchronize them using semaphores.
Tasks – monitors
Task 1 (The savings account problem).
A savings account is shared by several people (processes). Each person may deposit or withdraw funds from the
account. The current balance in the account is the sum of all deposits to date minus the sum of all withdrawals to
date. The balance must never become negative. A deposit never has to delay (except for mutual exclusion), but a
withdrawal has to wait until there are sufficient funds.
Develop a monitor to solve this problem. The monitor should have two procedures/functions: deposit(amount)
and withdraw(amount). Assume the arguments of both subroutines are positive.
Task 2 (Atomic broadcast with the buffer of size >= 1).
Assume one producer process and N consumer processes share a buffer. The producer deposits messages into
the buffer, consumer fetch them. Every message deposited by the producer has to be fetched by all N consumers
before the producer can deposit another message into the buffer. Develop solution using semaphores for
synchronization.
Assume that the buffer can store B messages at a time. Every message has to be received by all n consumers
before it is deleted from the buffer. Furthermore, each consumer is to receive the messages in the order they
were deposited. However, different consumers can receive messages at different times. For example, one
consumer can receive up to B messages more than another if the second consumer is slow.
Task 3 (Exchange of values).
Develop a monitor that allows pairs of processes to exchange values. The monitor has one operation:
exchange(int &value). After two processes have called exchange, the monitor swaps the values of the arguments
and returns them to the processes. The monitor should be reusable in the sense that it exchanges the parameters
of the first pair of callers, then the second pair of callers, and so on.
Task 4 (Search/insert/delete).
Three kinds of processes share access to a singly linked list: searchers, inserters and deleters. Searchers merely
examine the list; hence they can execute concurrently with each other. Inserters add new items to the end of the
list; insertions must be mutually exclusive to preclude inserting two items at about the same time. However,
deleters remove items from anywhere in the list. At most one deleter process can access the list at a time, and
deletion must also be mutually exclusive with searches and insertions.
Develop a monitor to implement this kind of synchronization.
Task 5 (Master and workers).
Suppose there are singe master process and N processes of worker. Master process has an array of M integer
values (M > N, M mod N = 0). Workers determine the smallest value in an array stored in master. Master prints on
the screen determined smallest value.
Develop a monitor to implement this kind of synchronization and communication.
Task 6 (The smallest value).
Suppose there are N processes and each process has own ID from the range 1..N. Each process generates random
integer value and processes determine ID of the process that generated the smallest value. You can assume that
the processes generate different values. Determined ID is printed on the screen by each process.
Develop a monitor to implement this kind of synchronization and communication.
Task 7 (The one-lane bridge).
Cars coming from the north and the south arrive at one-lane bridge. Cars heading in the same direction can cross
the bridge at the same time, but cars heading in opposite directions cannot.
Develop a monitor to implement this kind of synchronization.
Task 8 (A single-track railway).
A single-track railway is where trains in both directions share the same track. There exists a passing loop and it is a
place of double tracks where trains travelling in opposite directions can pass each other. The task is to
synchronize travelling trains using the monitor.
Task 9 (Cyclic buffers).
Suppose there exists two cyclic buffers B1 and B2 and N processes P1, ..., PN. Processes generates random integer
values and put them into B1. The S process takes a pair of values from B1, sums them and puts the result into B2.
The K process waits until B2 is full and takes all values from B2. The task is to write processes P1, …, PN, S, K and
synchronize them using semaphores.
Develop a monitor to implement this kind of synchronization.
Task 10 (Consumer and producers).
Suppose there are single consumer and N producers numbered from 0 to N-1. Processes share the cyclic buffer of
size N. The i-th producer puts own message into the buffer at i-th position. The consumer takes messages
according to round-robin algorithm (when it try to take message from i-th position and it is empty then it try to
take from the next position).
Develop a monitor to implement this kind of synchronization and communication.
Task 11 (The post office).
The post office has three cash desks that perform the same range of benefits. Each of the cash desks at a time can
operate only one client. All cash desks have one common queue of waiting customers. The first customer in the
queue is served by any free cash desk. You should take into account that some customers are served out of turn.
Develop a monitor to implement this kind of synchronization and communication.
Tasks – message passing
Task 1 (Precedence graph).
A precedence graph is a directed, acyclic graph. Nodes represent tasks and arcs indicate the order in which the
tasks are to be accomplished. In particular, a task can execute as soon as all its predecessors have been
completed. Assume that the tasks are processes and that the body of each process has the following outline:
begin
wait for predecessors, if any;
the body;
signal successors, if any;
end
Using message passing, show how to synchronize five processes whose permissible execution order is specified by
the following precedence graph:
Task 2 (Min-max exchange).
Given are two processes: A and B. A has a set of integers S, and B has a set of integers T. The two processes are to
exchange values one at a time until all elements of S are less than all elements of T. All values are distinct.
Task 3 (The savings account problem).
Using synchronous message passing, develop a server to solve the savings account problem and show the client
interface to the server. Clients make two kinds of request: one to deposit amount of USD and one to withdraw
amount of USD. Assume that amount is positive. The balance must never become negative.
Task 4 (Printers).
Suppose a computing centre has two printers, A and B, that are similar but not identical. Three kinds of client
processes use the printers: those that must use A, those that must use B, and those that can use either A or B.
Using synchronous message passing, develop code that each kind of client executes to request and release a
printer, and develop server process to allocate the printers. Your solution should be fair, assuming that a client
using a printer eventually releases it.
Task 5 (Smallest common value).
Given are three processes: P0; P1 and P2. Each has a local array of N integers; all three arrays are sorted in nondecreasing order. At least one value is in all three arrays. Develop a program in which the three processes interact
until each one has determined the smallest common value. Use synchronous message passing. Messages should
contain only one value at a time.
Download