Sie sind auf Seite 1von 8

Q1)

CSP Coue:

1 a)
#uefine N 2;
vai()=val(u);
val(i)=ieau.i->val(i)|j(|ja:{u..N}(wiite.a->val(a)));

1 b)
#asseit vai() |= <>(wiite.2->ieau.2->ieau.2->wiite.1->wiite.u->ieau.u);


Q2 a)



public class Threads {
public static int account[]={1000,1000};



static class Account1 implements Runnable {


public Account1(){}
@Override
public void run() {
boolean running=true;
while(running)
{

if (account[0]>100) {

int newval=account[0]-100;
account[1]=account[1]+100;
account[0]=newval;


if(account[0]+account[1]!=2000)
System.out.println("This
is transferring from account1 to account2 wherein account1's
balance is"+account[0]+" and account2's balance is
"+account[1]);
} else {
Thread.yield();
}

}
// TODO Auto-generated method stub

}

}
static class Account2 implements Runnable {


public Account2(){}
@Override
public void run() {
boolean running=true;
while(running)
{
// TODO Auto-generated method stub
if (account[1]>100) {
int newval=account[1]-100;
account[0]=account[0]+100;
account[1]=newval;

if(account[0]+account[1]!=2000)
System.out.println("This is
transferring from account2 to account1 wherein account1's
balance is"+account[0]+" and account2's balance is
"+account[1]);
} else {
Thread.yield();
}



}
}


}


@SuppressWarnings("deprecation")
public static void main(String[] args)
{


Thread t1=new Thread(new Account1());
Thread t2=new Thread(new Account2());
t1.start();
t2.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

}
Some wrong outputs were as follows:
This is transferring from account2 to account1 wherein
account1's balance is 1000 and account2's balance is 900
This is transferring from account2 to account1 wherein
account1's balance is 1100 and account2's balance is 800
This is transferring from account2 to account1 wherein
account1's balance is 1200 and account2's balance is 700
This is transferring from account2 to account1 wherein
account1's balance is 1300 and account2's balance is 600
This is transferring from account2 to account1 wherein
account1's balance is 1400 and account2's balance is 500
This is transferring from account2 to account1 wherein
account1's balance is 1500 and account2's balance is 400
This is transferring from account2 to account1 wherein
account1's balance is 1600 and account2's balance is 300
This is transferring from account2 to account1 wherein
account1's balance is 1700 and account2's balance is 200
This is transferring from account2 to account1 wherein
account1's balance is 1800 and account2's balance is 100

Cash transfer operation is not atomic.

Noney loss has occuiieu in these uisplayeu outputs uue to the fact that the 2
steps of cash withuiawal fiom one account anu cash ueposit into anothei
account aie not atomic. Bue to this the following possibility can happen wheiein
the fiist thieau ieaus the value of say Account 1 anu stoies it anu then the
seconu thieau comes in ieaus the value of the balance in Account 2 anu peifoims
the tiansfei opeiations fiom Account 2 to Account 1 but when the initial thieau
iesumes execution insteau of woiking with the new value of the ueposit in
Account1 it uses the value that it hau alieauy ieau anu tiansfeis the money fiom
Account 1 to Account 2. This leaus to an eiioi.
Foi ex:
Initially say both accounts have 1uuu cents. So in Account 1 the following
opeiations aie peifoimeu:
newval=1uuu-1uu=9uu;
Anu then thieau2 peifoims the following opeiation foi account2
Newval=1uuu-1uu=9uu;
Account1=1uuu+1uu=11uu;
Account2=9uu;
Then account 1 iesumes anu peifoims
Account2=9uu+1uu=1uuu;
Account1=newval;newval=9uu
So now Account1 has 9uu cents anu Account 2 has 1uuu cents This is an example
foi the money loss.
To expose this conuition the statement : if(account[0]+account[1]!=2000)
to check whether the sum of the 2 balances in the respective
accounts equals the total initial money can be performed as
highlighted in the java code above.

b)
vai acc1 = 1uuu;
vai acc2 = 1uuu;
vai newval1;
vai newval2;
vai in1 = false;
vai in2 = false;
#uefine goal acc1<u;
#uefine goal2 acc1+acc2==2uuu;

Tiansfei1() = |acc1>1uuj stait1{in1 = tiue} -> stoie1{newval1 = acc1 - 1uu;} ->
auu2{acc2 = acc2 + 1uu;} -> upuate1{acc1 = newval1;} -> enu1{in1 = false} ->
Tiansfei1() |j |acc1+acc2 != 2uuu && in2 == falsej illegal1 -> Stop();

Tiansfei2() = |acc2>1uuj stait2{in2 = tiue} -> stoie2{newval2 = acc2 - 1uu;} ->
auu1{acc1 = acc1 + 1uu;} -> upuate2{acc2 = newval2;} -> enu2{in2 = false} ->
Tiansfei2() |j |acc1+acc2 != 2uuu && in1 == falsej illegal2 -> Stop();

Act() = Tiansfei1() || Tiansfei2();

2 c)
#asseit Act() ieaches goal;
#asseit Act() ieaches goal2;
#asseit Act() |= !<> illegal1;
#asseit Act() |= !<> illegal2;
#asseit Act() ueteiministic;

In this mouel the following asseitions have been useu to expose the conuition.
uoal anu goal2 which check if account 1 anu account 2's combineu balances
eveiy ieach a state when they uo not equal 2uuu anu if account 1's balance is
positive.

2 u)
Fixeu }ava Coue:

