Add comments and reformat

This commit is contained in:
Isaac Shoebottom 2023-10-11 18:21:27 -03:00
parent fa28dec841
commit 840abae776

View File

@ -15,7 +15,7 @@
#define MAX_TIME_FOR_TRUCKER 3
#define MIN_TRIP_TIME 5
#define MAX_TRIP_TIME 10
#define RANDOM_WITHIN_RANGE(a,b,seed) (a+rand_r(&seed)%(b-a))
#define RANDOM_WITHIN_RANGE(a, b, seed) (a+rand_r(&seed)%(b-a))
// For pipes
#define READ_END 0
#define WRITE_END 1
@ -25,37 +25,43 @@ int houseFloor[2];
// Pipe between movers and truckers
int nextToTrucks[2];
//TODO: See if you can just use arg structure instead of making stack vars from the args
// Arg structure, need to assign id in loop, and a reference to a seed for rand_r(), since it mutates the seed
typedef struct args {
int id;
unsigned int seed;
} Args;
void* dweller(void* arg) {
Args *args = (Args*) arg;
// Routine for house dwellers
void *dweller(void *arg) {
Args *args = (Args *) arg; // Cast void* to Args*, as pthread_create() requires void*
int id = args->id;
unsigned int seed = args->seed;
printf("Hello from house dweller %d\n", id);
for(int i = 0; i < NUMBER_OF_BOXES_PER_DWELLER; i++) {
int weight = RANDOM_WITHIN_RANGE(MIN_BOX_WEIGHT, MAX_BOX_WEIGHT, seed);
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_HOUSE_DWELLER, MAX_TIME_FOR_HOUSE_DWELLER, seed);
sleep(time);
write(houseFloor[WRITE_END], &weight, sizeof(int));
for (int i = 0; i < NUMBER_OF_BOXES_PER_DWELLER; i++) {
int weight = RANDOM_WITHIN_RANGE(MIN_BOX_WEIGHT, MAX_BOX_WEIGHT, seed); // Generate random weight
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_HOUSE_DWELLER, MAX_TIME_FOR_HOUSE_DWELLER, seed); // Time to pack box
sleep(time); // Simulate packing time
write(houseFloor[WRITE_END], &weight, sizeof(int)); // Write weight to pipe
printf("House dweller %d created a box that weights %d in %d units of time\n", id, weight, time);
}
printf("House dweller %d is done packing\n", id);
free(args);
free(args); // Make sure to free the args struct
return NULL;
}
void* mover(void* arg) {
Args *args = (Args*) arg;
// Routine for movers
// Comments from dweller() apply here as well
void *mover(void *arg) {
Args *args = (Args *) arg;
int id = args->id;
unsigned int seed = args->seed;
int weight;
printf("Hello from mover %d\n", id);
while (read(houseFloor[READ_END], &weight, sizeof(int)) > 0) {
while (read(houseFloor[READ_END], &weight, sizeof(int)) > 0) { // Read from pipe, until dwellers are all done
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_MOVER, MAX_TIME_FOR_MOVER, seed);
sleep(time);
write(nextToTrucks[WRITE_END], &weight, sizeof(int));
@ -66,8 +72,9 @@ void* mover(void* arg) {
return NULL;
}
void* trucker(void* arg) {
Args *args = (Args*) arg;
// Routine for truckers
void *trucker(void *arg) {
Args *args = (Args *) arg;
int id = args->id;
unsigned int seed = args->seed;
int capacity = ROOM_IN_TRUCK;
@ -75,14 +82,14 @@ void* trucker(void* arg) {
int weight;
printf("Hello from trucker %d\n", id);
while (read(nextToTrucks[READ_END], &weight, sizeof(int)) > 0) {
while (read(nextToTrucks[READ_END], &weight, sizeof(int)) > 0) { // Read from pipe, until movers are all done
capacity--;
truckWeight += weight;
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_TRUCKER, MAX_TIME_FOR_TRUCKER, seed);
sleep(time);
printf("Trucker %d loaded up box of weight %d in %d units of time, room left: %d\n", id, weight, time, capacity);
if (capacity == 0) {
if (capacity == 0) { // If truck is full, go on trip, and reset
time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME, MAX_TRIP_TIME, seed);
printf("Full truck %d with load of %d units of mass departed. Round trip will take %d units of time\n", id, truckWeight, time);
sleep(time);
@ -90,7 +97,8 @@ void* trucker(void* arg) {
truckWeight = 0;
}
}
int time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME, MAX_TRIP_TIME, seed); // One way trip, so half the time
// If truck is not full, go on trip, and end, as there are no more boxes
int time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME, MAX_TRIP_TIME, seed);
printf("Not full truck %d with load of %d units of mass departed. One way will take %d units of time\n", id, truckWeight, time);
sleep(time);
printf("Truck %d is finished\n", id);
@ -105,46 +113,52 @@ int main(int argc, char **argv) {
pipe(houseFloor);
pipe(nextToTrucks);
// Get number of house dwellers, movers, and truckers
int numberOfHouseDwellers, numberOfMovers, numberOfTruckers;
printf("Please input number of people living in the house, number of movers and number of truck drivers\n");
scanf("%d %d %d", &numberOfHouseDwellers, &numberOfMovers, &numberOfTruckers);
// Create thread pools, and pass in args
pthread_t dwellerThreads[numberOfHouseDwellers];
pthread_t moverThreads[numberOfMovers];
pthread_t truckerThreads[numberOfTruckers];
for(int i = 0; i < numberOfHouseDwellers; i++) {
Args *args = (Args*) malloc(sizeof(Args));
for (int i = 0; i < numberOfHouseDwellers; i++) {
Args *args = (Args *) malloc(sizeof(Args));
args->id = i;
args->seed = rand();
pthread_create(&dwellerThreads[i], NULL, &dweller, (void*) args);
pthread_create(&dwellerThreads[i], NULL, &dweller, (void *) args);
}
for(int i = 0; i < numberOfMovers; i++) {
Args *args = (Args*) malloc(sizeof(Args));
for (int i = 0; i < numberOfMovers; i++) {
Args *args = (Args *) malloc(sizeof(Args));
args->id = i;
args->seed = rand();
pthread_create(&moverThreads[i], NULL, &mover, (void*) args);
pthread_create(&moverThreads[i], NULL, &mover, (void *) args);
}
for(int i = 0; i < numberOfTruckers; i++) {
Args *args = (Args*) malloc(sizeof(Args));
for (int i = 0; i < numberOfTruckers; i++) {
Args *args = (Args *) malloc(sizeof(Args));
args->id = i;
args->seed = rand();
pthread_create(&truckerThreads[i], NULL, &trucker, (void*) args);
pthread_create(&truckerThreads[i], NULL, &trucker, (void *) args);
}
// Wait for dwellers to finish and then close the house floor write, so movers know to stop
for (int i = 0; i < numberOfHouseDwellers; i++) {
pthread_join(dwellerThreads[i], NULL);
}
close(houseFloor[WRITE_END]);
// Wait for movers to finish and then close the next to trucks write, so truckers know to stop
// Also endings the read end of the house floor pipe, as no more boxes will be written to it
for (int i = 0; i < numberOfMovers; i++) {
pthread_join(moverThreads[i], NULL);
}
close(houseFloor[READ_END]);
close(nextToTrucks[WRITE_END]);
// Wait for truckers to finish, and close the read end of the next to trucks pipe, as no more boxes will be written to it
for (int i = 0; i < numberOfTruckers; i++) {
pthread_join(truckerThreads[i], NULL);
}
close(nextToTrucks[READ_END]);
printf("Moving is finished!\n");
return 0;
return EXIT_SUCCESS;
}