final code
This commit is contained in:
Binary file not shown.
+18
-10
@@ -1,13 +1,14 @@
|
||||
// HW3 - COS331
|
||||
// Pease
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// Globals
|
||||
unsigned int seed = 8;
|
||||
@@ -58,6 +59,10 @@ void *countNumsAndStore(void * arguments) {
|
||||
|
||||
// Thread Launcher
|
||||
void startThreadOutput(int threadCount) {
|
||||
// Get the start time
|
||||
struct timeval startTime, endTime;
|
||||
gettimeofday(&startTime,NULL);
|
||||
|
||||
// Array of thread ID's
|
||||
pthread_t threadIds[threadCount];
|
||||
|
||||
@@ -106,7 +111,6 @@ void startThreadOutput(int threadCount) {
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
if (modeFlag == 1) {
|
||||
sem_close(one_semaphore);
|
||||
@@ -119,6 +123,11 @@ void startThreadOutput(int threadCount) {
|
||||
sem_unlink(semName);
|
||||
}
|
||||
}
|
||||
// Get end time
|
||||
gettimeofday(&endTime,NULL);
|
||||
// Print out the time
|
||||
float difference = ((endTime.tv_sec * 1000000 + endTime.tv_usec) - (startTime.tv_sec * 1000000 + startTime.tv_usec)) * 0.000001;
|
||||
printf("Duration: %f seconds\n\n", difference);
|
||||
}
|
||||
|
||||
// main
|
||||
@@ -133,12 +142,11 @@ int main(int argc, char *argv[])
|
||||
random_array[i][j] = rand() % 256;
|
||||
}
|
||||
}
|
||||
|
||||
// startThreadOutput(threadCount); // modeFlag already set to 0 for no semaphore
|
||||
// modeFlag = 1; // Set modeFlag to 1 for semaphore usage
|
||||
// startThreadOutput(threadCount);
|
||||
|
||||
startThreadOutput(threadCount); // modeFlag already set to 0 for no semaphore
|
||||
modeFlag = 1; // Set modeFlag to 1 for semaphore usage
|
||||
startThreadOutput(threadCount);
|
||||
modeFlag = 2; // Set modeFlag to 2 for 256 semaphores
|
||||
startThreadOutput(threadCount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user