the third assignment cosc 4330/6310 spring 2011. implementing delays to be able to test the...

24
The Third Assignment The Third Assignment COSC COSC 4330/6310 4330/6310 Spring 2011 Spring 2011

Upload: norman-henry

Post on 31-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

The Third The Third AssignmentAssignment

COSC COSC 4330/63104330/6310

Spring 2011Spring 2011

Implementing delaysImplementing delays

To be able to test the semaphores, we To be able to test the semaphores, we must run the program must run the program in real timein real time– All delays will be implemented All delays will be implemented

through through sleep statementssleep statements

int m;int m;sleep(m);sleep(m);will delay the calling process bywill delay the calling process bym secondsm seconds

General organizationGeneral organization

Parent process:Parent process:– reads input filereads input file– forks one child per arriving vehicleforks one child per arriving vehicle

Child processesChild processes– wait to get on bridgewait to get on bridge– leave the bridgeleave the bridge

– terminate ( terminate ( _exit(0)_exit(0)))

Main programMain program

Creates semaphores andCreates semaphores andshared memory segmentshared memory segment

while (scanf(…) != 0) {while (scanf(…) != 0) { sleep(interarrival_delay); sleep(interarrival_delay); fork a child(); fork a child();}}

Wait for children terminationWait for children termination

Child processes Child processes

ShareShare– a "bridge" semaphore whose initial a "bridge" semaphore whose initial

value is the maximum bridge loadvalue is the maximum bridge load– some mutexessome mutexes– a shared memory segment holdinga shared memory segment holding

current timecurrent timecurrent bridge loadcurrent bridge load

Oversimplified Oversimplified pseudocodepseudocode for( i = 0; i < weight; i++) {for( i = 0; i < weight; i++) {

sem _wait(bridge); sem _wait(bridge); } // for } // for

sleep(crossing_time);sleep(crossing_time);

for( i = 0; i < weight; i++) {for( i = 0; i < weight; i++) { sem _post(bridge); sem _post(bridge); } // for } // for

What is missingWhat is missing

Child does not keep track of Child does not keep track of – current current bridge loadbridge load– current current timetime

Vehicles do not enter or leave the Vehicles do not enter or leave the bridge in anbridge in an atomic fashion atomic fashion

ExamplesExamples

Two trucks (weight > one ton) arrive Two trucks (weight > one ton) arrive at the bridge at the same timeat the bridge at the same time

Should enter the bridge one after the Should enter the bridge one after the otherother– Avoid potential deadlockAvoid potential deadlock

ExamplesExamples

One truck (weight > one ton) arrives One truck (weight > one ton) arrives at the bridge followed by several at the bridge followed by several cars (weights < one ton)cars (weights < one ton)

Cars should not enter the bridge until Cars should not enter the bridge until truck has truck has completely leftcompletely left the the bridgebridge

SolutionSolution

Adding Adding mutexesmutexes Using a shared memory segment Using a shared memory segment

to keep track ofto keep track of– current current timetime– current current bridge loadbridge load

Shared memory Shared memory segmentssegments Allocate four bytes for each int or Allocate four bytes for each int or

float variablefloat variable int shmid; // segment idint shmid; // segment idkey_t shmkey; //segment keykey_t shmkey; //segment keyshmid = shmget(shmkey,shmid = shmget(shmkey, nbytes, nbytes, 0600 | IPC_CREAT) 0600 | IPC_CREAT);;

Shared memory Shared memory segmentssegments Must attach segment before using itMust attach segment before using it int shmid; // segment idint shmid; // segment idint *pmem; // pointerint *pmem; // pointerpmem = (int *)pmem = (int *) shmat(shmid, 0, 0); shmat(shmid, 0, 0);

Type of Type of pmempmem pointer and casting inpointer and casting in shmat()shmat() defines type of data we will defines type of data we will store in the segmentstore in the segment

Shared memory Shared memory segmentssegments Once a segment is attached we can Once a segment is attached we can

access its contents through the access its contents through the pmempmem pointerpointer– pmem[0]pmem[0],, pmem[1]pmem[1], …, …

I define constants that let me write I define constants that let me write pmem[TIME]pmem[TIME],, pmem[LOAD]pmem[LOAD], …, …

Shared memory Shared memory segmentssegments We must detach shared memory We must detach shared memory

segments before deleting themsegments before deleting them

int shmid;int shmid;shmdt((char *)pmem);shmdt((char *)pmem);shmctl(shmid, 0, IPC_RMID); shmctl(shmid, 0, IPC_RMID);

POSIX semaphore tipsPOSIX semaphore tips

Sole non-trivial call is Sole non-trivial call is sem_open()sem_open()– Works like Works like open()open() with with O_CREATO_CREAT option optionAccesses named semaphoreAccesses named semaphoreCreates a new one if named Creates a new one if named semaphore did not existsemaphore did not exist

Sem_open syntaxSem_open syntax

sem_t *mysem;sem_t *mysem;char name[] = "/Sem Name";char name[] = "/Sem Name";unsigned int initial_value;unsigned int initial_value;mysem = sem_open(name,mysem = sem_open(name, O_CREAT, 0600, O_CREAT, 0600, initial_value); initial_value);

Semaphore namesSemaphore names

Semaphore names must start with Semaphore names must start with a slasha slash

Semaphores appear in the file Semaphores appear in the file system in subdirectory of system in subdirectory of /dev/shm/dev/shm– Names prefixed with "Names prefixed with "sem.sem.""

Can be removed just like regular Can be removed just like regular files using "files using "rmrm""

A source of troublesA source of troubles

sem_open(…)sem_open(…) does not change the does not change the value of an existing semaphorevalue of an existing semaphore–initial_valueinitial_value is not used if the is not used if the

semaphore already exists semaphore already exists Must be sure that all your Must be sure that all your

semaphores have been deleted semaphores have been deleted before restarting your programbefore restarting your program

– ls /dev/shm/sem.*ls /dev/shm/sem.*

A useful system callA useful system call

Can test at any time the value of Can test at any time the value of any opened semaphore:any opened semaphore:

int semid, value;int semid, value;sem_getvalue(semid,&value);sem_getvalue(semid,&value);

Non-standard feature of POSIX Non-standard feature of POSIX semaphoressemaphores

Keeping track of timeKeeping track of time

We are interested in We are interested in process process times times not actual processor timenot actual processor time– Must keep track of time Must keep track of time

ourselvesourselves

Tracking child Tracking child processesprocesses We need to keep track of times:We need to keep track of times:

– When the process is createdWhen the process is createdVehicle arrivesVehicle arrives

– When the process clears the When the process clears the bridge semaphoresbridge semaphoresVehicle goes on the bridgeVehicle goes on the bridge

– When the process terminatesWhen the process terminatesVehicle leaves the bridgeVehicle leaves the bridge

Process creation timeProcess creation time

Can be computed by the parent Can be computed by the parent processprocess– Inherited by the childInherited by the child– Used by the child to update Used by the child to update

global time (pmem[TIME])global time (pmem[TIME])

Time at which a process Time at which a process clears the bridge clears the bridge semaphoresemaphore Not directly known by the processNot directly known by the process Could beCould be

– Process creation time if there Process creation time if there was no wait for the semaphorewas no wait for the semaphore

– Termination time of process Termination time of process that released the semaphorethat released the semaphore

Use last value of global timeUse last value of global time

Process termination Process termination timetime Easily computed by the child Easily computed by the child

processprocess– Time at which the process Time at which the process

cleared the bridge semaphore cleared the bridge semaphore plus the crossing timeplus the crossing time

– Used by the child to update Used by the child to update global time (pmem[TIME])global time (pmem[TIME])