240 likes | 416 Views
The Third Assignment. COSC 4330/6310 Spring 2011. Implementing delays. To be able to test the semaphores, we must run the program in real time All delays will be implemented through sleep statements int m; sleep(m); will delay the calling process by m seconds. General organization.
E N D
The Third Assignment COSC 4330/6310 Spring 2011
Implementing delays • To be able to test the semaphores, we must run the program in real time • All delays will be implemented through sleep statements int m;sleep(m); will delay the calling process bym seconds
General organization • Parent process: • reads input file • forks one child per arriving vehicle • Child processes • wait to get on bridge • leave the bridge • terminate ( _exit(0))
Main program • Creates semaphores andshared memory segment • while (scanf(…) != 0) { sleep(interarrival_delay); fork a child();} • Wait for children termination
Child processes • Share • a "bridge" semaphore whose initial value is the maximum bridge load • some mutexes • a shared memory segment holding • current time • current bridge load
Oversimplified pseudocode • for( i = 0; i < weight; i++) { sem _wait(bridge); } // for • sleep(crossing_time); • for( i = 0; i < weight; i++) { sem _post(bridge); } // for
What is missing • Child does not keep track of • current bridge load • current time • Vehicles do not enter or leave the bridge in an atomic fashion
Examples • Two trucks (weight > one ton) arrive at the bridge at the same time • Should enter the bridge one after the other • Avoid potential deadlock
Examples • One truck (weight > one ton) arrives at the bridge followed by several cars (weights < one ton) • Cars should not enter the bridge until truck has completely left the bridge
Solution • Adding mutexes • Using a shared memory segment to keep track of • current time • current bridge load
Shared memory segments • Allocate four bytes for each int or float variable • int shmid; // segment idkey_t shmkey; //segment keyshmid = shmget(shmkey, nbytes, 0600 | IPC_CREAT);
Shared memory segments • Must attach segment before using it • int shmid; // segment idint *pmem; // pointerpmem = (int *) shmat(shmid, 0, 0); • Type of pmempointer and casting inshmat() defines type of data we will store in the segment
Shared memory segments • Once a segment is attached we can access its contents through the pmempointer • pmem[0],pmem[1], … • I define constants that let me write pmem[TIME],pmem[LOAD], …
Shared memory segments • We must detach shared memory segments before deleting them • int shmid;shmdt((char *)pmem);shmctl(shmid, 0, IPC_RMID);
POSIX semaphore tips • Sole non-trivial call is sem_open() • Works like open() with O_CREAT option • Accesses named semaphore • Creates a new one if named semaphore did not exist
Sem_open syntax • sem_t *mysem;char name[] = "/Sem Name";unsigned int initial_value;mysem = sem_open(name, O_CREAT, 0600, initial_value);
Semaphore names • Semaphore names must start with a slash • Semaphores appear in the file system in subdirectory of /dev/shm • Names prefixed with "sem." • Can be removed just like regular files using "rm"
A source of troubles • sem_open(…) does not change the value of an existing semaphore • initial_valueis not used if the semaphore already exists • Must be sure that all your semaphores have been deleted before restarting your program • ls /dev/shm/sem.*
A useful system call • Can test at any time the value of any opened semaphore: int semid, value;sem_getvalue(semid,&value); • Non-standard feature of POSIX semaphores
Keeping track of time • We are interested in process times not actual processor time • Must keep track of time ourselves
Tracking child processes • We need to keep track of times: • When the process is created • Vehicle arrives • When the process clears the bridge semaphores • Vehicle goes on the bridge • When the process terminates • Vehicle leaves the bridge
Process creation time • Can be computed by the parent process • Inherited by the child • Used by the child to update global time (pmem[TIME])
Time at which a process clears the bridge semaphore • Not directly known by the process • Could be • Process creation time if there was no wait for the semaphore • Termination time of process that released the semaphore • Use last value of global time
Process termination time • Easily computed by the child process • Time at which the process cleared the bridge semaphore plus the crossing time • Used by the child to update global time (pmem[TIME])