Last active
October 10, 2017 11:54
-
-
Save SerkZex/4a32cee80d5436f602fb0971b7375f28 to your computer and use it in GitHub Desktop.
assigment3Philo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "philosophers.h" | |
#include "taskimpl.h" | |
void phil1(); | |
void phil2(); | |
void tableToString(); | |
void decideAction(char philosopherState, char philosopher); | |
QLock qFork1, qFork2; | |
int buffer; | |
volatile philosopherStruct table; | |
void phil1(){ | |
while(1){ | |
decideAction(table.philosopher1, '1'); | |
taskyield(); | |
} | |
} | |
void phil2(){ | |
while(1){ | |
decideAction(table.philosopher2, '2'); | |
taskyield(); | |
} | |
} | |
void decideAction(char philosopherState, char philosopher){ | |
char newState; | |
switch(philosopherState){ | |
case thinking: { | |
newState = hungry; | |
break; | |
} | |
case hungry: { | |
newState = havingOneFork; | |
break; | |
} | |
case havingOneFork: { | |
newState = tryingToGetTheOtherFork; | |
break; | |
} | |
case tryingToGetTheOtherFork: { | |
newState = eating; | |
break; | |
} | |
case eating: { | |
newState = thinking; | |
break; | |
} | |
} | |
if (philosopher == '1'){table.philosopher1 = newState;} | |
else if (philosopher == '2'){table.philosopher2 = newState;} | |
tableToString(); | |
taskdelay(500); | |
} | |
void taskmain(int argc, char **argv){ | |
table.fork1 = fork1Taken; | |
table.philosopher1 = thinking; | |
table.fork2 = fork2Taken; | |
table.philosopher2 = eating; | |
// taskcreate(phil1, NULL, 32768); | |
// taskcreate(phil2, NULL, 32768); | |
printf("before: %d\n", canqlock(&qFork1)); | |
qlock(&qFork1); | |
printf("after: %d\n", canqlock(&qFork1)); | |
} | |
void tableToString(){ | |
printf("rspi: %c %c %c %c\n", table.fork1, table.philosopher1, table.fork2, table.philosopher2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment