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

rm

parent 7ce9ec6c
No related branches found
No related tags found
No related merge requests found
# 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 -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
File deleted
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define NUM_THREADS 6
int UnfallAnzahl;
int checkID;
pthread_mutex_t lock;
int check(char c, long tid){
if (c=='s') { //'s' für Start. Das Auto fährt auf die Brücke
checkID = tid; //und die TID dieses Autos wird gespeichert
printf("Thread %ld enters bridge\n", tid);
} else { //hier 'e' für Ende. Das Auto hat die Brücke überquert.
if (checkID == tid) //Wenn die TID mit der gespeicherten checkID übereinstimmt heißt es kein anderer Thread hat bisher die Brücke betreten da sonst die checkID nicht mit TID übereinstimmen würde.
printf("Thread %ld leaves Bridge \n ----SUCCEEDED!----\n",tid);
else {
printf("UNFALL!\n"); //wenn die TID nicht übereingestimmt hat ist ein Unfall passiert
UnfallAnzahl++;
}
}
}
void *crossBridge(void *threadid){
long tid;
int i, _error = 0;
tid = (long) threadid;
for (i=0;i <= 10000; i++) {
_error = pthread_mutex_lock(&lock);
check('s',tid); //check Funktion um zu gucken ob es einen Unfall gab / geben wird.
usleep(10); //die Dauer wie lange das Auto über die Brücke braucht
check('e',tid); //check Funktion um zu gucken ob es einen Unfall gab / geben wird.
_error = pthread_mutex_unlock(&lock);
usleep(10); // ohne dieses Warten wurde ein Thread immer hunderte Mal hintereinander ausgeführt. Es konnte also zu weniger Unfällen kommen, da der Thread weniger oft gewechselt wurde, was ich für dieses Programm als unvorteilhaft empfunden hab.
// nach Recherche liegt es wahrscheinlich daran wie der OS scheduler die Threads managed und das kann ich ja nicht beeinflussen. Dementsprechend war das die simpelste Lösung. einfach den aktuellen Prozess für kurze Zeit nicht weiterlaufen aka das Lock nicht sichern lassen.
}
pthread_exit(NULL);
}
int main(int argc, char *argv[]){
pthread_t threads[NUM_THREADS];
int rc;
int t;
int i,j;
pthread_mutex_init(&lock, NULL);
for (t = 0; t < NUM_THREADS; t++){
printf("In main: creating thread %d\n",t);
rc = pthread_create(&threads[t], NULL,crossBridge, (void *)t);
if (rc) {
printf("ERROR, return code ffrom phtread_create() is %d\n",rc);
exit(-1);
}
}
for (t=0; t < NUM_THREADS; t++) {
pthread_join(threads[t], NULL);
}
printf("Anzahl der Unfalle: %d\n", UnfallAnzahl);
pthread_exit(NULL);
}
main.o: main.c
File deleted
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment