initial
This commit is contained in:
@@ -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"
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
## cos331-hw3 ##
|
||||
|
||||
[](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
|
||||
[](https://drone.nicholaspease.com/umaine-npease/cos331-hw3)
|
||||
[](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos331-hw3)
|
||||

|
||||
<hr>
|
||||
## cos331-hw3 ##
|
||||
|
||||
[](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
|
||||
[](https://drone.nicholaspease.com/umaine-npease/cos331-hw3)
|
||||
[](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos331-hw3)
|
||||

|
||||
<hr>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user