/* simphil.h dining philosophers using simultaneous ops. Author : Neil Dunstan, UNE, Australia */ #include #include #include int forks; /* semsettran will return the semaphore set id (system wide) of the key you give with sems in the set. If no semaphore set has been established for this key, one is created */ int semsettran( int key, int sems ) { /* translate/ create semaphore key to id */ int sid; if( (sid = semget( (key_t)key, sems, 0666 | IPC_CREAT )) == -1 ) printf( "semget" ); return( sid ); } /* SimOps the semaphore signal operation. sid must be the system wide semaphore number returned by semtran above */ void SimOps( int sid, int snum1, int snum2, int op ) {/* apply ops on sems simultaneously */ struct sembuf sb[2]; sb[0].sem_num = snum1; sb[1].sem_num = snum2; sb[0].sem_op = sb[1].sem_op = op; sb[0].sem_flg = sb[1].sem_flg = 0; if( semop( sid, sb, 2 ) == -1 ) printf( "semop" ); } /* rm_sem remove a semaphore from the system. sid must be the system wide semaphore returned from semtran */ void rm_sem( int sid ){ /* remove semaphore */ (void)semctl( sid, 0, IPC_RMID, 0 ); } void set_the_table() { forks = semsettran( IPC_PRIVATE, 5 ); }