/** * @param pk a pointer to an array of c-strings */ void d_park(char pk[25][80]){ SEM_WAIT(parkMutex); SEM_WAIT(sysMutex); FILE *f; if(printed++ < 100) {f = fopen("pk.txt", "a+"); SWAP;} else{ f = fopen("pk.txt", "w"); SWAP; printed = 0; } int i; SWAP; //clear screen for(i=0; i<30; i++){ fprintf(f, "\n"); SWAP; } //draw park fprintf(f, "\n"); SWAP; for (i=0; i<25; i++) fprintf(f, "\n%s", pk[i]); SWAP; fprintf(f, "\n"); SWAP; fclose(f); SWAP; SEM_SIGNAL(sysMutex); SEM_SIGNAL(parkMutex); }
// *********************************************************************** // *********************************************************************** // group parent - create children // argv[0] = group base name // argv[1] = parent # // argv[2] = # of children // int parentTask(int argc, char* argv[]) // group 1 { int i, num_children; char buffer[32]; int parent = atoi(argv[1]); num_children = atoi(argv[2]); for (i = 'a'; i < 'a' + num_children; i++) { sprintf(buffer, "%s%d%c", "child_", atoi(argv[1]), i); createTask(buffer, // task name childTask, // task MED_PRIORITY, // priority 3, // task argc argv); // task argument pointers SEM_WAIT(childALive); // wait until child is going } SEM_SIGNAL(parentDead); // parent dies // while (1) SWAP; // keep parent alive while (1) { ++group_count[parent - 1]; SWAP; } return 0; } // end parentTask
// *********************************************************************** // *********************************************************************** // signal command void sem_signal(Semaphore* sem) // signal { if (sem) { printf("\nSignal %s", sem->name); SEM_SIGNAL(sem); } else my_printf("\nSemaphore not defined!"); return; } // end sem_signal
int carTask(int argc, char* argv[]) { int carIndex = atoi(argv[1]); int driverIndex; while(1){ int i = 0; while(i < 3){ SEM_WAIT(fillSeat[carIndex]); SWAP; SEM_SIGNAL(passengerRide); SWAP;//save passenger rideFinished[] semaphore SEM_WAIT(passengerJumpIntoRide); SWAP SEM_WAIT(parkMutex); SWAP myPark.numInCarLine--; SWAP myPark.numInCars++; SWAP myPark.numTicketsAvailable++; SWAP SEM_SIGNAL(parkMutex); SWAP SEM_SIGNAL(theTickets); SWAP if(i > 1){ currCar = carIndex+1; SWAP SEM_SIGNAL(noDriverYet); SWAP SEM_SIGNAL(helloDriverWakeUp); SWAP SEM_WAIT(ready2GoDriver); SWAP driverIndex = currDriver; SWAP } SEM_SIGNAL(seatFilled[carIndex]); SWAP; i++; } SEM_WAIT(rideOver[carIndex]); SWAP; int j = 0; while(j < 3){ SEM_WAIT(parkMutex); SWAP myPark.numInCars--; SWAP myPark.numInGiftLine++; SWAP SEM_SIGNAL(parkMutex); SWAP SEM_SIGNAL(rideFinished); SWAP j++; } SEM_SIGNAL(driverFinished[driverIndex]); SWAP }
// *********************************************************************** // *********************************************************************** // child task // argv[0] = group base name // argv[1] = parent # // argv[2] = # of siblings // int childTask(int argc, char* argv[]) // child Task { int parent = atoi(argv[1]); if ((parent<1) || (parent>NUM_PARENTS)) { printf("\n**Parent Error! Task %d, Parent %d", curTask, parent); return 0; // die!! } SEM_SIGNAL(childALive); // child is alive!! // count # of times scheduled while (1) { group_count[parent-1]++; SWAP; } return 0; } // end childTask
int tickDeltaClock() { int i; // Is it time to signal any semaphores? if(deltaClockSize && deltaClockTicker-- < 0 && --(dc[0].time) <= 0) { // For every entry that just expired, signal semaphore while(deltaClockSize > 0 && dc[0].time <= 0) { //printf("Signaling %s\n", dc[0].sem->name); SEM_SIGNAL(dc[0].sem); // shift everything down for(i=0; i<deltaClockSize-1; i++) { dc[i] = dc[i+1]; } deltaClockSize--; } deltaClockTicker = 10; } }
int P2_signal2(int argc, char* argv[]) // signal2 { SEM_SIGNAL(s2Sem); return 0; } // end signal
// *********************************************************************** int P2_signal1(int argc, char* argv[]) // signal1 { SEM_SIGNAL(s1Sem); return 0; } // end signal