Skip to content

Instantly share code, notes, and snippets.

@SerkZex
Last active October 10, 2017 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SerkZex/4a32cee80d5436f602fb0971b7375f28 to your computer and use it in GitHub Desktop.
Save SerkZex/4a32cee80d5436f602fb0971b7375f28 to your computer and use it in GitHub Desktop.
assigment3Philo
#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