public class Threads {
public static int account[]={1000,1000};

public synchronized static void AccountTransfer(int
act) {
if (account[act]>100) {
int other=1;
if(act==1)
other=0;

int newval=account[act]-100;
account[other]=account[other]+100;
account[act]=newval;
if(account[0]+account[1]!=2000)
System.out.println("Account1="+account[0]+"
Account2="+account[1]);
} else {
Thread.yield();
}
}

static class Account1 implements Runnable {


public Account1(){}
@Override
public void run() {
boolean running=true;
while(running)
{

AccountTransfer(0);

}
// TODO Auto-generated method stub

}


}
static class Account2 implements Runnable {


public Account2(){}
@Override
public void run() {
boolean running=true;
while(running)
{
AccountTransfer(1);


}
}


}


@SuppressWarnings("deprecation")
public static void main(String[] args)
{


Thread t1=new Thread(new Account1());
Thread t2=new Thread(new Account2());
t1.start();
t2.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//while(Threads.count!=0);
//t1.stop();
//t2.stop();

//System.out.println("This is transferring from
account2 to account1 wherein account1's balance
is"+account[0]+" and account2's balance is "+account[1]);


}

}

Fixeu by the usage of the synchionizeu methou account tiansfei.

Fixeu mouel by using atomic

vai acc1 = 1uuu;
vai acc2 = 1uuu;
vai newval1;
vai newval2;
vai in1 = false;
vai in2 = false;
#uefine goal acc1<u;
#uefine goal2 acc1+acc2==2uuu;

Tiansfei1() = atomic{|acc1>1uuj stait1{in1 = tiue} -> stoie1{newval1 = acc1 -
1uu;} -> auu2{acc2 = acc2 + 1uu;} -> upuate1{acc1 = newval1;} -> enu1{in1 =
false} -> Skip;}Tiansfei1() ;

Tiansfei2() = atomic{|acc2>1uuj stait2{in2 = tiue} -> stoie2{newval2 = acc2 -
1uu;} -> auu1{acc1 = acc1 + 1uu;} -> upuate2{acc2 = newval2;} -> enu2{in2 =
false} -> Skip;}Tiansfei2();

Act() = Tiansfei1() || Tiansfei2();

QS.
a)
#uefine N 2;

vall(i)=iequest.i->seivice.i->iesponse.i->val();
val()=|jb:{u..N}vall(b);
b)

Das könnte Ihnen auch gefallen