-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cauder case studies (to test.py as well)
- Loading branch information
Showing
70 changed files
with
444 additions
and
232 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-module(airline). | ||
-export([main/0, agent/2]). | ||
|
||
main() -> | ||
Main = self(), | ||
spawn(?MODULE, agent, [1, Main]), | ||
spawn(?MODULE, agent, [2, Main]), | ||
seats(3). | ||
|
||
seats(Num) -> | ||
receive | ||
{numOfSeats, Pid} -> | ||
Pid ! {seats, Num}, | ||
seats(Num); | ||
{sell, Pid} -> | ||
io:format("Seat sold!~n"), | ||
Pid ! {booked, Num}, | ||
seats(Num - 1) | ||
end. | ||
|
||
agent(NAg, Pid) -> | ||
Pid ! {numOfSeats, self()}, | ||
receive | ||
{seats, Num} when Num > 0 -> | ||
Pid ! {sell, self()}, | ||
receive | ||
{booked, _} -> agent(NAg, Pid) | ||
end; | ||
_ -> | ||
io:format("Agent~p done!~n", [NAg]) | ||
end. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
% sleeping barber problem (with a bug) | ||
|
||
-module(barber). | ||
-export([main/0, open_barber_shop/0, barber/1, customer/3]). | ||
|
||
main() -> | ||
io:format("~nCustomers: John and Joe~n~n"), | ||
ShopPid = spawn(?MODULE, open_barber_shop, []), | ||
spawn(?MODULE, customer, [ShopPid, 'John', self()]), | ||
spawn(?MODULE, customer, [ShopPid, 'Joe', self()]), | ||
receive | ||
{Name1, State1} -> io:format("~p ~p~n", [Name1, State1]) | ||
end, | ||
receive | ||
{Name2, State2} -> io:format("~p ~p~n", [Name2, State2]) | ||
end, | ||
ShopPid ! stop. | ||
|
||
%%customer | ||
customer(ShopPid, Name, MainPid) -> | ||
ShopPid ! {new, {self(), Name}}, | ||
receive | ||
X -> MainPid ! {Name, X} | ||
end. | ||
|
||
%% barber %% | ||
barber(ShopPid) -> | ||
ShopPid ! ready, | ||
receive | ||
wakeup -> | ||
barber(ShopPid); | ||
{customer, Customer} -> | ||
ShopPid ! {cut, Customer}, | ||
barber(ShopPid) | ||
end. | ||
|
||
%% shop %% | ||
open_barber_shop() -> | ||
BarberPid = spawn(?MODULE, barber, [self()]), | ||
barber_shop(BarberPid, []). | ||
|
||
%% main loop | ||
barber_shop(BarberPid, CustomersInChairs) -> | ||
receive | ||
{cut, {CustomerPid, _}} -> | ||
CustomerPid ! finished, | ||
barber_shop(BarberPid, CustomersInChairs); | ||
ready -> | ||
respond_to_barber(BarberPid, CustomersInChairs); | ||
{new, CustomerInfo} -> | ||
add_customer_if_available(BarberPid, CustomerInfo, CustomersInChairs); | ||
stop -> | ||
stop | ||
end. | ||
|
||
respond_to_barber(BarberPid, []) -> | ||
barber_shop(BarberPid, []); | ||
respond_to_barber(BarberPid, List) -> | ||
BarberPid ! {customer, last(List)}, | ||
barber_shop(BarberPid, removeCustomer(List)). | ||
|
||
% assuming 2 chairs | ||
add_customer_if_available(BarberPid, {CustomerPid, _CustomerName}, [X, Y | R]) -> | ||
CustomerPid ! no_room, | ||
barber_shop(BarberPid, [X, Y | R]); | ||
add_customer_if_available(BarberPid, {CustomerPid, CustomerName}, List) -> | ||
BarberPid ! wakeup, | ||
barber_shop(BarberPid, [{CustomerPid, CustomerName} | List]). | ||
|
||
last([A]) -> A; | ||
last([_A | R]) -> last(R). | ||
|
||
removeCustomer([_A | R]) -> R. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
digraph main_0 { | ||
rankdir="LR"; | ||
n_0 [label="main_0", shape="plaintext"]; | ||
n_1 [id="5", shape=circle, label="5"]; | ||
n_2 [id="6", shape=circle, label="6"]; | ||
n_3 [id="1", shape=circle, label="1"]; | ||
n_0 -> n_3 [arrowhead=none]; | ||
n_4 [id="2", shape=circle, label="2"]; | ||
n_5 [id="4", shape=doublecircle, label="7"]; | ||
n_6 [id="7", shape=circle, label="3"]; | ||
n_7 [id="3", shape=circle, label="4"]; | ||
|
||
n_1 -> n_2 [id="[$e|5]", label="receive {Name2,State2}"]; | ||
n_4 -> n_6 [id="[$e|0]", label="spawn customer/3.0"]; | ||
n_2 -> n_5 [id="[$e|2]", label="send stop to open_barber_shop/0.0"]; | ||
n_6 -> n_7 [id="[$e|3]", label="spawn customer/3.1"]; | ||
n_7 -> n_1 [id="[$e|4]", label="receive {Name1,State1}"]; | ||
n_3 -> n_4 [id="[$e|1]", label="spawn open_barber_shop/0.0"]; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
%code showing a mutual exclusion violation | ||
%the two concurrent increments of the variable managed by varManager | ||
%may lead to final value 1 instead of 2 under some schedulings | ||
|
||
-module(meViolation). | ||
-export([main/0, meManager/0, varManager/1, incrementer/2]). | ||
|
||
main() -> | ||
MePid = spawn(?MODULE, meManager, []), | ||
XPid = spawn(?MODULE, varManager, [0]), | ||
spawn(?MODULE, incrementer, [MePid, XPid]), | ||
spawn(?MODULE, incrementer, [MePid, XPid]). | ||
|
||
meManager() -> | ||
receive | ||
{request, Pid} -> Pid ! answer | ||
end, | ||
receive | ||
{release} -> meManager() | ||
end. | ||
|
||
varManager(Val) -> | ||
io:format("Variable value:~b~n", [Val]), | ||
receive | ||
{write, NewVal} -> varManager(NewVal); | ||
{read, Pid} -> Pid ! Val | ||
end, | ||
varManager(Val). | ||
|
||
incrementer(MePid, XPid) -> | ||
MePid ! {request, self()}, | ||
receive | ||
answer -> | ||
XPid ! {read, self()}, | ||
receive | ||
X -> | ||
XPid ! {write, X + 1}, | ||
MePid ! {release} | ||
end | ||
end. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
%FASE 2014 example | ||
|
||
-module(purchase). | ||
-export([main/0, asynchAnd/2, checkCredit/2, checkAddress/1, checkItem/1]). | ||
|
||
main() -> | ||
Pid = spawn(?MODULE, asynchAnd, [2, self()]), | ||
spawn(?MODULE, checkCredit, [15, Pid]), | ||
spawn(?MODULE, checkAddress, [Pid]), | ||
spawn(?MODULE, checkItem, [Pid]), | ||
receive | ||
true -> | ||
io:format("All checks successful.~n"), | ||
true; | ||
false -> | ||
io:format("Check failure.~n"), | ||
false | ||
end. | ||
|
||
asynchAnd(N, Out) -> | ||
if | ||
N > 0 -> | ||
receive | ||
true -> asynchAnd(N - 1, Out); | ||
false -> Out ! false | ||
end; | ||
true -> | ||
Out ! true | ||
end. | ||
|
||
checkCredit(Price, Pid) -> | ||
if | ||
Price < 10 -> | ||
io:format("Credit check successful~n"), | ||
Pid ! true; | ||
true -> | ||
io:format("Credit check failed~n"), | ||
Pid ! false | ||
end. | ||
|
||
checkAddress(Pid) -> | ||
io:format("Address check successful~n"), | ||
Pid ! true. | ||
|
||
checkItem(Pid) -> | ||
io:format("Item check successful~n"), | ||
Pid ! true. |
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
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
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
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
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
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
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
Oops, something went wrong.