progress on desktop
This commit is contained in:
BIN
00-WSLSetup/hello
Normal file
BIN
00-WSLSetup/hello
Normal file
Binary file not shown.
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
typedef struct node {
|
typedef struct node {
|
||||||
int value;
|
int value;
|
||||||
struct node* next;
|
struct node *next;
|
||||||
} node;
|
} node;
|
||||||
|
|
||||||
typedef struct stackm {
|
typedef struct stackm {
|
||||||
@@ -16,63 +16,134 @@ typedef struct stackm {
|
|||||||
|
|
||||||
|
|
||||||
void smInit(struct stackm *myStack){
|
void smInit(struct stackm *myStack){
|
||||||
(myStack->top) = NULL;
|
myStack->top = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smSize(struct stackm *myStack){
|
int smSize(struct stackm *myStack){
|
||||||
while(myStack->top->next != NULL) {
|
node *current = myStack->top;
|
||||||
|
int count = 0;
|
||||||
|
while(current != NULL) {
|
||||||
|
count++;
|
||||||
|
current = current->next;
|
||||||
}
|
}
|
||||||
return myStack->top;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smPush(struct stackm *myStack, int toStore){
|
int smPush(struct stackm *myStack, int toStore){
|
||||||
node node;
|
|
||||||
node.value = toStore;
|
node *newNode = (node *)malloc(sizeof(node));
|
||||||
|
if (newNode == NULL) {
|
||||||
*(myStack->top->next) = node;
|
return 0;
|
||||||
|
} else {
|
||||||
|
newNode->value = toStore;
|
||||||
|
newNode->next = myStack->top;
|
||||||
|
myStack->top = newNode;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smPop(struct stackm *myStack){
|
int smPop(struct stackm *myStack){
|
||||||
|
if(myStack->top == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
node *temp = myStack->top;
|
||||||
|
myStack->top = temp->next;
|
||||||
|
free(temp);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smTop(struct stackm *myStack, int* toStore){
|
int smTop(struct stackm *myStack, int* toStore){
|
||||||
|
if (myStack->top == NULL || toStore == NULL) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
toStore = (int*)myStack->top;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void smClear(struct stackm *myStack){
|
void smClear(struct stackm *myStack){
|
||||||
|
while (myStack->top != NULL) {
|
||||||
|
node *temp = myStack->top;
|
||||||
|
myStack->top = temp->next;
|
||||||
|
free(temp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void smPrint(struct stackm *myStack){
|
void smPrint(struct stackm *myStack){
|
||||||
|
node *temp = myStack->top;
|
||||||
|
printf("==Stack Contents==\n");
|
||||||
|
printf("Top -> ");
|
||||||
|
while (temp != NULL) {
|
||||||
|
printf("%d ", temp->value);
|
||||||
|
temp = temp->next;
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smAdd(struct stackm* myStack){
|
int smAdd(struct stackm* myStack){
|
||||||
|
if(smSize(myStack) <= 2){
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
int sum = (myStack->top->value) + (myStack->top->next->value);
|
||||||
|
smPush(myStack,sum);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smSub(struct stackm* myStack){
|
int smSub(struct stackm* myStack){
|
||||||
|
if(smSize(myStack) <= 2){
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
int result = (myStack->top->value) - (myStack->top->next->value);
|
||||||
|
smPop(myStack);
|
||||||
|
smPop(myStack);
|
||||||
|
smPush(myStack,result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smMult(struct stackm* myStack){
|
int smMult(struct stackm* myStack){
|
||||||
|
if(smSize(myStack) <= 2){
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
int sum = (myStack->top->value) * (myStack->top->next->value);
|
||||||
|
smPush(myStack,sum);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int smRotate(struct stackm* myStack, int depth){
|
int smRotate(struct stackm* myStack, int depth){
|
||||||
|
if(depth == 0){
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
node *temp = myStack->top;
|
||||||
|
int count = 1;
|
||||||
|
while (count < depth && temp != NULL) {
|
||||||
|
temp = temp->next;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if (temp == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
node *nNode = temp;
|
||||||
|
while (temp->next != NULL) {
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
temp->next = myStack->top;
|
||||||
|
myStack->top = nNode->next;
|
||||||
|
nNode->next = NULL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
BIN
02-TeenyTinyShell/ttsh
Normal file
BIN
02-TeenyTinyShell/ttsh
Normal file
Binary file not shown.
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define INPUT_MAX 256
|
#define INPUT_MAX 256
|
||||||
#define CMD_MAX 5
|
#define CMD_MAX 5
|
||||||
@@ -71,6 +72,8 @@ int main()
|
|||||||
char user_input[INPUT_MAX];
|
char user_input[INPUT_MAX];
|
||||||
char cmd_strs[CMD_MAX][INPUT_MAX];
|
char cmd_strs[CMD_MAX][INPUT_MAX];
|
||||||
|
|
||||||
|
int quit_flag = 0;
|
||||||
|
|
||||||
// TODO need to be able to get input from
|
// TODO need to be able to get input from
|
||||||
// the user in a loop
|
// the user in a loop
|
||||||
|
|
||||||
@@ -84,6 +87,11 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Figure out how to handle the 'quit' command
|
// TODO: Figure out how to handle the 'quit' command
|
||||||
|
if (strcmp(user_input, "quit") == 0){
|
||||||
|
printf("Quitting...");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Chop the input into command strings
|
// Chop the input into command strings
|
||||||
int cmd_count = parse_commands(user_input, cmd_strs);
|
int cmd_count = parse_commands(user_input, cmd_strs);
|
||||||
@@ -101,6 +109,7 @@ int main()
|
|||||||
// NOTE: the command name is always the first argument
|
// NOTE: the command name is always the first argument
|
||||||
// 2) fork a process
|
// 2) fork a process
|
||||||
// 3) execute the command with execvp
|
// 3) execute the command with execvp
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
BIN
02-TeenyTinyShell/ttsh.o
Normal file
BIN
02-TeenyTinyShell/ttsh.o
Normal file
Binary file not shown.
0
03-MatrixAddition (fork)/mat
Normal file
0
03-MatrixAddition (fork)/mat
Normal file
81
03-MatrixAddition (fork)/matAddFork.c
Normal file
81
03-MatrixAddition (fork)/matAddFork.c
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
|
||||||
|
int i, j, k, sum = 0;
|
||||||
|
int shmid;
|
||||||
|
int *shm;
|
||||||
|
|
||||||
|
// Allocate shared memory
|
||||||
|
shmid = shmget(IPC_PRIVATE, MAX * MAX * sizeof(int), 0666 | IPC_CREAT);
|
||||||
|
if (shmid < 0) {
|
||||||
|
perror("shmget");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attach shared memory
|
||||||
|
shm = shmat(shmid, NULL, 0);
|
||||||
|
if (shm == (int *)-1) {
|
||||||
|
perror("shmat");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize matrices A and B
|
||||||
|
for (i = 0; i < MAX; i++) {
|
||||||
|
for (j = 0; j < MAX; j++) {
|
||||||
|
A[i][j] = i + j;
|
||||||
|
B[i][j] = i - j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fork a child process
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
if (pid == -1) {
|
||||||
|
perror("fork");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else if (pid == 0) { // Child process
|
||||||
|
for (i = 0; i < MAX; i++) {
|
||||||
|
for (j = 0; j < MAX; j++) {
|
||||||
|
sum = 0;
|
||||||
|
for (k = 0; k < MAX; k++) {
|
||||||
|
sum += A[i][k] * B[k][j];
|
||||||
|
}
|
||||||
|
*(shm + i * MAX + j) = sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else { // Parent process
|
||||||
|
wait(NULL);
|
||||||
|
|
||||||
|
// Retrieve result from shared memory
|
||||||
|
for (i = 0; i < MAX; i++) {
|
||||||
|
for (j = 0; j < MAX; j++) {
|
||||||
|
C[i][j] = *(shm + i * MAX + j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print result matrix C
|
||||||
|
printf("Matrix C:\n");
|
||||||
|
for (i = 0; i < MAX; i++) {
|
||||||
|
for (j = 0; j < MAX; j++) {
|
||||||
|
printf("%d ", C[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detach and remove shared memory
|
||||||
|
shmdt(shm);
|
||||||
|
shmctl(shmid, IPC_RMID, NULL);
|
||||||
|
munmap(shm);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
16
03-MatrixAddition (fork)/matRead.c
Normal file
16
03-MatrixAddition (fork)/matRead.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int rows, columns;
|
||||||
|
FILE* input = fopen(argv[1], "r");
|
||||||
|
fscanf(input, "%d", &rows);
|
||||||
|
fscanf(input, "%d", &columns);
|
||||||
|
for(int i = 0; i < rows; i++) {
|
||||||
|
for(int j = 0; j < columns; j++) {
|
||||||
|
int value;
|
||||||
|
fscanf(input, "%d", &value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(input);
|
||||||
|
}
|
||||||
12
CS3841.code-workspace
Normal file
12
CS3841.code-workspace
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"uri": "vscode-remote://wsl+ubuntu/mnt/c/Users/Trevor/Documents/GitRepos/CS3841"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {
|
||||||
|
"files.associations": {
|
||||||
|
"stdio.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user