1 / 24

The Third Assignment

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.

bunny
Download Presentation

The Third Assignment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Third Assignment COSC 4330/6310 Spring 2011

  2. 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

  3. 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))

  4. Main program • Creates semaphores andshared memory segment • while (scanf(…) != 0) { sleep(interarrival_delay); fork a child();} • Wait for children termination

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. Solution • Adding mutexes • Using a shared memory segment to keep track of • current time • current bridge load

  11. 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);

  12. 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

  13. 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], …

  14. Shared memory segments • We must detach shared memory segments before deleting them • int shmid;shmdt((char *)pmem);shmctl(shmid, 0, IPC_RMID);

  15. 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

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

  17. 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"

  18. 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.*

  19. 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

  20. Keeping track of time • We are interested in process times not actual processor time • Must keep track of time ourselves

  21. 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

  22. Process creation time • Can be computed by the parent process • Inherited by the child • Used by the child to update global time (pmem[TIME])

  23. 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

  24. 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])

More Related