This commit is contained in:
2025-04-05 22:35:30 +00:00
parent 8eeec62a1f
commit 18f760273e
4 changed files with 113 additions and 7 deletions
+19
View File
@@ -0,0 +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"
}
+7 -7
View File
@@ -1,7 +1,7 @@
## cos331-hw3 ##
[![](https://gitea-actions.nicholaspease.com/actions/umaine-npease/cos331-hw3/badge?label=build&style=flat&branch=main)](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
[![](https://drone.nicholaspease.com/api/badges/umaine-npease/cos331-hw3/status.svg)](https://drone.nicholaspease.com/umaine-npease/cos331-hw3)
[![](https://wakaapi.nicholaspease.com/api/badge/LAX18/interval:any/project:cos331-hw3)](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos331-hw3)
![](https://server1.nicholaspease.com/badges/cloc/npease/cos331-hw3.svg)
<hr>
## cos331-hw3 ##
[![](https://gitea-actions.nicholaspease.com/actions/umaine-npease/cos331-hw3/badge?label=build&style=flat&branch=main)](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
[![](https://drone.nicholaspease.com/api/badges/umaine-npease/cos331-hw3/status.svg)](https://drone.nicholaspease.com/umaine-npease/cos331-hw3)
[![](https://wakaapi.nicholaspease.com/api/badge/LAX18/interval:any/project:cos331-hw3)](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos331-hw3)
![](https://server1.nicholaspease.com/badges/cloc/npease/cos331-hw3.svg)
<hr>
BIN
View File
Binary file not shown.
+87
View File
@@ -0,0 +1,87 @@
// 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;
// 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;
// Do the counting math
for(int i = startRow; i < endRow; i++) {
for (int j = 0; j < 1024; j++) {
int value = random_array[i][j];
localCount[value]++;
}
}
// No semaphore used, so we directly update the global total array
for (int i = 0; i < 256; i++) total_random[i] += localCount[i];
}
// 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;
}
}
// Array of thread ID's
pthread_t threadIds[threadCount];
// 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);
}
// Print the total count
printf("Total counts of each number:\n");
for (int i = 0; i < 256; i++) {
printf("%d: %d\n", i, total_random[i]);
}
return 0;
}