diff --git a/CMakeLists.txt b/CMakeLists.txt index 596caf2..5b57597 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,11 @@ cmake_minimum_required(VERSION 3.22) project(C_Dynamic_List C) set(CMAKE_C_STANDARD 11) +set(CMAKE_C_FLAGS "-pthread") include_directories(.) include_directories(src/test) add_executable(C_Dynamic_List src/list.c src/test/test.c src/list.h) + +add_executable(1 "Further pthread practice Qs/1.c") \ No newline at end of file diff --git a/Further pthread practice Qs/1.c b/Further pthread practice Qs/1.c new file mode 100644 index 0000000..2abb934 --- /dev/null +++ b/Further pthread practice Qs/1.c @@ -0,0 +1,38 @@ +#include +#include +#include + +#define NUM_THREADS 20 + +void *fizzbuzz(void *arg) { + int *number = (int *) arg; + if (*number % 3 == 0) { + printf("%d -> %s\n", *number, "fizz"); + } else if (*number % 5 == 0) { + printf("%d -> %s\n", *number, "buzz"); + } else { + printf("%d -> %s\n", *number, "no div"); + } + + pthread_exit(NULL); +} + +int main() { + + pthread_t threads[20]; + int nums[20]; + + int i; + for (i = 0; i < NUM_THREADS; i++) { + nums[i] = i + 1; + pthread_create(&threads[i], NULL, fizzbuzz, &nums[i]); + } + + for (i = 0; i < NUM_THREADS; i++) { + char *returnedStr; + pthread_join(threads[i], (void **) &returnedStr); + } + + pthread_exit(NULL); + +} diff --git a/Further pthread practice Qs/3.c b/Further pthread practice Qs/3.c new file mode 100644 index 0000000..527a922 --- /dev/null +++ b/Further pthread practice Qs/3.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include // gives us sleep(). Replace with windows.h for compiling on windows! + +#define NUM_THREADS 20 + +typedef struct _threadinfo { + bool * keepRunning; + int * total; +} threadinfo_t; + +void * getInput(void * arg) { + threadinfo_t * args = (threadinfo_t *) arg; + + int currInput; + printf("Input as much as you can --- you have 10 seconds!\n"); + while (*args->keepRunning) { + printf("Add another number! : "); + scanf("%d", &currInput); + *args->total += currInput; + } + pthread_exit(NULL); +} + +int main() { + + pthread_t thread; + bool keepRunning = true; + int total = 0; + threadinfo_t args = {&keepRunning, &total}; + + pthread_create(&thread, NULL, getInput, &args); + + sleep(10); // sleep for 10 secs + keepRunning = false; + pthread_join(thread, NULL); + printf("\n\nYou managed to enter a total of: %d!\n", total); + + pthread_exit(NULL); + +} + diff --git a/Further pthread practice Qs/5.c b/Further pthread practice Qs/5.c new file mode 100644 index 0000000..f7cdef3 --- /dev/null +++ b/Further pthread practice Qs/5.c @@ -0,0 +1,8 @@ +#include + +// the rest is up to you! Implement a program which starts _two_ pthreads. +// One reads input from the user, and adds numbers to a total. +// the other prints the current total value every 3 seconds. +// After 30 seconds, the main thread sets a bool to false that signals +// to both threads that they should join, just like in 3.c. +// You can use sleep(int seconds) from unistd.h to make your threads sleep. \ No newline at end of file diff --git a/Further pthread practice Qs/further_pthread_practice.pdf b/Further pthread practice Qs/further_pthread_practice.pdf new file mode 100644 index 0000000..2962c7c Binary files /dev/null and b/Further pthread practice Qs/further_pthread_practice.pdf differ diff --git a/src/test/test.c b/src/test/test.c index fa043f1..1195c9b 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1,4 +1,5 @@ #include "src/list.h" +#include "pthread.h" #include "string.h" #include "assert.h" @@ -63,10 +64,35 @@ void shouldReturnSortedList() { printSuccess(__func__); } +typedef struct args { + List *list; + int elemToInsert; +} Arguments; + +void *append(void *arg) { + Arguments *args = (Arguments *) arg; + List *list = args->list; + List_append(list, args->elemToInsert); + pthread_exit(NULL); +} + int main() { - shouldReturnListLengthZero(); - shouldReturnListLengthTwo(); - shouldReturnSortedList(); +// shouldReturnListLengthZero(); +// shouldReturnListLengthTwo(); +// shouldReturnSortedList(); + List *list = newList(); + + pthread_t pthreads[5]; + for (int i = 0; i < 5; i++) { + Arguments args = {list, i}; + pthread_create(&pthreads[i], NULL, append, &args); + } + + for (int i = 0; i < 5; i++) { + List_print(list); + pthread_join(pthreads[i], NULL); + } + return 0; }