144 lines
4.2 KiB
C
144 lines
4.2 KiB
C
// HW3 - COS331
|
|
// Pease
|
|
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <semaphore.h>
|
|
#include <sys/stat.h>
|
|
#include <stdio.h>
|
|
|
|
// Globals
|
|
unsigned int seed = 8;
|
|
int random_array[1024][1024];
|
|
int total_random[256];
|
|
pthread_t ThreadID;
|
|
int modeFlag = 0; // 0 for no semaphore, 1 for semaphore, 2 for 256 semaphores
|
|
|
|
sem_t *one_semaphore;
|
|
sem_t *multiple_semaphores[256];
|
|
|
|
// Struct
|
|
struct threadArgVector {
|
|
int startRow;
|
|
int endRow;
|
|
int threadNum;
|
|
};
|
|
|
|
// Thread
|
|
void *countNumsAndStore(void * arguments) {
|
|
// Format the passed args
|
|
struct threadArgVector *passedArgs = arguments;
|
|
int startRow = passedArgs->startRow;
|
|
int endRow = passedArgs->endRow;
|
|
int threadNum = passedArgs->threadNum;
|
|
|
|
// Initialize local array to 0
|
|
int localCount[256];
|
|
for (int i = 0; i < 256; i++) localCount[i] = 0;
|
|
|
|
// One semaphore wait
|
|
if (modeFlag == 1) sem_wait(one_semaphore);
|
|
|
|
// Protected Critical Section
|
|
for(int i = startRow; i < endRow; i++) {
|
|
for (int j = 0; j < 1024; j++) {
|
|
int value = random_array[i][j];
|
|
// 255 semaphores wait
|
|
if (modeFlag == 2) sem_wait(multiple_semaphores[value]);
|
|
total_random[random_array[i][j]]++;
|
|
if (modeFlag == 2) sem_post(multiple_semaphores[value]);
|
|
}
|
|
}
|
|
|
|
// One semaphore post
|
|
if (modeFlag == 1) sem_post(one_semaphore);
|
|
}
|
|
|
|
// Thread Launcher
|
|
void startThreadOutput(int threadCount) {
|
|
// Array of thread ID's
|
|
pthread_t threadIds[threadCount];
|
|
|
|
// semaphore setups
|
|
if (modeFlag == 1) {
|
|
one_semaphore = sem_open("one_semaphore", O_CREAT, 777, 1);
|
|
} else if (modeFlag == 2) {
|
|
for (int i = 0; i < 256; i++) {
|
|
char semName[10];
|
|
sprintf(semName, "S%d", i);
|
|
multiple_semaphores[i] = sem_open(semName, O_CREAT, 777, 1);
|
|
}
|
|
}
|
|
// Reset the total_random array to 0 before counting
|
|
for (int i = 0; i < 256; i++) total_random[i] = 0;
|
|
|
|
// Create the set number of threads
|
|
for (int i = 0; i < threadCount; i++) {
|
|
struct threadArgVector currThreadArgs = {
|
|
.startRow = (i * 1024) / threadCount,
|
|
.endRow = ((i + 1) * 1024) / threadCount,
|
|
.threadNum = i
|
|
};
|
|
int ret = pthread_create(&threadIds[i], NULL, countNumsAndStore, (void *) &currThreadArgs);
|
|
};
|
|
|
|
for (int i = 0; i < threadCount; i++) {
|
|
// Wait for each thread to finish
|
|
int ret = pthread_join(threadIds[i], NULL);
|
|
}
|
|
|
|
char* modeString = modeFlag == 0 ? "No Semaphore" :
|
|
modeFlag == 1 ? "1 Semaphore" :
|
|
modeFlag == 2 ? "256 Semaphores" : "Unknown Mode";
|
|
|
|
// Print the total count in table format, with hexadecimal columns and rows
|
|
// I.E Row "F" and Column "F" means the count of number 255
|
|
printf("Total Counts of Each Number (%s):\n", modeString);
|
|
for (int col = 0; col < 16; col++) printf("\t%X", col);
|
|
printf("\n");
|
|
for (int row = 0; row < 16; row++) {
|
|
printf("%X \t", row);
|
|
for (int col = 0; col < 16; col++) {
|
|
int index = row * 16 + col;
|
|
printf("%d \t", total_random[index]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("\n");
|
|
|
|
if (modeFlag == 1) {
|
|
sem_close(one_semaphore);
|
|
sem_unlink("one_semaphore");
|
|
} else if (modeFlag == 2) {
|
|
for (int i = 0; i < 256; i++) {
|
|
char semName[10];
|
|
sprintf(semName, "S%d", i);
|
|
sem_close(multiple_semaphores[i]);
|
|
sem_unlink(semName);
|
|
}
|
|
}
|
|
}
|
|
|
|
// main
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int threadCount = atoi(argv[1]);
|
|
srand(seed);
|
|
|
|
// Initialize the random array
|
|
for (int i = 0; i < 1024; i++) {
|
|
for (int j = 0; j < 1024; j++) {
|
|
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);
|
|
modeFlag = 2; // Set modeFlag to 2 for 256 semaphores
|
|
startThreadOutput(threadCount);
|
|
|
|
return 0;
|
|
} |