secondary

This commit is contained in:
2025-04-09 15:57:05 +00:00
parent 3895f9a812
commit f735532766
3 changed files with 162 additions and 155 deletions
+19 -19
View File
@@ -1,19 +1,19 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/debian
{
"name": "Debian",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:bullseye"
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/debian
{
"name": "Debian",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:bullseye"
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
BIN
View File
Binary file not shown.
+143 -136
View File
@@ -1,137 +1,144 @@
// 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");
}
}
// 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;
// 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;
}