-module(diningPhils). -export([start/1,stickDown/0,phil/3]). start(N) when N>=2 -> Sticks = createSticks(N), startPhils([lists:last(Sticks)|Sticks],0). createSticks(0) -> []; createSticks(N) when N>0 -> Stick = spawn(diningPhils,stickDown,[]), Sticks = createSticks(N-1), [Stick|Sticks]. startPhils([_],_) -> ok; startPhils([SL,SR|Sticks],N) -> spawn(diningPhils,phil,[SL,SR,N]), startPhils([SR|Sticks],N+1). stickDown() -> receive {take,P} -> P!took, stickUp() end. stickUp() -> receive put -> stickDown() end. take(Stick) -> Stick!{take,self()}, receive took -> ok end. put(Stick) -> Stick!put. phil(SL,SR,N) -> base:print(N), base:printLn(" is thinking"), timer:sleep(10+N), take(SL), take(SR), base:print(N), base:printLn(" is eating"), put(SL), put(SR), phil(SL,SR,N).