hello i am an exchange student and english is not my first language and i am having problem with my programming assigments, this it what i have rigth now
1. (10+10+15+15=50%) Experience the race condition and context switching.
a). Compile and run the code for 10-20 times (you will need shm437.h).
/*===========================================================================*/
/* race.cc --- for play with */
/*===========================================================================*/
#include < unistd.h >
#include < stdio.h >
#include < stdlib.h >
#include < sys/wait.h >
#include < errno.h >
#include "shm437.h"
struct BANK {
int balance[2];
ECE437 Operating Systems file:///D:/eBook/UNMcourse/437/437f12/Projects/437proj3(2012f)/437p...
1 of 4 10/08/12 1:25 AM BANK() {balance[0]=balance[1]=0;}
};
int main(int argc, char **argv)
{ pid_t pid; int tmp1, tmp2, rint; double dummy;
Shm437 *pShmBank = new Shm437(1,sizeof(BANK));
BANK *ptrBank = (BANK*) pShmBank->ShmAlloc();
ptrBank->balance[0] = ptrBank->balance[1] = 100;
srandom(getpid());
for (int i=0; i < 100; i++) {
tmp1 = ptrBank->balance[0]; tmp2 = ptrBank->balance[1];
rint = (rand()%20)-10;
if ((tmp1+rint)>=0 && (tmp2-rint)>=0) {
ptrBank->balance[0] = tmp1 + rint;
for (int j=0; j < rint*100; j++) {dummy=2.345*8.765/1.234;}
usleep(1);
ptrBank->balance[1] = tmp2 - rint;
}
}
if (pid!=0) {
wait(NULL);
printf("Let's check the balances 0:%d + 1:%d ==> %d ?= 200\n",
ptrBank->balance[0],ptrBank->balance[1],
ptrBank->balance[0]+ptrBank->balance[1]);
}
}
Write a paragraph to explain what happened.
Change the code by arranging the variables and statements to make the result "better". Rerun the
code and explain what happened.
b). Use semaphores (either System V semaphore or POSIX semaphore, as your choice) to ensure the
reliability of of your bank. Show your code and the results. (a sample uasge of System V semaphore
sem437.h is provided.)
c). Use pthread mutexes and condition variables to implement your own semaphore class (in C++).
Although semaphore class implementation in C++ is preferred, we also accept your semaphre structure
in C as long as it works identically to semaphore class. Show your design and code. Note that you need
to use your own semaphore class (or structure) in part (d).
d). Redo part (b) with thread replacing process and your newly created semaphore in part (c) replacing
semaphores used in part (b)
2. (50%) You work as a systems engineer for Universal Studios, which is currently in the process of
designing a Jurassic Park ride at the theme park. You are in charge of designing the waiting area,
God-forsaken land of ropes, where people going around in circles. Here are specifications:
The ride consists of putting MAXPERCAR or less people on a Ford Explorer and sending through a
small scale version of Jurassic Park where the dinosaurs run loosely and attack tourists.
The process is semi-continuous, as an empty Explorer will arrive at the embarkation periodically. You
have been told by industrial engineers that it takes 7 seconds to load the people into the car and to send
them on their way and another 53 seconds to complete the ride.
But not every car is completely full, as the number of people waiting on line may be less than
MAXPERCAR.
The enqueuing rate is variable, as people get in line at various rates during the day. You can assume all
ECE437 Operating Systems file:///D:/eBook/UNMcourse/437/437f12/Projects/437proj3(2012f)/437p...
2 of 4 10/08/12 1:25 AMpeople arrival at beginning of every minute. The mean value of arrival rate varies as follows:
09:00:00--10:59:59, meanArrival = 25 persons per minute
11:00:00--13:59:59, meanArrival = 45 persons per minute
14:00:00--15:59:59, meanArrival = 35 persons per minute
16:00:00--18:59:59, meanArrival = 25 persons per minute
At the beginning of every minute, we can decide the number people who will arrive in this minute by calling Random.poisson(meanArrival), provided in file random437.h.
Assume there is a limit on the number of people kept in the waiting area, MAXWAITPEOPLE(800). If
more people come, they will be advised to come back later since the waiting line is too long.
You are going to design the waiting area where the lines will snake back and forth endlessly between
roped-off guideways, before spilling over into the street. Your task is to simulate the waiting lines on a
computer with multiple concurrent threads to observe the dynamics of the lines at different times during
the day.
There are total CARNUM Explorers, you need to create a new thread for each of them
You need a thread to take care of incoming people and decide that either accept them into the waiting
line or reject some or all of them away. It will write a status line into a output file:
XXX arrive YYY reject ZZZ waitline WWW at HH:MM:SS
where, XXX is the time step ranging from 0 to 599, YYY is the number of persons arrived, ZZZ is the
number of persons rejected, WWW is number of persons in the waiting line, HH:MM:SS is hours,
minutes, and seconds. At the end of the day, it will display:
The total number of people arrived
The total number of people taking the ride
The total number of people going away due to the long waiting line
The average waiting time per person (in minutes)
Determine the length of the line at its worst case, and the time of day at which that occurs.
All of your threads need to synchronize every minute.
You can use either C++ or C to implement your project.
You can choose one type of semaphores for implementation. Your implementation should not have any
thread doing busy waiting.
Your program will take two integers, CARNUM and MAXPERCAR, as its command-line arguments,
and run simulation from 9am to 7pm.
Run your simulation with MAXPERCAR=7,9, and CARNUM=4,6. (We may use different parameters
to test your code!)
From your output, fill out a table
Total
Arrival
Total
GoAway
Rejection
Ratio
Average Wait
Time (mins)
Max Waiting
Line
CARNUM=4,MAXPERCAR=7
CARNUM=4,MAXPERCAR=9
CARNUM=6,MAXPERCAR=7
CARNUM=6,MAXPERCAR=9
Provide three figures to illustrate:
number of persons arrived in every minutes. (one curve in one figure)
number of persons reject in every minutes. (four curves in one figure)
number of persons waiting in every minutes. (four curves in one figure)
class Random {
public:
Random(bool pseudo = true);
double random_real();
int random_integer(int, int);
int poisson(double);
private:
int reseed();
int seed;
int multiplier;
int add_on;
};