Skip to content

Commit 6126a05

Browse files
Sandeep PinnintiSandeep Pinninti
authored andcommitted
dining philosophers' problem (#51)
* Dining Philosophers problem * PhillArgs_V2 * Dining phils commit * update targerFolder * PhilArgs as decl class * PickUpStrategy fix * Improved Code * code update * Added example Phil_V3 * change in folder path * DiningPhils examples * revert unrealted changes * CR-1057 Printout update * RIC-1057 Update in logs * RIC-1057 Update in logs * RIC-1057 DiningPhils changes --------- Co-authored-by: Sandeep Pinninti <sandeep.pinninti@prod.hclpnp.com>
1 parent 3a7b7bb commit 6126a05

16 files changed

Lines changed: 603 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
capsule Fork{
2+
3+
[[rt::header_preface]]
4+
`
5+
#include <cstdio>
6+
#include <cstdlib>
7+
#include <iostream>
8+
`
9+
10+
[[rt::decl]]
11+
`
12+
private:
13+
const char * myName;
14+
`
15+
service behavior port leftP~: ForkProt;
16+
service behavior port rightP~: ForkProt;
17+
behavior port logP: Log;
18+
statemachine {
19+
state Down, Up;
20+
initial -> Down `
21+
// what is my name?
22+
myName = getName();
23+
std::cout << myName << " starting up" << std::endl;
24+
`;
25+
gotUpFromLeft : Down -> Up on leftP.up `
26+
leftP.ack().send();
27+
`;
28+
gotUpFromRight : Down -> Up on rightP.up `
29+
rightP.ack().send();
30+
`;
31+
gotUpFromLeft1 : Up -> Up on leftP.up `
32+
leftP.nack().send();
33+
`;
34+
gotUpFromRight1 : Up -> Up on rightP.up `
35+
rightP.nack().send();
36+
`;
37+
gotDown : Up -> Down on rightP.down , leftP.down`
38+
`;
39+
};
40+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
protocol ForkProt{
2+
3+
in ack();
4+
out down();
5+
in nack();
6+
out up();
7+
};
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
capsule Phil {
2+
[[rt::header_preface]]
3+
`
4+
#include <cstring>
5+
#include <cstdio>
6+
#include <cstdlib>
7+
#include <time.h>
8+
#include <iostream>
9+
#include "ForkProt.h"
10+
`
11+
[[rt::decl]]
12+
`
13+
public:
14+
enum PickUpStrategy {
15+
RANDOM, LEFTFIRST, RIGHTFIRST
16+
};
17+
`
18+
[[rt::decl]]
19+
`
20+
PickUpStrategy pickUpStrat = PickUpStrategy::RANDOM;
21+
const char * myName;
22+
`
23+
service behavior port rightP : ForkProt;
24+
service behavior port leftP : ForkProt;
25+
behavior port timerP : Timing;
26+
behavior port logP : Log;
27+
statemachine {
28+
state Thinking {
29+
entry
30+
`
31+
std::cout << myName << " thinking " << std::flush << std::endl;
32+
timerP.informIn(RTTimespec(3, 100000000));
33+
`;
34+
}, Wait4Right, Wait4Left, Wait4First, GoLeftWait4Right, GoRightWait4Right, Eating {
35+
entry
36+
`
37+
std::cout << " " << myName << " eating " << std::flush << std::endl;
38+
int rnd = rand() % 10;
39+
timerP.informIn(RTTimespec(6,rnd*10000000));
40+
`;
41+
};
42+
choice _junction;
43+
_initial: initial -> Thinking
44+
`
45+
// what is my name?
46+
myName = getName();
47+
std::cout << myName << " starting up " << std::flush << std::endl;
48+
std::cout << myName << " using" << std::flush << std::endl;
49+
50+
if (pickUpStrat==PickUpStrategy::RANDOM)
51+
std::cout << myName << " random" << std::flush << std::endl;
52+
else if (pickUpStrat==PickUpStrategy::LEFTFIRST)
53+
std::cout << myName << " left-first" << std::flush << std::endl;
54+
else if (pickUpStrat==PickUpStrategy::RIGHTFIRST)
55+
std::cout << myName << " right-first" << std::flush << std::endl;
56+
57+
58+
// initialize random number generator
59+
srand(time(NULL));
60+
`;
61+
gotTimeout: Thinking -> _junction on timerP.timeout;
62+
left_first: _junction -> Wait4Left when
63+
`
64+
return (pickUpStrat==PickUpStrategy::LEFTFIRST);`
65+
`
66+
leftP.up().send();
67+
`;
68+
right_first: _junction -> Wait4Right when
69+
`
70+
return (pickUpStrat==PickUpStrategy::RIGHTFIRST);`
71+
`
72+
rightP.up().send();
73+
`;
74+
random: _junction -> Wait4First when
75+
`
76+
return (pickUpStrat==PickUpStrategy::RANDOM);
77+
`
78+
`
79+
int rnd = rand() % 100;
80+
if (rnd>50)
81+
leftP.up().send();
82+
else
83+
rightP.up().send();
84+
`;
85+
random_else: _junction -> Wait4First when `else`;
86+
gotLeft1: Wait4Left -> GoLeftWait4Right on leftP.ack
87+
`
88+
std::cout << " " << myName << " got left " << std::flush << std::endl;
89+
rightP.up().send();
90+
`;
91+
gotLeft2: Wait4First -> GoLeftWait4Right on leftP.ack
92+
`
93+
std::cout << " " << myName << " got left " << std::flush << std::endl;
94+
rightP.up().send();
95+
`;
96+
gotRight1: Wait4Right -> GoRightWait4Right on rightP.ack
97+
`
98+
std::cout << " " << myName << " got right " << std::flush << std::endl;
99+
leftP.up().send();
100+
`;
101+
gotRight2: Wait4First -> GoRightWait4Right on rightP.ack
102+
`
103+
std::cout << " " << myName << " got right " << std::flush << std::endl;
104+
leftP.up().send();
105+
`;
106+
gotLeft3: GoRightWait4Right -> Eating on leftP.ack
107+
`
108+
// std::cout << " " << myName << " to eating " << std::flush << std::endl;
109+
`;
110+
gotRight3: GoLeftWait4Right -> Eating on rightP.ack
111+
`
112+
// std::cout << " " << myName << " to eating " << std::flush << std::endl;
113+
`;
114+
putDownForks: Eating -> Thinking on timerP.timeout
115+
`
116+
leftP.down().send();
117+
rightP.down().send();
118+
`;
119+
noNack1: Wait4Left -> Wait4Left on leftP.nack
120+
`
121+
leftP.up().send();
122+
`;
123+
noNeck2: Wait4First -> Wait4First on leftP.nack, rightP.nack
124+
`
125+
RTProtocol * port = msg->sap();
126+
if (port != (RTProtocol *) 0)
127+
((ForkProt::Base *) port)->up().send();
128+
`;
129+
noNeck3: Wait4Right -> Wait4Right on rightP.nack
130+
`
131+
rightP.up().send();
132+
`;
133+
noNeck4: GoLeftWait4Right -> GoLeftWait4Right on rightP.nack
134+
`
135+
rightP.up().send();
136+
`;
137+
noNeck5: GoRightWait4Right -> GoRightWait4Right on leftP.nack
138+
`
139+
leftP.up().send();
140+
`;
141+
};
142+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- model of Classical Dining Philosophers problem
2+
Version v1: Illustrates fixed capsules w/ wired ports, enumeration types (PickUpStrategy), dependencies (Phil using PickUpStrategy), random number generation, and getName() to determine name of capsule instance.
3+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
capsule Top {
2+
3+
[[rt::header_preface]]
4+
`
5+
#include <cstdio>
6+
#include <cstdlib>
7+
#include <iostream>
8+
`
9+
10+
behavior port logP: Log;
11+
behavior port frameP: Frame;
12+
fixed part fork0: Fork, fork1: Fork,fork2: Fork,fork3: Fork;
13+
fixed part phil0: Phil, phil1: Phil,phil2: Phil,phil3: Phil;
14+
15+
connect phil0.leftP with fork0.rightP;
16+
connect fork0.leftP with phil1.rightP;
17+
connect phil1.leftP with fork1.rightP;
18+
connect fork1.leftP with phil2.rightP;
19+
connect phil2.leftP with fork2.rightP;
20+
connect fork2.leftP with phil3.rightP;
21+
connect phil3.leftP with fork3.rightP;
22+
connect fork3.leftP with phil0.rightP;
23+
24+
25+
statemachine{
26+
state State1;
27+
initial -> State1
28+
`
29+
std::cout << "[Top] starting up" << std::endl;
30+
`;
31+
};
32+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//Transformation Configuration File
2+
let tc = TCF.define(TCF.CPP_TRANSFORM);
3+
tc.topCapsule = 'Top';
4+
tc.targetFolder = 'DiningPhils_v1_target';
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
capsule Fork {
2+
[[rt::header_preface]]
3+
`
4+
#include <cstdio>
5+
#include <cstdlib>
6+
#include <iostream>
7+
#include <cstring>
8+
#include "ForkArgs.art.h"
9+
#include <PickUpStrategy.art.h>
10+
11+
`
12+
[[rt::decl]]
13+
`
14+
public:
15+
char * myName;
16+
int id;
17+
int numPhils;
18+
PickUpStrategy pickUpStrat;
19+
`
20+
service behavior port leftP~ : ForkProt;
21+
service behavior port rightP~ : ForkProt;
22+
behavior port logP : Log;
23+
statemachine {
24+
state Down, Up;
25+
initial -> Down
26+
`// receive initialization arguments
27+
ForkArgs fArgs = (ForkArgs) *((ForkArgs *) rtdata);
28+
id = fArgs.id;
29+
numPhils = fArgs.numPhils;
30+
31+
// my name is 'f'+id
32+
char buffer1[10];
33+
snprintf(buffer1, 10, "f%d", id);
34+
myName = new char[strlen(buffer1) + 1];
35+
strcpy(myName, buffer1);
36+
//logP.log("[%s] starting up", myName);
37+
std::cout << myName << " starting up" << std::endl;
38+
`;
39+
gotUp: Down -> Up on leftP.up, rightP.up
40+
`
41+
RTProtocol * port = msg->sap();
42+
if (port != (RTProtocol *) 0)
43+
((ForkProt::Conjugate *) port)->ack().send();
44+
`;
45+
gotUp1: Up -> Up on leftP.up, rightP.up
46+
`
47+
48+
RTProtocol * port = msg->sap();
49+
if (port != (RTProtocol *) 0)
50+
((ForkProt::Conjugate *) port)->nack().send();
51+
`;
52+
gotDown: Up -> Down on rightP.down, leftP.down
53+
`
54+
55+
`;
56+
};
57+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[[rt::decl]]
2+
`
3+
#include <PickUpStrategy.art.h>
4+
class [[rt::auto_descriptor]] ForkArgs {
5+
public:
6+
int id;
7+
int numPhils;
8+
};
9+
10+
`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
protocol ForkProt{
2+
3+
in ack();
4+
out down();
5+
in nack();
6+
out up();
7+
};

0 commit comments

Comments
 (0)