Skip to content
Snippets Groups Projects
Commit 83435db3 authored by kadsendino's avatar kadsendino
Browse files

Übung 3 - Aufgabe 1

parent df947ab5
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=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
// simple example for pthreads
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include<unistd.h>
#define NUM_THREADS 4
#define NUM_SIM 10000
int kollisionen;
long thread_start[NUM_THREADS];
long thread_end[NUM_THREADS];
pthread_mutex_t lock;
int level[NUM_THREADS];
int last[NUM_THREADS];
// int favoured;
void *driveAuto(void *threadid)
{
long tid;
tid = (long)threadid;
int _error = 0;
struct timeval tv;
//Beginn kritischer Abschnitt
_error = pthread_mutex_lock(&lock);
gettimeofday(&tv,NULL); //Zeitpunkt, wann das Auto losfaehrt.
thread_start[tid]=tv.tv_usec; //Speichert Startzeitpunkt von Auto pid
// printf("Hehe Car %ld is driving\n", tid);
sleep(0.001); //Auto faehrt ueber Bruecke
gettimeofday(&tv,NULL); //Zeitpunkt, wann das Auto ankommt.
thread_end[tid]=tv.tv_usec; //Speichert Endzeitpunkt von Auto pid
_error = pthread_mutex_unlock(&lock);
//Ende kritischer Abschnitt
pthread_exit (NULL);
}
int main (void)
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
//inital data
for (int i = 0; i < NUM_THREADS; i++) {
level[i] = 0;
last[i] = 0;
}
kollisionen = 0;
// init lock
pthread_mutex_init(&lock, NULL);
for(int i= 0;i<NUM_SIM;i++){ //Anzahl der Simulationen
for(t=0; t < NUM_THREADS; t++) {
// printf ("In main: creating thread %ld\n", t);
rc = pthread_create (&threads[t], NULL, driveAuto, (void *)t);
if (rc) {
printf ("ERROR; return code from pthread_create () is %d\n", rc);
exit (-1);
}
}
// joining threads
for (t = 0; t < NUM_THREADS; t++) {
pthread_join (threads[t], NULL);
}
//berechne, ob Autos kollidiert sind
for(int k=0;k<NUM_THREADS;k++){
for (int l=0;l<NUM_THREADS;l++) {
if ((thread_start[k] < thread_end[l] && thread_start[k] > thread_start[l]) ) {
kollisionen++;
}
}
}
}
printf("Simulationen: %d\n",NUM_SIM);
printf("Kollisionen: %d\n",kollisionen);
// for(int i=0;i<NUM_THREADS;i++){
// printf("%ld , %ld\n", thread_start[i], thread_end[i]);
// }
//
/* Last thing that main() should do */
pthread_exit(NULL);
}
aufgabe1.o: aufgabe1.c
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment