Download presentation
Presentation is loading. Please wait.
Published byNorman Henry Modified over 9 years ago
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 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
3
General organization Parent process: Parent process: –reads input file –forks one child per arriving vehicle Child processes Child processes –wait to get on bridge –leave the bridge –terminate ( _exit(0) )
4
Main program Creates semaphores and shared memory segment Creates semaphores and shared memory segment while (scanf(…) != 0) { sleep(interarrival_delay); fork a child(); } while (scanf(…) != 0) { sleep(interarrival_delay); fork a child(); } Wait for children termination Wait for children termination
5
Child processes Share Share –a "bridge" semaphore whose initial value is the maximum bridge load – some mutexes –a shared memory segment holding current time current time current bridge load current bridge load
6
Oversimplified pseudocode for( i = 0; i < weight; i++) { sem _wait(bridge); } // for for( i = 0; i < weight; i++) { sem _wait(bridge); } // for sleep(crossing_time); sleep(crossing_time); for( i = 0; i < weight; i++) { sem _post(bridge); } // for for( i = 0; i < weight; i++) { sem _post(bridge); } // for
7
What is missing Child does not keep track of Child does not keep track of –current bridge load –current time Vehicles do not enter or leave the bridge in an atomic fashion 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 Two trucks (weight > one ton) arrive at the bridge at the same time Should enter the bridge one after the other 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) arrives at the bridge followed by several cars (weights < one ton) Cars should not enter the bridge until truck has completely left the bridge Cars should not enter the bridge until truck has completely left the bridge
10
Solution Adding mutexes Adding mutexes Using a shared memory segment to keep track of 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 Allocate four bytes for each int or float variable int shmid; // segment id key_t shmkey; //segment key shmid = shmget(shmkey, nbytes, 0600 | IPC_CREAT); int shmid; // segment id key_t shmkey; //segment key shmid = shmget(shmkey, nbytes, 0600 | IPC_CREAT);
12
Shared memory segments Must attach segment before using it Must attach segment before using it int shmid; // segment id int *pmem; // pointer pmem = (int *) shmat(shmid, 0, 0); int shmid; // segment id int *pmem; // pointer pmem = (int *) shmat(shmid, 0, 0); Type of pmem pointer and casting in shmat() defines type of data we will store in the segment Type of pmem pointer and casting in shmat() 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 pmem pointer Once a segment is attached we can access its contents through the pmem pointer – pmem[0], pmem[1], … I define constants that let me write pmem[TIME], pmem[LOAD], … I define constants that let me write pmem[TIME], pmem[LOAD], …
14
Shared memory segments We must detach shared memory segments before deleting them We must detach shared memory segments before deleting them int shmid; shmdt((char *)pmem); shmctl(shmid, 0, IPC_RMID); int shmid; shmdt((char *)pmem); shmctl(shmid, 0, IPC_RMID);
15
POSIX semaphore tips Sole non-trivial call is sem_open() Sole non-trivial call is sem_open() –Works like open() with O_CREAT option Accesses named semaphore Accesses named semaphore Creates a new one if named semaphore did not exist 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); 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 Semaphore names must start with a slash Semaphores appear in the file system in subdirectory of /dev/shm Semaphores appear in the file system in subdirectory of /dev/shm –Names prefixed with " sem. " Can be removed just like regular files using " rm " 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 sem_open(…) does not change the value of an existing semaphore –initial_value is not used if the semaphore already exists Must be sure that all your semaphores have been deleted before restarting your program 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: Can test at any time the value of any opened semaphore: int semid, value; sem_getvalue(semid,&value); Non-standard feature of POSIX semaphores Non-standard feature of POSIX semaphores
20
Keeping track of time We are interested in process times not actual processor 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: We need to keep track of times: –When the process is created Vehicle arrives Vehicle arrives –When the process clears the bridge semaphores Vehicle goes on the bridge Vehicle goes on the bridge –When the process terminates Vehicle leaves the bridge Vehicle leaves the bridge
22
Process creation time Can be computed by the parent process 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 Not directly known by the process Could be 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 Use last value of global time
24
Process termination time Easily computed by the child process 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])
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.