Upload Lab Files & Experiment With Threading

main
Hammy 3 years ago
parent 54da2eb09d
commit 08a0ee0f1a

@ -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")

@ -0,0 +1,38 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#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);
}

@ -0,0 +1,44 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h> // 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);
}

@ -0,0 +1,8 @@
#include <unistd.h>
// 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.

@ -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;
}

Loading…
Cancel
Save