COSC 4330--Operating Systems
Programming Assignment No. 2
Due Date: November 18, 2004
In this assignment, you
will implement a program with Unix semaphores to
handle ticket reservations taken by different ticket-selling agents connected
to the "Fall-OS" ticketmaster's central
computer.
We will present each
agent by a separate process whose body will consist of all the ticketing made
by that agent. These agent processes
will execute in parallel. Two or more agents may be doing the same transactions
at around the same time. Obviously,
reservations/ticketing to the same seats must be performed in mutual exclusion. If an agent tries to reserve/ticket a seat
that has already been taken, then disallow this action and print a "seat
taken" message. If a transaction tries to operate on a non-existent seat, print
an error message.
To ensure these
concurrent operations yield correct results, you are to use Unix
semaphores to control access to seats, which are stored in shared variables. The
concert hall will have n rows labeled
A, B, C,
.and each row will have m seats numbered 1 to m.
To simulate (1) the
transmission delay between the ticketmaster's central
computer and an agent's computer terminal and (2) the processing of each
transaction, the first four lines in the body of each agent will specify the
required total execution time (in seconds) for each of the four operations
performed at that agent. In your
implementation, each specified time is the length of the critical section for
the corresponding transaction. You
should simulate them through system calls to sleep().
The input will be as follows:
n
// number of rows
m
// number of seats in each row
k
// number of agents
agent 1
reserve reserve_time (in seconds)
ticket ticket_time
(in seconds)
cancel cancel_time
(in seconds)
check_customer check_time
(in seconds)
:
valid transactions
:
end
:
more agents
:
agent k
reserve reserve_time (in seconds)
ticket ticket_time
(in seconds)
cancel cancel_time
(in seconds)
check_customer check_time
(in seconds)
:
valid transactions
:
end
Valid transactions are:
reserve seat(s)
name_of_customer
ticket seat(s) name_of_customer
cancel seat(s) name_of_customer
show all name_of_customer
// show seats reserved or ticketed
where seat(s) can be one or more seats as in:
A 2 // row A, seat 2
E 4 7 // row E, seats 4 to 7
Customer names will never contain spaces.
Agents don't need to reserve seats before ticketing
them. They can only cancel seats for the
customer who has reserved them but only until he or she has not purchased them.
Your program should print out
1.
A
summary of each transaction immediately after that transaction is processed.
2.
A report showing the final seat assignments
with customer names when the program terminates.
HINTS
(1) Don't forget the
following includes:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
(2) To get nbytes bytes of shared memory,
use:
int shmid;
long key;
int nbytes;
shmid = shmget(key, nbytes, 0666 | IPC_CREAT);
To
avoid conflicts, you might want to use your Student ID or a telephone number as
key. You can use the same unique key for
your semaphore array and your shared memory segment.
(3) To attach the shared
memory segment to
your address space, use:
char *pmem;
pmem = shmat(shmid, 0, 0);
To
test for error,
if (pmem ==
(char *)(-1)) ...
(4) To detach the shared
memory segment from your address space before destroying the segment, use:
shmdt(pmem)
(5) To destroy a shared
memory segment, use:
semctl(shmid, 0, IPC_RMID, 0);
(6) To create a single
semaphore, use:
int sid;
sid = semget(key, 1, 0666 |
IPC_CREAT);
// initial value is always zero
(7) To do a DOWN (wait)
operation, use:
int sem_down( int sid)
{
struct sembuf sb;
sb.sem_num
= 0;
sb.sem_op =
-1;
sb.sem_flg
= 0;
return semop(sid, &sb, 1);
}
(8) To do an UP (signal)
operation, replace -1 with 1 (and rename function).
(9) To destroy a semaphore,
use:
semctl(sid,0,
IPC_RMID, 0);
These
specifications were modified last on Sunday, October 31, 2004
Check the course web sites
for corrections and updates. The most recent changes will be
highlighted in the same color as the timestamp of the last update.