Skip to content
Snippets Groups Projects
Commit 7f02a047 authored by vincem95's avatar vincem95
Browse files

dopplung rm

parent 08463bb0
No related branches found
No related tags found
No related merge requests found
File deleted
# This makefile compiles all C files in the same directory and produces a single
# executable named solution
CC = gcc
CFLAGS = -std=gnu17 -Wall -Wextra -pedantic -O2 -g
CPPFLAGS = -MMD
LD_FLAGS = -pthread -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) $(LD_FLAGS) $(OBJ_FILES) -o $@
%.o: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
clean:
@rm -v *.o *.d
@rm -v ./$(BIN_NAME)
.PHONY: run clean
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define philo_NUM 5
pthread_mutex_t locks[philo_NUM]; //jede Gabel kriegt ihr eigenes Lock
pthread_t philosophen[philo_NUM];
void* philosopher(void* threadid) {
int id = *(int*)threadid; //hier habe ich mir KI hilfe holen lassen. (long) tid hat irgendwie nicht funktioniert
int i = 0;
for (i=0;i <= 10000; i++) {
printf("Philosoph %d denkt nach.\n", id);
usleep(100); //Denkzeit
printf("Philosoph %d hat Hunger.\n", id);
pthread_mutex_lock(&locks[id]); //linke Gabel nehmen
pthread_mutex_lock(&locks[(id + 1) % philo_NUM]); //rechte Gabel nehmen
printf("Philosoph %d isst.\n", id);
usleep(100); //Essenszeit
pthread_mutex_unlock(&locks[(id + 1) % philo_NUM]); //rechte Gabel zurücklegen
pthread_mutex_unlock(&locks[id]); //linke Gabel zurücklegen
}
pthread_exit(NULL);
}
int main() {
int ids[philo_NUM];
for (int i = 0; i < philo_NUM; i++) {
pthread_mutex_init(&locks[i], NULL); // Initialisierung von locks anstelle von forks
}
for (int i = 0; i < philo_NUM; i++) { //Threads erstellen
ids[i] = i;
pthread_create(&philosophen[i], NULL, philosopher, &ids[i]);
}
for (int i = 0; i < philo_NUM; i++) {
pthread_join(philosophen[i], NULL);
}
pthread_exit(NULL);
}
# This makefile compiles all C files in the same directory and produces a single
# executable named solution
CC = gcc
CFLAGS = -std=gnu17 -Wall -Wextra -pedantic -O2 -g
CPPFLAGS = -MMD
LD_FLAGS = -pthread -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) $(LD_FLAGS) $(OBJ_FILES) -o $@
%.o: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
clean:
@rm -v *.o *.d
@rm -v ./$(BIN_NAME)
.PHONY: run clean
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define philo_NUM 5
pthread_mutex_t locks[philo_NUM]; //jede Gabel kriegt ihr eigenes Lock
void* philosoph(void* theadid) {
int id = *(int*)theadid;
for (int i = 0; i <= 10000; i++) {
printf("Philosoph %d denkt nach.\n", id);
usleep(100); // Denkzeit
printf("Philosoph %d hat Hunger.\n", id);
if (id % 2 == 0) { //alle gerade Philosophen
// zuerst Linke dann rechte Gabel
pthread_mutex_lock(&locks[id]);
pthread_mutex_lock(&locks[(id + 1) % philo_NUM]);
} else { //alle ungerade Philosophen
// zuerst Rechte dann linke Gabel
pthread_mutex_lock(&locks[(id + 1) % philo_NUM]);
pthread_mutex_lock(&locks[id]);
}
printf("Philosoph %d isst.\n", id);
usleep(100); // Essenszeit
// Gabeln zurücklegen
pthread_mutex_unlock(&locks[(id + 1) % philo_NUM]);
pthread_mutex_unlock(&locks[id]);
}
pthread_exit(NULL);
}
int main() {
pthread_t threads[philo_NUM];
int philosoph_nummern[philo_NUM];
for (int i = 0; i < philo_NUM; i++) { //Mutexes initialisieren
pthread_mutex_init(&locks[i], NULL);
}
for (int i = 0; i < philo_NUM; i++) { //Threads erstellen
philosoph_nummern[i] = i;
pthread_create(&threads[i], NULL, philosoph, &philosoph_nummern[i]);
}
for (int i = 0; i < philo_NUM; i++) {
pthread_join(threads[i], NULL);
}
pthread_exit(NULL);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment