Skip to content

Commit

Permalink
Merge pull request #5 from herrera-diego/herrera-diego/waiting-state
Browse files Browse the repository at this point in the history
Herrera diego/waiting state
  • Loading branch information
herrera-diego authored Oct 1, 2021
2 parents 19b4bc6 + 298bcd3 commit 148bee9
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
*.exe
*.out
*.app

# Build Folders
bin/
obj/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Dining Philosopher Implementation

## Build
`make all`
## Execution
Run the executable:

`./bin/dining-philosopher`
## Build Test
`make test`
## Run Tests
Run the executable:

`./bin/unittest`
87 changes: 87 additions & 0 deletions code/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
.DEFAULT_GOAL := all


# Include directories
INCDIR := \
-I$(FUSED_GTEST_DIR) \
-I$(FUSED_GTEST_DIR)/src \


SRCDIR=src
BINDIR=bin
OBJDIR=obj
LIBDIR=lib
TSTDIR=unittest

CC=gcc
CXX=g++
CFLAGS =-I$(SRCDIR) $(INCDIR)
CXXFLAGS =-I$(SRCDIR) $(INCDIR)
LDFLAGS =-L$(LIBDIR)
LDLIBS = -lrt
TSTLIBS = -lrt -lgtest -lgtest_main -pthread -lcrypto -lssl -lgcrypt -lgpg-error

OUTFILE=dining-philosopher
TESTFILE=unittest

CODEFILES := $(shell find $(SRCDIR) -type f -not -path "$(TSTDIR)/*")
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))

EXCFILE := main.*
TESTCODEFILES := $(shell find $(SRCDIR) -type f -not -name "$(EXCFILE)")
TESTCODEFILES += $(shell find $(TSTDIR) -type f )
TESTCFILES := $(filter %.c,$(TESTCODEFILES))
TESTCPPFILES := $(filter %.cpp,$(TESTCODEFILES))
TESTOBJFILES := $(subst $(TSTDIR)/, $(OBJDIR)/, $(subst $(SRCDIR)/, $(OBJDIR)/,$(TESTCPPFILES:%.cpp=%.o) $(TESTCFILES:%.c=%.o)))

$(OBJDIR)/%.o: $(SRCDIR)/%.c $(HDRFILES)
mkdir -p $(dir $@)
$(CC) -c -o $@ $< $(CFLAGS)
$(OBJDIR)/%.o: $(TSTDIR)/%.c $(HDRFILES)
mkdir -p $(dir $@)
$(CC) -c -o $@ $< $(CFLAGS)

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(HDRFILES)
mkdir -p $(dir $@)
$(CXX) -c -o $@ $< $(CXXFLAGS)

$(OBJDIR)/%.o: $(TSTDIR)/%.cpp $(HDRFILES)
mkdir -p $(dir $@)
$(CXX) -c -o $@ $< $(CXXFLAGS)

$(OUTFILE): $(OBJFILES)
mkdir -p $(BINDIR)
$(CXX) -o $(BINDIR)/$@ $^ $(CXXFLAGS) $(LDFLAGS) $(LDLIBS)

$(TESTFILE): $(TESTOBJFILES)
echo $(TESTOBJFILES)
mkdir -p $(BINDIR)
$(CXX) -o $(BINDIR)/$@ $^ $(CXXFLAGS) $(LDFLAGS) $(TSTLIBS)

# $(OUTFILE): $(OBJFILES)
# mkdir -p $(BINDIR)
# ar rc $(BINDIR)/$@ $^
# ranlib $(BINDIR)/$@

debug: CXXFLAGS += -DDEBUG -O0 -ggdb3
debug: CFLAGS += -DDEBUG -O0 -ggdb3
debug: $(OUTFILE)

test: CXXFLAGS += -DDEBUG -O0 -ggdb3
test: CFLAGS += -DDEBUG -O0 -ggdb3
test: $(TESTFILE)

rebuild: clean $(OUTFILE)

.PHONY: all clean test

all: $(OUTFILE)

clean:
rm -rf $(OBJDIR)/*
rm -rf $(BINDIR)/*

6 changes: 6 additions & 0 deletions code/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "waiting-state/philosopher.h"

int main()
{
return 0;
}
25 changes: 25 additions & 0 deletions code/src/waiting-state/philosopher.cpp
Original file line number Diff line number Diff line change
@@ -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()
{

}
27 changes: 27 additions & 0 deletions code/src/waiting-state/philosopher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef PHILOSOPHER_H_
#define PHILOSOPHER_H_

// #ifdef __cplusplus
// extern "C" {
// #endif
#include <string>

namespace WaitingPhilosopher
{
class Philosopher
{
public:
Philosopher();
bool checkNeighbor();
void wait();
void eat();
protected:
bool waitingState = true;


};
}

#endif /* PHILOSOPHER_H_ */

// EOF
40 changes: 40 additions & 0 deletions code/unittest/test-waiting-state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "gtest/gtest.h"
#include <string>
#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());


}

10 changes: 10 additions & 0 deletions code/unittest/testmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include "gtest/gtest.h"

using namespace std;

GTEST_API_ int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 148bee9

Please sign in to comment.