diff --git a/aufgabe03/Makefile b/aufgabe03/Makefile old mode 100644 new mode 100755 diff --git a/aufgabe03/aufgabe1.d b/aufgabe03/aufgabe1.d deleted file mode 100644 index 10ae487fdcfe9c513038b6938441ac66d185da5e..0000000000000000000000000000000000000000 --- a/aufgabe03/aufgabe1.d +++ /dev/null @@ -1 +0,0 @@ -aufgabe1.o: aufgabe1.c diff --git a/aufgabe03/aufgabe1.o b/aufgabe03/aufgabe1.o deleted file mode 100644 index 1b6abb05dc605b854d71df24eb7df14a18c00a84..0000000000000000000000000000000000000000 Binary files a/aufgabe03/aufgabe1.o and /dev/null differ diff --git a/aufgabe03/solution b/aufgabe03/solution deleted file mode 100755 index 1ddbc3679988e75bef7531b65b4638646bfefd54..0000000000000000000000000000000000000000 Binary files a/aufgabe03/solution and /dev/null differ diff --git a/aufgabe04/Makefile b/aufgabe04/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..ba8a685199dac2f3670d9a2fd2f14853c50091fc --- /dev/null +++ b/aufgabe04/Makefile @@ -0,0 +1,28 @@ +# This makefile compiles all C files in the same directory and produces a single +# executable named solution +CC = gcc +CFLAGS = -std=c11 -Wall -Wextra -pedantic -O2 -g -pthread # this uses the C11 standard, if you want to use a different standard, change it to -std=c[NUMBER] +CPPFLAGS = -MMD +LD_FLAGS = -lm + +BIN_NAME = solution +SRC_FILES = $(wildcard *.c) +OBJ_FILES = $(SRC_FILES:.c=.o) +DEP_FILES = $(wildcard *.d) + +run: $(BIN_NAME) + ./$(BIN_NAME) + +include $(DEP_FILES) + +$(BIN_NAME): $(OBJ_FILES) + $(CC) $(CPPFLAGS) $(LD_FLAGS) $(OBJ_FILES) -o $@ + +%.o: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ + +clean: + @rm -v *.o *.d + @rm -v ./$(BIN_NAME) + +.PHONY: run clean diff --git a/aufgabe04/aufgabe1.c b/aufgabe04/aufgabe1.c new file mode 100644 index 0000000000000000000000000000000000000000..72277eaea1e6b6fe222594600fa662861be2d618 --- /dev/null +++ b/aufgabe04/aufgabe1.c @@ -0,0 +1,74 @@ +// dining philosophers with unshared chopsticks +#include <stdio.h> +#include <stdlib.h> +#include <pthread.h> +#include <unistd.h> + +#define NUM_THREADS 5 +pthread_mutex_t lock; +int chopsticks[NUM_THREADS]; + +void* Philosopher (void *threadid) +{ + long tid; + tid = (long)threadid; + int _error = 0; + + for (int i= 0; i < 10; i++) { + // thinking + sleep (0.5); + + //wait + + //Beginn kritischer Abschnitt + _error = pthread_mutex_lock(&lock); + + //Warten darauf, dass chopstick frei werden + while (chopsticks[tid] + chopsticks[(tid + 1) % NUM_THREADS] < 2); + + chopsticks[tid] = 0; + chopsticks[(tid + 1) % NUM_THREADS] = 0; + + _error = pthread_mutex_unlock(&lock); + //Ende kritischer Abschnitt + + printf("\n %ld Dining..\n", tid); + sleep(0.5); + printf("\n %ld Finished..\n", tid); + + //Beginn kritischer Abschnitt + _error = pthread_mutex_lock(&lock); + chopsticks[tid] = 1; + chopsticks[(tid + 1) % NUM_THREADS] = 1; + _error = pthread_mutex_unlock(&lock); + //Ende kritischer Abschnitt + } +} + + +int main (void) +{ + pthread_t threads[NUM_THREADS]; + int rc; + long t; + + for(int i=0;i<NUM_THREADS;i++){ + chopsticks[i]=1; + } + + for(t=0; t < NUM_THREADS; t++) { + printf ("In main: creating thread %ld\n", t); + rc = pthread_create (&threads[t], NULL, Philosopher, (void *)t); + if (rc) { + printf ("ERROR; return code from pthread_create () is %d\n", rc); + exit (-1); + } + } + + for(t=0; t < NUM_THREADS; t++) { + pthread_join (threads[t], NULL); + } + + return 0; +} +