Inline arg variables in threads
This commit is contained in:
parent
840abae776
commit
af78125046
@ -25,8 +25,6 @@ int houseFloor[2];
|
|||||||
// Pipe between movers and truckers
|
// Pipe between movers and truckers
|
||||||
int nextToTrucks[2];
|
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
|
// 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 {
|
typedef struct args {
|
||||||
int id;
|
int id;
|
||||||
@ -36,18 +34,16 @@ typedef struct args {
|
|||||||
// Routine for house dwellers
|
// Routine for house dwellers
|
||||||
void *dweller(void *arg) {
|
void *dweller(void *arg) {
|
||||||
Args *args = (Args *) arg; // Cast void* to Args*, as pthread_create() requires void*
|
Args *args = (Args *) arg; // Cast void* to Args*, as pthread_create() requires void*
|
||||||
int id = args->id;
|
printf("Hello from house dweller %d\n", 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++) {
|
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 weight = RANDOM_WITHIN_RANGE(MIN_BOX_WEIGHT, MAX_BOX_WEIGHT, args->seed); // Generate random weight
|
||||||
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_HOUSE_DWELLER, MAX_TIME_FOR_HOUSE_DWELLER, seed); // Time to pack box
|
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_HOUSE_DWELLER, MAX_TIME_FOR_HOUSE_DWELLER, args->seed); // Time to pack box
|
||||||
sleep(time); // Simulate packing time
|
sleep(time); // Simulate packing time
|
||||||
write(houseFloor[WRITE_END], &weight, sizeof(int)); // Write weight to pipe
|
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 created a box that weights %d in %d units of time\n", args->id, weight, time);
|
||||||
}
|
}
|
||||||
printf("House dweller %d is done packing\n", id);
|
printf("House dweller %d is done packing\n", args->id);
|
||||||
free(args); // Make sure to free the args struct
|
free(args); // Make sure to free the args struct
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -56,18 +52,16 @@ void *dweller(void *arg) {
|
|||||||
// Comments from dweller() apply here as well
|
// Comments from dweller() apply here as well
|
||||||
void *mover(void *arg) {
|
void *mover(void *arg) {
|
||||||
Args *args = (Args *) arg;
|
Args *args = (Args *) arg;
|
||||||
int id = args->id;
|
|
||||||
unsigned int seed = args->seed;
|
|
||||||
int weight;
|
int weight;
|
||||||
|
|
||||||
printf("Hello from mover %d\n", id);
|
printf("Hello from mover %d\n", args->id);
|
||||||
while (read(houseFloor[READ_END], &weight, sizeof(int)) > 0) { // Read from pipe, until dwellers are all done
|
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);
|
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_MOVER, MAX_TIME_FOR_MOVER, args->seed);
|
||||||
sleep(time);
|
sleep(time);
|
||||||
write(nextToTrucks[WRITE_END], &weight, sizeof(int));
|
write(nextToTrucks[WRITE_END], &weight, sizeof(int));
|
||||||
printf("Mover %d brought down a box of weight %d in %d units of time\n", id, weight, time);
|
printf("Mover %d brought down a box of weight %d in %d units of time\n", args->id, weight, time);
|
||||||
}
|
}
|
||||||
printf("Mover %d is done moving\n", id);
|
printf("Mover %d is done moving\n", args->id);
|
||||||
free(args);
|
free(args);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -75,38 +69,35 @@ void *mover(void *arg) {
|
|||||||
// Routine for truckers
|
// Routine for truckers
|
||||||
void *trucker(void *arg) {
|
void *trucker(void *arg) {
|
||||||
Args *args = (Args *) arg;
|
Args *args = (Args *) arg;
|
||||||
int id = args->id;
|
|
||||||
unsigned int seed = args->seed;
|
|
||||||
int capacity = ROOM_IN_TRUCK;
|
int capacity = ROOM_IN_TRUCK;
|
||||||
int truckWeight = 0;
|
int truckWeight = 0;
|
||||||
int weight;
|
int weight;
|
||||||
|
|
||||||
printf("Hello from trucker %d\n", id);
|
printf("Hello from trucker %d\n", args->id);
|
||||||
while (read(nextToTrucks[READ_END], &weight, sizeof(int)) > 0) { // Read from pipe, until movers are all done
|
while (read(nextToTrucks[READ_END], &weight, sizeof(int)) > 0) { // Read from pipe, until movers are all done
|
||||||
capacity--;
|
capacity--;
|
||||||
truckWeight += weight;
|
truckWeight += weight;
|
||||||
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_TRUCKER, MAX_TIME_FOR_TRUCKER, seed);
|
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_TRUCKER, MAX_TIME_FOR_TRUCKER, args->seed);
|
||||||
sleep(time);
|
sleep(time);
|
||||||
printf("Trucker %d loaded up box of weight %d in %d units of time, room left: %d\n", id, weight, time, capacity);
|
printf("Trucker %d loaded up box of weight %d in %d units of time, room left: %d\n", args->id, weight, time, capacity);
|
||||||
|
|
||||||
if (capacity == 0) { // If truck is full, go on trip, and reset
|
if (capacity == 0) { // If truck is full, go on trip, and reset
|
||||||
time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME, MAX_TRIP_TIME, seed);
|
time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME, MAX_TRIP_TIME, args->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);
|
printf("Full truck %d with load of %d units of mass departed. Round trip will take %d units of time\n", args->id, truckWeight, time);
|
||||||
sleep(time);
|
sleep(time);
|
||||||
capacity = ROOM_IN_TRUCK;
|
capacity = ROOM_IN_TRUCK;
|
||||||
truckWeight = 0;
|
truckWeight = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If truck is not full, go on trip, and end, as there are no more boxes
|
// 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);
|
int time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME, MAX_TRIP_TIME, args->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);
|
printf("Not full truck %d with load of %d units of mass departed. One way trip will take %d units of time\n", args->id, truckWeight, time);
|
||||||
sleep(time);
|
sleep(time);
|
||||||
printf("Truck %d is finished\n", id);
|
printf("Truck %d is finished\n", args->id);
|
||||||
free(args);
|
free(args);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user