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 // 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 // README at: https://github.com/devcontainers/templates/tree/main/src/debian
{ {
"name": "Debian", "name": "Debian",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:bullseye" "image": "mcr.microsoft.com/devcontainers/base:bullseye"
// Features to add to the dev container. More info: https://containers.dev/features. // Features to add to the dev container. More info: https://containers.dev/features.
// "features": {}, // "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally. // Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [], // "forwardPorts": [],
// Configure tool-specific properties. // Configure tool-specific properties.
// "customizations": {}, // "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root" // "remoteUser": "root"
} }
BIN
View File
Binary file not shown.
+143 -136
View File
@@ -1,137 +1,144 @@
// HW3 - COS331 // HW3 - COS331
// Pease // Pease
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <semaphore.h> #include <semaphore.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
// Globals // Globals
unsigned int seed = 8; unsigned int seed = 8;
int random_array[1024][1024]; int random_array[1024][1024];
int total_random[256]; int total_random[256];
pthread_t ThreadID; pthread_t ThreadID;
int modeFlag = 0; // 0 for no semaphore, 1 for semaphore, 2 for 256 semaphores int modeFlag = 0; // 0 for no semaphore, 1 for semaphore, 2 for 256 semaphores
sem_t *one_semaphore; sem_t *one_semaphore;
sem_t *multiple_semaphores[256]; sem_t *multiple_semaphores[256];
// Struct // Struct
struct threadArgVector { struct threadArgVector {
int startRow; int startRow;
int endRow; int endRow;
int threadNum; int threadNum;
}; };
// Thread // Thread
void *countNumsAndStore(void * arguments) { void *countNumsAndStore(void * arguments) {
// Format the passed args // Format the passed args
struct threadArgVector *passedArgs = arguments; struct threadArgVector *passedArgs = arguments;
int startRow = passedArgs->startRow; int startRow = passedArgs->startRow;
int endRow = passedArgs->endRow; int endRow = passedArgs->endRow;
int threadNum = passedArgs->threadNum; int threadNum = passedArgs->threadNum;
// Initialize local array to 0 // Initialize local array to 0
int localCount[256]; int localCount[256];
for (int i = 0; i < 256; i++) localCount[i] = 0; for (int i = 0; i < 256; i++) localCount[i] = 0;
// One semaphore wait // One semaphore wait
if (modeFlag == 1) sem_wait(one_semaphore); if (modeFlag == 1) sem_wait(one_semaphore);
// Protected Critical Section // Protected Critical Section
for(int i = startRow; i < endRow; i++) { for(int i = startRow; i < endRow; i++) {
for (int j = 0; j < 1024; j++) { for (int j = 0; j < 1024; j++) {
int value = random_array[i][j]; int value = random_array[i][j];
// 255 semaphores wait // 255 semaphores wait
if (modeFlag == 2) sem_wait(multiple_semaphores[value]); if (modeFlag == 2) sem_wait(multiple_semaphores[value]);
total_random[random_array[i][j]]++; total_random[random_array[i][j]]++;
if (modeFlag == 2) sem_post(multiple_semaphores[value]); if (modeFlag == 2) sem_post(multiple_semaphores[value]);
} }
} }
// One semaphore post // One semaphore post
if (modeFlag == 1) sem_post(one_semaphore); if (modeFlag == 1) sem_post(one_semaphore);
} }
// Thread Launcher // Thread Launcher
void startThreadOutput(int threadCount) { void startThreadOutput(int threadCount) {
// Array of thread ID's // Array of thread ID's
pthread_t threadIds[threadCount]; pthread_t threadIds[threadCount];
// semaphore setups // semaphore setups
if (modeFlag == 1) { if (modeFlag == 1) {
one_semaphore = sem_open("one_semaphore", O_CREAT, 777, 1); one_semaphore = sem_open("one_semaphore", O_CREAT, 777, 1);
} else if (modeFlag == 2) { } else if (modeFlag == 2) {
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
char semName[10]; char semName[10];
sprintf(semName, "S%d", i); sprintf(semName, "S%d", i);
multiple_semaphores[i] = sem_open(semName, O_CREAT, 777, 1); multiple_semaphores[i] = sem_open(semName, O_CREAT, 777, 1);
} }
} }
// Reset the total_random array to 0 before counting // Reset the total_random array to 0 before counting
for (int i = 0; i < 256; i++) total_random[i] = 0; for (int i = 0; i < 256; i++) total_random[i] = 0;
// Create the set number of threads // Create the set number of threads
for (int i = 0; i < threadCount; i++) { for (int i = 0; i < threadCount; i++) {
struct threadArgVector currThreadArgs = { struct threadArgVector currThreadArgs = {
.startRow = (i * 1024) / threadCount, .startRow = (i * 1024) / threadCount,
.endRow = ((i + 1) * 1024) / threadCount, .endRow = ((i + 1) * 1024) / threadCount,
.threadNum = i .threadNum = i
}; };
int ret = pthread_create(&threadIds[i], NULL, countNumsAndStore, (void *) &currThreadArgs); int ret = pthread_create(&threadIds[i], NULL, countNumsAndStore, (void *) &currThreadArgs);
}; };
for (int i = 0; i < threadCount; i++) { for (int i = 0; i < threadCount; i++) {
// Wait for each thread to finish // Wait for each thread to finish
int ret = pthread_join(threadIds[i], NULL); int ret = pthread_join(threadIds[i], NULL);
} }
char* modeString = modeFlag == 0 ? "No Semaphore" : char* modeString = modeFlag == 0 ? "No Semaphore" :
modeFlag == 1 ? "1 Semaphore" : modeFlag == 1 ? "1 Semaphore" :
modeFlag == 2 ? "256 Semaphores" : "Unknown Mode"; modeFlag == 2 ? "256 Semaphores" : "Unknown Mode";
// Print the total count in table format, with hexadecimal columns and rows // 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 // I.E Row "F" and Column "F" means the count of number 255
printf("Total Counts of Each Number (%s):\n", modeString); printf("Total Counts of Each Number (%s):\n", modeString);
for (int col = 0; col < 16; col++) printf("\t%X", col); for (int col = 0; col < 16; col++) printf("\t%X", col);
printf("\n"); printf("\n");
for (int row = 0; row < 16; row++) { for (int row = 0; row < 16; row++) {
printf("%X \t", row); printf("%X \t", row);
for (int col = 0; col < 16; col++) { for (int col = 0; col < 16; col++) {
int index = row * 16 + col; int index = row * 16 + col;
printf("%d \t", total_random[index]); printf("%d \t", total_random[index]);
} }
printf("\n"); printf("\n");
} }
printf("\n"); printf("\n");
if (modeFlag == 1) { if (modeFlag == 1) {
sem_close(one_semaphore); sem_close(one_semaphore);
sem_unlink("one_semaphore"); sem_unlink("one_semaphore");
} } else if (modeFlag == 2) {
} for (int i = 0; i < 256; i++) {
char semName[10];
// main sprintf(semName, "S%d", i);
int main(int argc, char *argv[]) sem_close(multiple_semaphores[i]);
{ sem_unlink(semName);
int threadCount = atoi(argv[1]); }
srand(seed); }
}
// Initialize the random array
for (int i = 0; i < 1024; i++) { // main
for (int j = 0; j < 1024; j++) { int main(int argc, char *argv[])
random_array[i][j] = rand() % 256; {
} int threadCount = atoi(argv[1]);
} srand(seed);
// startThreadOutput(threadCount); // modeFlag already set to 0 for no semaphore // Initialize the random array
// modeFlag = 1; // Set modeFlag to 1 for semaphore usage for (int i = 0; i < 1024; i++) {
// startThreadOutput(threadCount); for (int j = 0; j < 1024; j++) {
modeFlag = 2; // Set modeFlag to 2 for 256 semaphores random_array[i][j] = rand() % 256;
startThreadOutput(threadCount); }
}
return 0;
// 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;
} }