diff --git a/README.md b/README.md index 2490130..47499c3 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,11 @@ This was originally formulated in 1965 by Edsger Dijkstra as a student exam exer ## Solution +### Waiting State + - The philosopher enters into a waiting state when neighbors are eating + - When its neighbors are done, go for the forks + - Its neighbors can't enter into the waiting state if there is a neighbor already waiting + +## Tests + +Tests are done using googletests diff --git a/code/README.md b/code/README.md new file mode 100644 index 0000000..e69de29 diff --git a/code/bin/dining-philosopher b/code/bin/dining-philosopher new file mode 100755 index 0000000..5316863 Binary files /dev/null and b/code/bin/dining-philosopher differ diff --git a/code/makefile b/code/makefile new file mode 100644 index 0000000..c8f981c --- /dev/null +++ b/code/makefile @@ -0,0 +1,64 @@ +.DEFAULT_GOAL := all + + +# Include directories +INCDIR := \ + -I.. \ + +SRCDIR=src +BINDIR=bin +OBJDIR=obj +LIBDIR=lib + +CC=gcc +CXX=g++ +CFLAGS =-I$(SRCDIR) $(INCDIR) +CXXFLAGS =-I$(SRCDIR) $(INCDIR) +LDFLAGS =-L$(LIBDIR) +LDLIBS = -lrt + +OUTFILE=dining-philosopher + +EXCDIR := unittest +CODEFILES := $(shell find $(SRCDIR) -type f -not -path "$(EXCDIR)/*") +CFILES := $(filter %.c,$(CODEFILES)) +CPPFILES := $(filter %.cpp,$(CODEFILES)) +SRCFILES := $(CFILES) $(CPPFILES) +HDRFILES := $(filter %.h,$(CODEFILES)) +OBJFILES := $(subst $(SRCDIR), $(OBJDIR),$(CPPFILES:%.cpp=%.o) $(CFILES:%.c=%.o)) + + + + + +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(HDRFILES) + mkdir -p $(dir $@) + $(CC) -c -o $@ $< $(CFLAGS) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(HDRFILES) + mkdir -p $(dir $@) + $(CXX) -c -o $@ $< $(CXXFLAGS) + +$(OUTFILE): $(OBJFILES) + mkdir -p $(BINDIR) + $(CXX) -o $(BINDIR)/$@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS) + +# $(OUTFILE): $(OBJFILES) +# mkdir -p $(BINDIR) +# ar rc $(BINDIR)/$@ $^ +# ranlib $(BINDIR)/$@ + +debug: CXXFLAGS += -DDEBUG -O0 -ggdb3 +debug: CFLAGS += -DDEBUG -O0 -ggdb3 +debug: $(OUTFILE) + +rebuild: clean $(OUTFILE) + +.PHONY: all clean + +all: $(OUTFILE) + +clean: + rm -rf $(OBJDIR)/* + rm -rf $(BINDIR)/* + \ No newline at end of file diff --git a/code/src/main.cpp b/code/src/main.cpp new file mode 100644 index 0000000..3551fbb --- /dev/null +++ b/code/src/main.cpp @@ -0,0 +1,6 @@ +#include "waiting-state/philosopher.h" + +int main() +{ + return 0; +} \ No newline at end of file diff --git a/code/src/waiting-state/philosopher.cpp b/code/src/waiting-state/philosopher.cpp new file mode 100644 index 0000000..da2d0e0 --- /dev/null +++ b/code/src/waiting-state/philosopher.cpp @@ -0,0 +1,25 @@ +#include "philosopher.h" + +using namespace WaitingPhilosopher; + +Philosopher::Philosopher() +{ + +} + +bool Philosopher::checkNeighbor() +{ + bool neighborBusy = true; + + return neighborBusy; +} + +void Philosopher::wait() +{ + +} + +void Philosopher::eat() +{ + +} \ No newline at end of file diff --git a/code/src/waiting-state/philosopher.h b/code/src/waiting-state/philosopher.h new file mode 100644 index 0000000..0001545 --- /dev/null +++ b/code/src/waiting-state/philosopher.h @@ -0,0 +1,27 @@ +#ifndef PHILOSOPHER_H_ +#define PHILOSOPHER_H_ + +// #ifdef __cplusplus +// extern "C" { +// #endif +#include + +namespace WaitingPhilosopher +{ + class Philosopher + { + public: + Philosopher(); + bool checkNeighbor(); + void wait(); + void eat(); + protected: + bool waitingState = true; + + + }; +} + +#endif /* PHILOSOPHER_H_ */ + +// EOF \ No newline at end of file diff --git a/code/unittest/bin/unittest b/code/unittest/bin/unittest new file mode 100755 index 0000000..b5542eb Binary files /dev/null and b/code/unittest/bin/unittest differ diff --git a/code/unittest/makefile b/code/unittest/makefile new file mode 100644 index 0000000..fb2ff54 --- /dev/null +++ b/code/unittest/makefile @@ -0,0 +1,62 @@ +.DEFAULT_GOAL := all + +SRCDIR=src +BINDIR=bin +OBJDIR=obj +LIBDIR= + +FUSED_GTEST_DIR = /usr/src/googletest/googletest + + + +# Include directories +INCDIR := \ + -Iinclude/ \ + -I$(FUSED_GTEST_DIR) \ + -I$(FUSED_GTEST_DIR)/src \ + -I../src \ + +CC=gcc +CXX=g++ +CFLAGS =-I$(SRCDIR) $(INCDIR) +CXXFLAGS =-I$(SRCDIR) $(INCDIR) +LDFLAGS =-L$(LIBDIR) +LDLIBS = -lrt -lgtest -lgtest_main -pthread -lcrypto -lssl -lgcrypt -lgpg-error + +OUTFILE=unittest + +EXCFILE := main.* +CODEFILES := $(shell find $(SRCDIR) -type f -not -name "$(EXCFILE)") +CFILES := $(filter %.c,$(CODEFILES)) +CPPFILES := $(filter %.cpp,$(CODEFILES)) +SRCFILES := $(CFILES) $(CPPFILES) +HDRFILES := $(filter %.h,$(CODEFILES)) +OBJFILES := $(subst $(SRCDIR), $(OBJDIR),$(CPPFILES:%.cpp=%.o) $(CFILES:%.c=%.o)) +SRCOBJFILES := $(shell find ../obj -type f -not -name "$(EXCFILE)") + +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(HDRFILES) + mkdir -p $(dir $@) + $(CC) -c -o $@ $< $(CFLAGS) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(HDRFILES) + mkdir -p $(dir $@) + $(CXX) -c -o $@ $< $(CXXFLAGS) + +$(OUTFILE): $(OBJFILES) $(SRCOBJFILES) + mkdir -p $(BINDIR) + $(CXX) -o $(BINDIR)/$@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS) + + +debug: CXXFLAGS += -DDEBUG -O0 -ggdb3 +debug: CFLAGS += -DDEBUG -O0 -ggdb3 +debug: clean $(OUTFILE) + +rebuild: clean $(OUTFILE) + +.PHONY: all clean + +all: debug + +clean: + rm -rf $(OBJDIR)/* + rm -rf $(BINDIR)/* \ No newline at end of file diff --git a/code/unittest/src/unittest.cpp b/code/unittest/src/unittest.cpp new file mode 100644 index 0000000..dadb1a1 --- /dev/null +++ b/code/unittest/src/unittest.cpp @@ -0,0 +1,10 @@ +#include +#include "gtest/gtest.h" + +using namespace std; + +GTEST_API_ int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/code/unittest/src/waiting-philosopher.cpp b/code/unittest/src/waiting-philosopher.cpp new file mode 100644 index 0000000..b65cfac --- /dev/null +++ b/code/unittest/src/waiting-philosopher.cpp @@ -0,0 +1,40 @@ +#include "gtest/gtest.h" +#include +#include "waiting-state/philosopher.h" + +using namespace WaitingPhilosopher; +using namespace std; + +class WaitingPhilosopherTest : public testing::Test +{ + public: + + void SetUp() override { } + void TearDown() override { } + + // Per-test-suite set-up. + // Called before the first test in this test suite. + // Can be omitted if not needed. + static void SetUpTestSuite() + { + + } + + // Per-test-suite tear-down. + // Called after the last test in this test suite. + // Can be omitted if not needed. + static void TearDownTestSuite() + { + + } +}; + + +TEST_F(WaitingPhilosopherTest, evenPhilosophers) +{ + Philosopher nietzche; + ASSERT_TRUE(nietzche.checkNeighbor()); + + +} +