diff options
author | Matt Strapp <matt@mattstrapp.net> | 2022-05-24 11:18:46 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2022-05-24 11:19:55 -0500 |
commit | 7a73162607544204032aa66cce755daf21edebda (patch) | |
tree | 58578e01f15f34a855d99c32898db9d7a1603e67 /csci4061 | |
parent | do some stuff (diff) | |
download | homework-7a73162607544204032aa66cce755daf21edebda.tar homework-7a73162607544204032aa66cce755daf21edebda.tar.gz homework-7a73162607544204032aa66cce755daf21edebda.tar.bz2 homework-7a73162607544204032aa66cce755daf21edebda.tar.lz homework-7a73162607544204032aa66cce755daf21edebda.tar.xz homework-7a73162607544204032aa66cce755daf21edebda.tar.zst homework-7a73162607544204032aa66cce755daf21edebda.zip |
Graduate
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'csci4061')
27 files changed, 1191 insertions, 0 deletions
diff --git a/csci4061/091420_breakout/Makefile b/csci4061/091420_breakout/Makefile new file mode 100644 index 0000000..1e807a9 --- /dev/null +++ b/csci4061/091420_breakout/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-I./include +OBJ=main.o name.o address.o major.o +EXE=w2.out + +all: $(EXE) + +$(EXE): $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) + +%.o: %.c + $(CC) -c -o $@ $< $(CFLAGS) +clean: + rm *.o $(EXE) +run: + ./$(EXE)
\ No newline at end of file diff --git a/csci4061/091420_breakout/address.c b/csci4061/091420_breakout/address.c new file mode 100644 index 0000000..3f36f70 --- /dev/null +++ b/csci4061/091420_breakout/address.c @@ -0,0 +1,15 @@ +#include "include/main.h" +#include "include/address.h" + +/* + * Write getAddress function + * void getAddress(char * address); + * Prints a prompt "Enter your address: " to get an address, + * Reads in the user's address + */ + +void getAddress(char * address) { + printf("Enter your address: "); + fgets(address, maxLen, stdin); + fflush(stdin); +}
\ No newline at end of file diff --git a/csci4061/091420_breakout/include/address.h b/csci4061/091420_breakout/include/address.h new file mode 100644 index 0000000..97a4c03 --- /dev/null +++ b/csci4061/091420_breakout/include/address.h @@ -0,0 +1,6 @@ +#ifndef INC_091420_BREAKOUT_ADDRESS_H +#define INC_091420_BREAKOUT_ADDRESS_H + +void getAddress(char * address); + +#endif //INC_091420_BREAKOUT_ADDRESS_H diff --git a/csci4061/091420_breakout/include/main.h b/csci4061/091420_breakout/include/main.h new file mode 100644 index 0000000..6f7f6fe --- /dev/null +++ b/csci4061/091420_breakout/include/main.h @@ -0,0 +1,7 @@ +#ifndef INC_091420_BREAKOUT_MAIN_H +#define INC_091420_BREAKOUT_MAIN_H + +#include <stdio.h> +static const int maxLen = 100; + +#endif //INC_091420_BREAKOUT_MAIN_H diff --git a/csci4061/091420_breakout/include/major.h b/csci4061/091420_breakout/include/major.h new file mode 100644 index 0000000..e649ea6 --- /dev/null +++ b/csci4061/091420_breakout/include/major.h @@ -0,0 +1,6 @@ +#ifndef INC_091420_BREAKOUT_MAJOR_H +#define INC_091420_BREAKOUT_MAJOR_H + +void getMajor(char * major); + +#endif //INC_091420_BREAKOUT_MAJOR_H diff --git a/csci4061/091420_breakout/include/name.h b/csci4061/091420_breakout/include/name.h new file mode 100644 index 0000000..2bb96aa --- /dev/null +++ b/csci4061/091420_breakout/include/name.h @@ -0,0 +1,6 @@ +#ifndef INC_091420_BREAKOUT_NAME_H +#define INC_091420_BREAKOUT_NAME_H + +void getName(char * name); + +#endif //INC_091420_BREAKOUT_NAME_H diff --git a/csci4061/091420_breakout/main.c b/csci4061/091420_breakout/main.c new file mode 100644 index 0000000..aeb989e --- /dev/null +++ b/csci4061/091420_breakout/main.c @@ -0,0 +1,36 @@ +/* + * Recitation section number: + * Breakout Group Number: + * Member name (email address): + * Member name (email address): + * Member name (email address): + * Member name (email address): + * Member name (email address): + * Member name (email address): + * Member name (email address): + */ + +#include "include/main.h" +#include "include/name.h" +#include "include/address.h" +#include "include/major.h" + +int main() { + + char name[maxLen]; + char address[maxLen]; + char major[maxLen]; + + printf("Hi there! \nTell me more about you.\n"); + getName(name); + getAddress(address); + getMajor(major); + + printf("Thanks for your information!\n"); + printf("Your name: %s", name); + printf("Your address: %s", address); + printf("Your major: %s", major); + printf("Bye!\n"); + + return 0; +}
\ No newline at end of file diff --git a/csci4061/091420_breakout/major.c b/csci4061/091420_breakout/major.c new file mode 100644 index 0000000..cfc9cef --- /dev/null +++ b/csci4061/091420_breakout/major.c @@ -0,0 +1,15 @@ +#include "include/main.h" +#include "include/major.h" + +/* + * Write getMajor function + * void getMajor(char * major); + * Prints a prompt "Enter your major: " to get an major, + * Reads in the user's major + */ + +void getMajor(char * major) { + printf("Enter your major: "); + fgets(major, maxLen, stdin); + fflush(stdin); +}
\ No newline at end of file diff --git a/csci4061/091420_breakout/name.c b/csci4061/091420_breakout/name.c new file mode 100644 index 0000000..97c2a9d --- /dev/null +++ b/csci4061/091420_breakout/name.c @@ -0,0 +1,8 @@ +#include "include/main.h" +#include "include/name.h" + +void getName(char * name) { + printf("Enter your name: "); + fgets(name, maxLen, stdin); + fflush(stdin); +}
\ No newline at end of file diff --git a/csci4061/092120_breakout/main.c b/csci4061/092120_breakout/main.c new file mode 100644 index 0000000..8d8e08d --- /dev/null +++ b/csci4061/092120_breakout/main.c @@ -0,0 +1,50 @@ +/* +CSci 4061 - Recitation 2 - 21st Sept 2020 +Breakout 1 +An iditoic loner's lone attempt +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <errno.h> + +int main(int argc, char **argv) { + // take 'n' from the input and convert to integer + // hint: see cmd slide + if (argc < 2) { + printf("USAGE: ./main int\n"); + return -1; + } + int n = strtol(argv[1], NULL, 10); + // for i in n + // create child process + // print pid //hint: check week 2 slides + // call execl to print 'hello there' using 'echo' //hint: check recitation slide + pid_t pid; + for (int i=0; i<n; i++) { + pid = fork(); + if (pid == 0) { + + } else { +printf("%d\n", getpid()); + execl("/bin/echo", "/bin/echo", "hello", "there", NULL); + } + } + // parent waits for all child processes to terminate + for (int i=0; i<n; i++) { + wait(NULL); + } + // parent create child process + // call execv on 'ptime' executable // hint: similar to 'echo' usage in slide + pid = fork(); + if (pid != 0) { + char *args[] = {"./ptime", NULL}; + execv(*args, args); + } + // parent waits for child to complete + waitpid(pid, NULL, 0); + return 0; +}
\ No newline at end of file diff --git a/csci4061/092820_breakout/main.c b/csci4061/092820_breakout/main.c new file mode 100644 index 0000000..0757a06 --- /dev/null +++ b/csci4061/092820_breakout/main.c @@ -0,0 +1,36 @@ +/* +* Recitation Section Number: +* Breakout Number: +* Member Name +* Member Name +* Member Name +* Member Name +*/ + + +#include<stdio.h> +#include <stdlib.h> // For exit() + + +int main() { + + //STEP 1 + //Your code to open the file copy the file and close the file + FILE* fread = fopen("sample.txt", "r"); + FILE* fwrite = fopen("sample_copy.txt", "w"); + int bufsize = 30; + char str[bufsize]; + fgets(str, bufsize, fread); + fputs(str, fwrite); + + //STEP 2 + //Your code to read the text and add the numbers + char* text = "sample text 2 4 5"; + int a=0, b=0, c=0; + char* waste1; char* waste2; + sscanf(text, "%s %s %d %d %d", &waste1, &waste2, &a, &b, &c); + printf("%d\n", a+b+c); + + return 0; + +}
\ No newline at end of file diff --git a/csci4061/092820_breakout/sample.txt b/csci4061/092820_breakout/sample.txt new file mode 100644 index 0000000..e568c7b --- /dev/null +++ b/csci4061/092820_breakout/sample.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxya
\ No newline at end of file diff --git a/csci4061/092820_breakout/sample_copy.txt b/csci4061/092820_breakout/sample_copy.txt new file mode 100644 index 0000000..e568c7b --- /dev/null +++ b/csci4061/092820_breakout/sample_copy.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxya
\ No newline at end of file diff --git a/csci4061/100520_breakout/exercise.c b/csci4061/100520_breakout/exercise.c new file mode 100644 index 0000000..6d560d5 --- /dev/null +++ b/csci4061/100520_breakout/exercise.c @@ -0,0 +1,47 @@ +/* +* Recitation Section Number: +* Breakout Number: +* Member Name +* Member Name +* Member Name +* Member Name +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> +#define BUFSIZE 20 +#define PERM 0644 +int main(int argc, char *argv[]) { + int infile; char buffer[BUFSIZE]; + // create/open a file named write.txt + infile = open("write.txt", O_CREAT | O_RDWR | O_TRUNC, PERM); + // write hello world to write.txtS + write(infile, "hello world\n", 12); + // spawn a child process and use this child process to reroute stdout to write.txt + pid_t pid = fork(); + if (!pid) { + dup2(infile, 1); + } + // print pid + printf("Process %d\n", pid); + + // use the child process spawned previously to read the contents from write.txt, restore output to stdout + // and then prints the contents of write.txt to stdout. + if (!pid) { + read(infile, buffer, 20); + int stdout = open("/dev/tty", O_WRONLY); + dup2(1, stdout); + printf("%s\n", buffer); + return 0; + } + // print pid + printf("%d\n", pid); + // close the file write.txt + close(infile); + return 0; +}
\ No newline at end of file diff --git a/csci4061/101220_breakout/exercise.c b/csci4061/101220_breakout/exercise.c new file mode 100644 index 0000000..b6e863e --- /dev/null +++ b/csci4061/101220_breakout/exercise.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <time.h> + +int numOfEntry(char *path) +{ + int count = 0; + DIR *dir = opendir(path); + struct dirent *entry; + + while ((entry = readdir(dir)) != NULL) + { + + if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) + continue; + + count++; + } + + closedir(dir); + + return count; +} + +int main(int argc, char **argv) +{ + + if (argc < 2) + { + printf("Pass the path as an argument to the program"); + exit(1); + } + + char *path = argv[1]; + + DIR *dir = opendir(path); + if (dir == NULL) + { + printf("The path passed is invalid"); + return -1; + } + struct dirent *entry; + + while ((entry = readdir(dir)) != NULL) + { + + if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) + continue; + + if (entry->d_type == DT_DIR) + { + char next[strlen(path) + strlen(entry->d_name) + 2]; + next[0] = '\0'; + strcat(next, path); + strcat(next, "/"); + strcat(next, entry->d_name); + printf("Directory: %s\n\tEntries: %d\n", entry->d_name, numOfEntry(next)); + } + else if (entry->d_type == DT_REG) + { + struct stat *buf = (struct stat *)malloc(sizeof(struct stat)); + stat(entry->d_name, buf); + printf("Regular File: %s\n\tOwner: %d\n\tSize: %f\n\tInode: %llu\n", + entry->d_name, buf->st_uid, (double)buf->st_size, buf->st_ino); + free(buf); + } + else + { + printf("File: %s\n\tType:%hhu\n", entry->d_name, entry->d_type); + } + } + + closedir(dir); + return 0; +}
\ No newline at end of file diff --git a/csci4061/101920_breakout/pipe_template.c b/csci4061/101920_breakout/pipe_template.c new file mode 100644 index 0000000..2c37941 --- /dev/null +++ b/csci4061/101920_breakout/pipe_template.c @@ -0,0 +1,65 @@ +// ----------------------------------------------------------------------------- +// Pipes +// +// Write a program that creates a child process and pipes the string +// "hello child" to the child to be printed. The parent should wait for the +// child to terminate +// +// Template: Implement the TODOs +// +// Expected output: +// Parent Sending: hello child +// Child Received: hello child +// ----------------------------------------------------------------------------- + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/wait.h> +#include <unistd.h> + +int main(void) { + // Open pipe + int ends[2]; + pipe(ends); + + char string_to_send[] = "hello child"; + int bytes_to_send_recv = strlen(string_to_send) + 1; + + // Create child process + pid_t pid = fork(); + if (pid < 0) { + // Error + fprintf(stderr, "ERROR: Failed to fork\n"); + } + else if (pid > 0) { + // Parent + printf("Parent Sending: %s\n", string_to_send); + + // Parent doesn't need read file descriptor + close(ends[0]); + // Write the string to the pipe + write(ends[1], string_to_send, bytes_to_send_recv); + // Done writing + close(ends[1]); + // Wait for child to terminate + wait(NULL); + } + else { + // Child + + char *recv_buffer = malloc(bytes_to_send_recv); + + // Child doesn't need write file descriptor + close(ends[1]); + // Read the string from the pipe + read(ends[0], recv_buffer, bytes_to_send_recv); + // Done reading + close(ends[0]); + // Print result + printf("Child Received: %s\n", recv_buffer); + free(recv_buffer); + } + + return 0; +} diff --git a/csci4061/102620_breakout/msg_queue_template.c b/csci4061/102620_breakout/msg_queue_template.c new file mode 100644 index 0000000..af026f3 --- /dev/null +++ b/csci4061/102620_breakout/msg_queue_template.c @@ -0,0 +1,108 @@ +/* +* Recitation Section Number: 02 +* Breakout Number: 05 +* Graden Hill (hill1582) +* Gustav Baumgart (baumg260) +* Matt Strapp (strap012) +* Skylan Recnana (recan001) +*/ + +#include <stdio.h> +#include <sys/ipc.h> +#include <sys/msg.h> +#include <sys/wait.h> +#include <zconf.h> +#include <string.h> +#include <stdlib.h> + +#define PERM 0666 +#define MSGSIZE 100 +#define NCHILD 3 + +// structure for message queue +typedef struct msg_buffer { + long mtype; + char mtext[MSGSIZE]; +} message; + +int main(void) { + key_t key; + int msgid; + pid_t pid1, pid2; + message msg; + + // generate unique key + key = ftok("recitation", 4061); + + // creates a message queue + msgid = msgget(key, PERM | IPC_CREAT); + + + // sender child process + pid1 = fork(); + if (pid1 == 0) { + for (int i = 0; i < NCHILD; i++) { + msg.mtype = 111; + memset(msg.mtext, '\0', MSGSIZE); + sprintf(msg.mtext, "Hello child %d", i); + // send message to other child processes + msgsnd(msgid, &msg, MSGSIZE, 0); + + + // display the message + printf("[%d] Data sent is : %s \n", i, msg.mtext); + } + + for (int i = 0; i < NCHILD; i++) { + msg.mtype = 222; + memset(msg.mtext, '\0', MSGSIZE); + sprintf(msg.mtext, "Message %d", i); + // send message to other child processes + msgsnd(msgid, &msg, MSGSIZE, 0); + + // display the message + printf("[%d] Data sent is : %s \n", i, msg.mtext); + } + + for (int i = 0; i < NCHILD; i++) { + msg.mtype = 333; + memset(msg.mtext, '\0', MSGSIZE); + sprintf(msg.mtext, "Bye %d", i); + // send message to other child processes + msgsnd(msgid, &msg, MSGSIZE, 0); + + // display the message + printf("[%d] Data sent is : %s \n", i, msg.mtext); + } + + exit(0); + } else if (pid1 < 0) { + printf("fork1 error\n"); + return -1; + } + + // receiver child processes + for (int j = 0; j < NCHILD; j++) { + if ((pid2 = fork()) == 0) { + // receive message + msgrcv(msgid, &msg, MSGSIZE, 222, 0); + + + // display the message + printf("[%d] Data received is : %s \n", j, msg.mtext); + + exit(0); + } else if (pid2 < 0) { + printf("fork2 error\n"); + return -1; + } + } + + while (wait(NULL) > 0); + + // to destroy the message queue + msgctl(msgid, IPC_RMID, NULL); + + + return 0; +}
\ No newline at end of file diff --git a/csci4061/110920_breakout/chap8/Makefile b/csci4061/110920_breakout/chap8/Makefile new file mode 100644 index 0000000..bd92bec --- /dev/null +++ b/csci4061/110920_breakout/chap8/Makefile @@ -0,0 +1,12 @@ +pgm81: pgm_8_1.c + gcc pgm_8_1.c -lm -o out + ./out 5 + +pgm85: pgm_8_5.c + gcc pgm_8_5.c -lm -o out + ./out + +.PHONY: clean + +clean: + rm out diff --git a/csci4061/110920_breakout/chap8/pgm_8_1.c b/csci4061/110920_breakout/chap8/pgm_8_1.c new file mode 100644 index 0000000..a979bb6 --- /dev/null +++ b/csci4061/110920_breakout/chap8/pgm_8_1.c @@ -0,0 +1,42 @@ +// Program 8.1 + +#include <math.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +int main(int argc, char *argv[]) { + int i; + sigset_t intmask; + int repeatfactor; + double y = 0.0; + + if (argc != 2) { + fprintf(stderr, "Usage: %s repeatfactor\n", argv[0]); + return 1; + } + repeatfactor = atoi(argv[1]); + if ((sigemptyset(&intmask) == -1) || (sigaddset(&intmask, SIGINT) == -1)){ + perror("Failed to initialize the signal mask"); + return 1; + } + for ( ; ; ) { + if (sigprocmask(SIG_BLOCK, &intmask, NULL) == -1) + break; + fprintf(stderr, "SIGINT signal blocked\n"); + for (i = 0; i < repeatfactor; i++) + y += sin((double)i); + fprintf(stderr, "Blocked calculation is finished, y = %f\n", y); + sleep(3); + if (sigprocmask(SIG_UNBLOCK, &intmask, NULL) == -1) + break; + fprintf(stderr, "SIGINT signal unblocked\n"); + for (i = 0; i < repeatfactor; i++) + y += sin((double)i); + fprintf(stderr, "Unblocked calculation is finished, y=%f\n", y); + sleep(3); + } + perror("Failed to change signal mask"); + return 1; +}
\ No newline at end of file diff --git a/csci4061/110920_breakout/chap8/pgm_8_5.c b/csci4061/110920_breakout/chap8/pgm_8_5.c new file mode 100644 index 0000000..b20cdfa --- /dev/null +++ b/csci4061/110920_breakout/chap8/pgm_8_5.c @@ -0,0 +1,43 @@ +// Program 8.5 + +#include <math.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> + +static volatile sig_atomic_t doneflag = 0; + +/* ARGSUSED */ +static void setdoneflag(int signo) { + doneflag = 1; +} + +int main (void) { + struct sigaction act; + int count = 0; + double sum = 0; + double x; + + act.sa_handler = setdoneflag; /* set up signal handler */ + act.sa_flags = 0; + if ((sigemptyset(&act.sa_mask) == -1) || + (sigaction(SIGINT, &act, NULL) == -1)) { + perror("Failed to set SIGINT handler"); + return 1; + } + + while (!doneflag) { + x = (rand() + 0.5)/(RAND_MAX + 1.0); + sum += sin(x); + count++; + printf("Count is %d and average is %f\n", count, sum/count); + sleep(3); + } + + printf("Program terminating ...\n"); + if (count == 0) + printf("No values calculated yet\n"); + else + printf("Count is %d and average is %f\n", count, sum/count); + return 0; +}
\ No newline at end of file diff --git a/csci4061/110920_breakout/sol1.c b/csci4061/110920_breakout/sol1.c new file mode 100644 index 0000000..d33b0df --- /dev/null +++ b/csci4061/110920_breakout/sol1.c @@ -0,0 +1,37 @@ +/* +* Recitation Section Number: 9 +* Breakout Number: 6 +* Audrey Hebert (heber169) +* Qiyu Tian (tian0068) +* Mouhari Mouhamed (mouha003) +* Matt Strapp (strap012) +*/ + +#include <stdio.h> +#include <signal.h> +#include <unistd.h> +#include <time.h> + +void infinitePrint() { + + static int count = 0; + printf("%ld: Count = %d\n", time(NULL), ++count); + sleep(1); +} + +int main() { + + // ------------------sol1.c----------------- + // initialize new sigset - sigset_t, sigemptyset + sigset_t sigset; + sigemptyset(&sigset); + // add SIGINT to sigset - sigaddset + sigaddset(&sigset, SIGINT); + // block SIGINT - sigprocmask + sigprocmask(SIG_BLOCK, &sigset, NULL); + // Do not modify the while loop and infinitePrint() + // ----------------------------------------- + + /* Print infinitely. */ + while (1) infinitePrint(); +}
\ No newline at end of file diff --git a/csci4061/110920_breakout/sol2.c b/csci4061/110920_breakout/sol2.c new file mode 100644 index 0000000..04dad3e --- /dev/null +++ b/csci4061/110920_breakout/sol2.c @@ -0,0 +1,52 @@ +/* +* Recitation Section Number: 9 +* Breakout Number: 6 +* Audrey Hebert (heber169) +* Qiyu Tian (tian0068) +* Mouhari Mouhamed (mouha003) +* Matt Strapp (strap012) +*/ + +#include <stdio.h> +#include <signal.h> +#include <unistd.h> +#include <time.h> + +// Remember the usage of sig_atomic_t in the recitation example +static volatile sig_atomic_t doneflag = 0; + +static void setdoneflag(int signo) { + doneflag = doneflag ^ 1; +} + +void infinitePrint() { + + static int count = 0; + printf("%ld: Count = %d\n", time(NULL), ++count); + sleep(1); +} + +int main() { + + // ------------------sol2.c----------------- + // setup signal handler - sigaction struct, sigemptyset, + struct sigaction act; + sigset_t sigset; + // specify action associated with SIGNT - sigaction() + act.sa_handler = setdoneflag; /* set up signal handler */ + act.sa_flags = 0; + if ((sigemptyset(&act.sa_mask) == -1) || + (sigaction(SIGINT, &act, NULL) == -1)) + { + perror("Failed to set SIGINT handler"); + return 1; + } + // You are free to modify the while loop but not the infinitePrint() + // ----------------------------------------- + + /* Print infinitely. */ + while (1) { + if (!doneflag) + infinitePrint(); + } +}
\ No newline at end of file diff --git a/csci4061/110920_breakout/zip/sol1.c b/csci4061/110920_breakout/zip/sol1.c new file mode 100644 index 0000000..ca60c50 --- /dev/null +++ b/csci4061/110920_breakout/zip/sol1.c @@ -0,0 +1,37 @@ +/* +* Recitation Section Number: 9 +* Breakout Number: +* Audrey Hebert (heber169) +* Qiyu Tian (tian0068) +* Mouhari Mouhamed (mouha003) +* Matt Strapp (strap012) +*/ + +#include <stdio.h> +#include <signal.h> +#include <unistd.h> +#include <time.h> + +void infinitePrint() { + + static int count = 0; + printf("%ld: Count = %d\n", time(NULL), ++count); + sleep(1); +} + +int main() { + + // ------------------sol1.c----------------- + // initialize new sigset - sigset_t, sigemptyset + sigset_t sigset; + sigemptyset(&sigset); + // add SIGINT to sigset - sigaddset + sigaddset(&sigset, SIGINT); + // block SIGINT - sigprocmask + sigprocmask(SIG_BLOCK, &sigset, NULL); + // Do not modify the while loop and infinitePrint() + // ----------------------------------------- + + /* Print infinitely. */ + while (1) infinitePrint(); +}
\ No newline at end of file diff --git a/csci4061/110920_breakout/zip/sol2.c b/csci4061/110920_breakout/zip/sol2.c new file mode 100644 index 0000000..04dad3e --- /dev/null +++ b/csci4061/110920_breakout/zip/sol2.c @@ -0,0 +1,52 @@ +/* +* Recitation Section Number: 9 +* Breakout Number: 6 +* Audrey Hebert (heber169) +* Qiyu Tian (tian0068) +* Mouhari Mouhamed (mouha003) +* Matt Strapp (strap012) +*/ + +#include <stdio.h> +#include <signal.h> +#include <unistd.h> +#include <time.h> + +// Remember the usage of sig_atomic_t in the recitation example +static volatile sig_atomic_t doneflag = 0; + +static void setdoneflag(int signo) { + doneflag = doneflag ^ 1; +} + +void infinitePrint() { + + static int count = 0; + printf("%ld: Count = %d\n", time(NULL), ++count); + sleep(1); +} + +int main() { + + // ------------------sol2.c----------------- + // setup signal handler - sigaction struct, sigemptyset, + struct sigaction act; + sigset_t sigset; + // specify action associated with SIGNT - sigaction() + act.sa_handler = setdoneflag; /* set up signal handler */ + act.sa_flags = 0; + if ((sigemptyset(&act.sa_mask) == -1) || + (sigaction(SIGINT, &act, NULL) == -1)) + { + perror("Failed to set SIGINT handler"); + return 1; + } + // You are free to modify the while loop but not the infinitePrint() + // ----------------------------------------- + + /* Print infinitely. */ + while (1) { + if (!doneflag) + infinitePrint(); + } +}
\ No newline at end of file diff --git a/csci4061/111620_breakout/program.c b/csci4061/111620_breakout/program.c new file mode 100644 index 0000000..52f67a1 --- /dev/null +++ b/csci4061/111620_breakout/program.c @@ -0,0 +1,155 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <pthread.h> + +#define MATSIZE 5000 + +int mat1[MATSIZE][MATSIZE]; +int mat2[MATSIZE][MATSIZE]; + + +typedef struct argstruct { + int x_start; + int x_end; + int y_start; + int y_end; + int** res_mat; +} args_t; + +int getX_start(int i) { + return (i % 2) * MATSIZE / 2; +} +int getY_start(int i) { + return ((i < 2) ? 0 : 1) * MATSIZE / 2; +} + + +//TODO: Complete this function (to be accessed by a thread) which will +// perform matrix addition on a portion of a matrix, dictated by +// the input parameter 'args' +void * partial_matrix_add(void * args) { + int **dest; + args_t *input = (struct argstruct *) args; // params can be accessed using input pointer + dest = input->res_mat; //can be accessed as dest[][] now + + // Todo: Your code goes here (for reference checkout the pseudo code in the slides) + for (int i=input->x_start; i<input->x_end; i++) { + for (int j=input->y_start; j<input->y_end; j++) { + dest[i][j] = mat1[i][j]*mat2[i][j]; + } + } + return NULL; +} + +int main() { + // to store mulitple dispatcher threads + pthread_t m_threads[4]; + + // to store single default thread + pthread_t s_thread; + + // variable to pass values to multiple thread multiplication + args_t m_args[4]; + + //variable to pass values to single thread muliplication + args_t s_args; + + /** + * initializing matrices for sinlge and multiple matrix multiplication + */ + int i, j, k, c; + struct timeval t_multi_start, t_multi_end, t_single_start, t_single_end; + int ** res_mat_multi = malloc(MATSIZE * sizeof(int *)); + int ** res_mat_single = malloc(MATSIZE * sizeof(int *)); + for(i = 0; i < MATSIZE; i++) { + res_mat_multi[i] = malloc(MATSIZE * sizeof(int)); + res_mat_single[i] = malloc(MATSIZE * sizeof(int)); + } + + //Populate base matrices with random integers + //Initialize result matrices with -1 + for(j = 0; j < MATSIZE; j++) { + for(k = 0; k < MATSIZE; k++) { + mat1[j][k] = rand() % 1000 + 1; + mat2[j][k] = rand() % 1000 + 1; + res_mat_multi[j][k] = -1; + res_mat_single[j][k] = -1; + } + } +//Version 1 ************************************************************************** + //Measure time for multiple thread addition + gettimeofday(&t_multi_start, NULL); + + //Todo: create attribute for detached threads + //Create mulitple threads to populate res_mat_multi with the result + // of performing matrix addition mat1 + mat2 + for(i = 0; i < 4; i++) { + + int x_start = getX_start(i); + int y_start = getY_start(i); + int x_end = x_start + MATSIZE/2; + int y_end = y_start + MATSIZE/2; + m_args[i].res_mat = res_mat_multi; + //your code goes here + //Todo:Create m_agrs using x_start, x_end, y_start, y_end, and create detached threads + m_args[i].x_start = x_start; + m_args[i].x_end = x_end; + m_args[i].y_start = y_start; + m_args[i].y_end = y_end; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_create(&m_threads[i], &attr, partial_matrix_add, (void *) &m_args[i]); + } + + gettimeofday(&t_multi_end, NULL); +//Version 2 ************************************************************************* + + //Measure time for single thread addition + gettimeofday(&t_single_start, NULL); + + // Create single thread to populate res_mat_multi with the result + // of performing matrix addition mat1 + mat2 + + int x_start = 0; + int x_end = MATSIZE; + int y_start = 0; + int y_end = MATSIZE; + s_args.res_mat = res_mat_single; + //your code goes here + //Todo: Assign values to s_args using x_start, y_start etc. + s_args.x_start = x_start; + s_args.y_start = y_start; + s_args.x_end = x_end; + s_args.y_end = y_end; + //Todo:Create thread + pthread_create(&s_thread, NULL, partial_matrix_add, (void *) &s_args); + //Todo:join thread + pthread_join(s_thread, NULL); + gettimeofday(&t_single_end, NULL); + +// ********************************************************************************** + //Don't change anything from here + //Test to ensure that both methods produce the same result + c = 0; + for(j = 0; j < MATSIZE; j++) { + for(k = 0; k < MATSIZE; k++) { + if(res_mat_multi[j][k] == res_mat_single[j][k] && res_mat_multi[j][k] != -1) { + c++; + } + } + } + printf("Verification: %d out of %d entries matched.\n", c, MATSIZE * MATSIZE); + //Display time for each version + double time_taken; + time_taken = (t_multi_end.tv_sec - t_multi_start.tv_sec) * 1e6; + time_taken = (time_taken + (t_multi_end.tv_usec - t_multi_start.tv_usec)) * 1e-6; + printf("Time for multiple threads: %f seconds\n", time_taken); + + time_taken = (t_single_end.tv_sec - t_single_start.tv_sec) * 1e6; + time_taken = (time_taken + (t_single_end.tv_usec - t_single_start.tv_usec)) * 1e-6; + printf("Time for single thread: %f seconds\n", time_taken); + return 0; +} diff --git a/csci4061/112320_breakout/sol-condition_variables.c b/csci4061/112320_breakout/sol-condition_variables.c new file mode 100644 index 0000000..86cafb1 --- /dev/null +++ b/csci4061/112320_breakout/sol-condition_variables.c @@ -0,0 +1,128 @@ +#define NUM_ARGS 0 + +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <time.h> +#include <pthread.h> +#include <semaphore.h> +#include <sys/time.h> + +long condTotal = 0; + +struct queue { + + int vals[100]; + int index; //indicates the next available spot +}; + +struct condQueue { + + struct queue* q; + pthread_cond_t* cond; + pthread_mutex_t* mutex; +}; + + + +void insert(struct queue* q, int val) { + + q->vals[q->index] = val; + ++q->index; +} + +int delete(struct queue* q) { + + --q->index; + int val = q->vals[q->index]; + return val; +} + +// TODO: Insert code to use a condition variable. +void *condProducer(void* arg) { + + // Random delay. DO NOT REMOVE! + usleep(rand() % 1000); + + struct condQueue* cq = (struct condQueue*) arg; + + pthread_mutex_lock(cq->mutex); + + // Counter. + static int in = 0; + ++in; + + // Add an element to the queue. + insert(cq->q, in); + + pthread_cond_signal(cq->cond); //to send signal to any waiting consumer + pthread_mutex_unlock(cq->mutex); +} + +// TODO: Insert code to use a condition variable. +void *condConsumer(void* arg) { + + // Random delay. DO NOT REMOVE! + usleep(rand() % 1000); + + struct condQueue* cq = (struct condQueue*) arg; + + pthread_mutex_lock(cq->mutex); + while (cq->q->index < 1) pthread_cond_wait(cq->cond, cq->mutex);//wait if buffer is empty + + // Remove an element from the queue. + condTotal += delete(cq->q); + + pthread_mutex_unlock(cq->mutex); +} + +int main(int argc, char** argv) { + + if (argc != NUM_ARGS + 1) { + + printf("Wrong number of args, expected %d, given %d\n", NUM_ARGS, argc - 1); + exit(1); + } + + // Seed the random generator. + srand(time(NULL)); + + // Create threads. + pthread_t condPool[100]; + + struct timeval start; + gettimeofday(&start, NULL); + + // Create the cond variable controlled task queue. + struct condQueue* cq = (struct condQueue*) malloc(sizeof(struct condQueue)); + cq->q = (struct queue*) malloc(sizeof(struct queue)); + cq->q->index=0; + + // Allocate memory and initialize condition variable and mutex + cq->cond = (pthread_cond_t*) malloc(sizeof(pthread_cond_t)); + cq->mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); + pthread_cond_init(cq->cond, NULL); + pthread_mutex_init(cq->mutex, NULL); + + // Launch them. + for (int i=0; i < 50; ++i) { + + pthread_create(&condPool[i], NULL, condProducer, (void*) cq); //start 50 producer threads + pthread_create(&condPool[50 + i], NULL, condConsumer, (void*) cq); //start 50 consumer threads + } + + for (int i=0; i < 100; ++i) pthread_join(condPool[i], NULL); //wait for all the threads to be finished + + struct timeval end; + gettimeofday(&end, NULL); + + printf("Cond Test: \nTotal of buffer = %ld\n", condTotal); + printf("Time (in us) to run = %ld\n\n", ((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec)); + + } diff --git a/csci4061/112320_breakout/sol-semaphore.c b/csci4061/112320_breakout/sol-semaphore.c new file mode 100644 index 0000000..4af3a5b --- /dev/null +++ b/csci4061/112320_breakout/sol-semaphore.c @@ -0,0 +1,128 @@ +#define NUM_ARGS 0 + +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <time.h> +#include <pthread.h> +#include <semaphore.h> +#include<sys/time.h> + +long semTotal = 0; + +struct buffer { + + int vals[100]; + int index; +}; + +struct semBuffer { + + struct buffer* q; + sem_t* psem; + sem_t* csem; + pthread_mutex_t* mutex; +}; + +void insert(struct buffer* q, int val) { + + q->vals[q->index] = val; + ++q->index; +} + +int delete(struct buffer* q) { + + --q->index; + int val = q->vals[q->index]; + return val; +} + +// TODO: Insert code to use a semaphore. +void* semProducer(void* arg) { + + // Random delay. DO NOT REMOVE! + usleep(rand() % 1000); + + struct semBuffer* sq = (struct semBuffer*) arg; + sem_wait(sq->psem); + pthread_mutex_lock(sq->mutex); + + static int in = 0; + ++in; + // Add an element to the queue. + insert(sq->q, in); + + pthread_mutex_unlock(sq->mutex); + sem_post(sq->csem); + +} + +// TODO: Insert code to use a semaphore. +void* semConsumer(void* arg) { + + // Random delay. DO NOT REMOVE! + usleep(rand() % 1000); + + struct semBuffer* sq = (struct semBuffer*) arg; + + sem_wait(sq->csem); + pthread_mutex_lock(sq->mutex); + + // Reove an element from the queue. + semTotal += delete(sq->q); + + pthread_mutex_unlock(sq->mutex); + sem_post(sq->psem); +} + +int main(int argc, char** argv) { + + if (argc != NUM_ARGS + 1) { + + printf("Wrong number of args, expected %d, given %d\n", NUM_ARGS, argc - 1); + exit(1); + } + + // Seed the random generator. + srand(time(NULL)); + + // Create threads. + pthread_t semPool[100]; + + struct timeval start; + gettimeofday(&start, NULL); + + // Create task queue. + struct semBuffer* sq = (struct semBuffer*) malloc(sizeof(struct semBuffer)); + + sq->q = (struct buffer*) malloc(sizeof(struct buffer)); + sq->q->index=0; + sq->psem = (sem_t*) malloc(sizeof(sem_t)); + sq->csem = (sem_t*) malloc(sizeof(sem_t)); + sq->mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); + + // TODO: Initilaize semaphores + sem_init(sq->psem, 0, 50); + sem_init(sq->csem, 0, 0); + + pthread_mutex_init(sq->mutex, NULL); + + for (int i=0; i < 50; ++i) { + + pthread_create(&semPool[i], NULL, semProducer, (void*) sq); + pthread_create(&semPool[50 + i], NULL, semConsumer, (void*) sq); + } + + for (int i=0; i < 100; ++i) pthread_join(semPool[i], NULL); + + struct timeval end; + gettimeofday(&end, NULL); + printf("Sem Test: \nTotal of buffer = %ld\n", semTotal); + printf("Time (in us) to complete = %ld\n", ((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec)); +} |