Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Operating systems Homewokr problems

elpapiverdeelpapiverde Posts: 1Member
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 Experience the race condition and context switching.
a). Compile and run the code for 10-20 times (you will need shm437.h).

/* --- 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;

printf("Init balances 0:%d + 1:%d ==> %d!
pid = fork();

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;}
ptrBank->balance[1] = tmp2 - rint;

if (pid!=0) {
printf("Let's check the balances 0:%d + 1:%d ==> %d ?= 200

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


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
Average Wait
Time (mins)
Max Waiting
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)

this are the sample files:

// sample file to deal with UNIX semaphore
#ifndef sem437_h
#define sem437_h

// IF YOU ARE USING LINUX, uncomment the next line!!!
#define LINUX

extern int errno;

class Sem437 {
// constructor
Sem437(int seqnum, int initval) : semerr(false), semid(-1) {
key_t key; int status;
if ((key=ftok(getenv("HOME"), seqnum))==(key_t)-1) {
perror("ERROR: ftok");
if ((semid=semget(key,1,0600|IPC_CREAT))==-1) {
if (errno==EEXIST)
semid = semget(key,1,0);
if (semid !=-1)
#ifdef LINUX
status=semctl(semid, 0, SETVAL, initval);
status=semctl(semid, 0, SETVAL, &initval);
if (semid==-1 || status==-1) {
perror("ERROR: semctl"); semerr=true;
// modification members
void P() {
struct sembuf pbuf={0,-1,0};
if (semop(semid,&pbuf,1)==-1) {
perror("ERROR: semop"); semerr = true;
void V() {
struct sembuf vbuf={0,1,0};
if (semop(semid,&vbuf,1)==-1) {
perror("ERROR: semop"); semerr = true;
// examination members
bool HasErr() { return semerr; }
int GetVal() {
int v;
if ((v=semctl(semid, 0, GETVAL, NULL))==-1) {
perror("ERROR: semctl");
return v;
// data members
int semid;
bool semerr;

#endif /* sem437_h */

#ifndef random437_h
#define random437_h


const int max_int = 0x7FFFFFFF;

class Random {
Random(bool pseudo = true);
double random_real();
int random_integer(int, int);
int poisson(double);
int reseed();
int seed;
int multiplier;
int add_on;

int Random::reseed() {
seed = seed * multiplier + add_on;
return seed;

Random::Random(bool pseudo) {
if (pseudo)
seed = 1;
else {
seed = random();
multiplier = 2743;
add_on = 5923;

double Random::random_real() {
double max = max_int+1.0;
double temp = reseed();
if (temp < 0) temp = temp + max;
return temp/max;

int Random::random_integer(int low, int high) {
if (low > high)
return random_integer(high,low);
return ((int)((high-low+1)*random_real())) + low;

int Random::poisson(double mean) {
double limit = exp(-mean);
double product = random_real();
int count = 0;
while (product > limit) {
product *= random_real();
return count;

#endif /* random437_h */

// sample file to deal with UNIX shared memory
#ifndef shm437_h
#define shm437_h

class Shm437 {
// constructor
Shm437(int seqnum, unsigned int size) {
key_t key;
if ((key=ftok(getenv("HOME"), seqnum))==(key_t)-1) {
perror("ERROR: ftok");
else if ((shmid=shmget(key,size,0600|IPC_CREAT))==-1) {
perror("ERROR: shmget");
ssize = size;
void *ShmAlloc() {
void *ptr;
if ((ptr=shmat(shmid,(void*)NULL,0600))==(void*)-1) {
perror("ERROR: shmalloc");
return ptr;
// data members
int shmid, ssize;
#endif /* shm437_h */

Thanks for any help i would appreciate

Sign In or Register to comment.