CloudLabProgramming Exercises
CloudLabProgramming Exercises
1
Answers to Selected Programming Exercises
Thread Model
Qn : Write a program to print “Hello World” using Aneka Thread Programming model use Single Thread?
Procedure
Follow the below mentioned steps to create the project and the necessary references
1. Creating a C# Console Application in .NET 2.0
a. Go to File -> New -> Project.
b. From New Project Dialog select visual C# - > windows .
c. Select .NET 2.0 and Console Application.
d. Enter the Solution Name and press OK.
2. Add References to the project from the following folders under Solution Explorer
a. Right Click on the references
i. Click on Add Reference
ii. Click on Browse Tab
iii. Select the following folders one after the other and add them
C:\Program Files\Manjrasoft\Aneka.3.0\Tools\SDK\Common
C:\Program Files\Manjrasoft\Aneka.3.0\Tools\SDK\Runtime
C:\Program Files\Manjrasoft\Aneka.3.0\Tools\SDK\Thread Model
3. Place the conf.xml in the folder and mention the same path in the code.
4. Add the below mentioned code to .CS file by replacing the code in the appropriate place
Source code
using System;
using System.Collections.Generic;
using System.Text;
using Aneka;
using Aneka.Threading;
using Aneka.Entity;
using System.Threading;
namespace AnekaThreadPractise1
{
[Serializable]
public class HelloWorld
{
public string result;
public HelloWorld()
{
}
public void PrintHello()
{
result = "HelloWorld";
}
}
class Program
{
static void Main(string[] args)
{
AnekaApplication<AnekaThread, ThreadManager> app = null;
try
{
Logger.Start();
Configuration conf =
Configuration.GetConfiguration(@"C:\Users\raghav\Documents\
CloudComputing-Lectures\conf.xml");
2
app = new AnekaApplication<AnekaThread,
ThreadManager>(conf);
HelloWorld hw = new HelloWorld();
Ouput :
Value : Helloworld
Qn : Write a program to print “Hello World” based on Thread model and use exactly five threads also print the
executor node information along with the Submission Time and Completion Time?
using System;
using System.Collections.Generic;
using System.Text;
using Aneka;
using Aneka.Threading;
using Aneka.Entity;
using System.Threading;
namespace AnekaThreadsfive
{
[Serializable]
public class HelloWorld
{
public string result;
public HelloWorld()
{
}
public void PrintHello()
{
result = "HelloWorld";
}
}
class Program
{
static void Main(string[] args)
{
AnekaApplication<AnekaThread, ThreadManager> app = null;
try
{
Logger.Start();
Configuration conf = Configuration.GetConfiguration(@"C:\Users\
raghav\Documents\CloudComputing-Lectures\conf.xml");
app = new AnekaApplication<AnekaThread, ThreadManager>(conf);
HelloWorld hw = new HelloWorld();
AnekaThread[] th = new AnekaThread[5];
3
for (int i = 0; i < 5; i++)
{
th[i] = new AnekaThread(hw.PrintHello, app);
th[i].Start();
}
for (int i = 0; i < 5; i++)
{
th[i].Join();
hw = (HelloWorld)th[i].Target;
Console.WriteLine("Value : {0} , NodeId:{1},Submission Time:
{2},Completion Time{3}", hw.result,
th[i].NodeId,th[i].SubmissionTime,th[i].CompletionTime);
}
}
finally
{
app.StopExecution();
Logger.Stop();
}
}
}
}
Qn : Write a program to print “Hello World” using Aneka Thread Programming model and
Conventional Thread and Understand the differences?
Procedure
Follow the steps mentioned from 1 to 4 of mentioned in the procedure of the previous Question
Source code
using System;
using System.Collections.Generic;
using System.Text;
using Aneka;
using Aneka.Threading;
using Aneka.Entity;
using System.Threading;
namespace AnekaThreadPractise1
{
[Serializable]
public class HelloWorld
{
public string result;
public HelloWorld()
{
}
public void PrintHello()
{
Console.WriteLine("inside printHello : HelloWorld");
result = "HelloWorld";
}
}
class Program
{
static void Main(string[] args)
{
AnekaApplication<AnekaThread, ThreadManager> app = null;
try
{
4
Logger.Start();
Configuration conf =
Configuration.GetConfiguration(@"C:\Users\raghav\Documents\
CloudComputing-Lectures\conf.xml");
app = new AnekaApplication<AnekaThread,
ThreadManager>(conf);
HelloWorld hw = new HelloWorld();
}
finally
{
app.StopExecution();
Logger.Stop();
}
}
}
}
Ouput :
Value : HelloWorld
inside printHello : HelloWorld
Qn : Write a program to print “Hello World” using Aneka Thread Programming model use Five
Threads , also print the NodeIds on which the threads are executed and submission time and
Completion Time of the Threads?
Procedure
1.Followthe steps mentioned from 1 to 4 of mentioned in the procedure of the previous Question
Source code
using System;
using System.Collections.Generic;
using System.Text;
using Aneka;
using Aneka.Threading;
using Aneka.Entity;
using System.Threading;
namespace AnekaThreadsfive
{
[Serializable]
public class HelloWorld
{
public string result;
public HelloWorld()
{
}
public void PrintHello()
5
{
result = "HelloWorld";
}
}
class Program
{
static void Main(string[] args)
{
AnekaApplication<AnekaThread, ThreadManager> app = null;
try
{
Logger.Start();
Configuration conf = Configuration.GetConfiguration(@"C:\Users\
raghav\Documents\CloudComputing-Lectures\conf.xml");
app = new AnekaApplication<AnekaThread, ThreadManager>(conf);
HelloWorld hw = new HelloWorld();
AnekaThread[] th = new AnekaThread[5];
for (int i = 0; i < 5; i++)
{
th[i] = new AnekaThread(hw.PrintHello, app);
th[i].Start();
}
for (int i = 0; i < 5; i++)
{
th[i].Join();
hw = (HelloWorld)th[i].Target;
Console.WriteLine("Value : {0} , NodeId:{1},Submission Time:
{2},Completion Time{3}", hw.result,
th[i].NodeId,th[i].SubmissionTime,th[i].CompletionTime);
}
}
finally
{
app.StopExecution();
Logger.Stop();
}
}
}
}
output
Value : HelloWorld , NodeId:raghav-PC-9090,Submission Time:20-Nov-12 11:19:45 PM,Completion
Time20-Nov-12 11:19:45 PM
Value : HelloWorld , NodeId:raghav-PC-9090,Submission Time:20-Nov-12 11:19:45 PM,Completion
Time20-Nov-12 11:19:45 PM
Value : HelloWorld , NodeId:raghav-PC-9090,Submission Time:20-Nov-12 11:19:45 PM,Completion
Time20-Nov-12 11:19:45 PM
Value : HelloWorld , NodeId:raghav-PC-9090,Submission Time:20-Nov-12 11:19:45 PM,Completion
Time20-Nov-12 11:19:45 PM
Value : HelloWorld , NodeId:raghav-PC-9090,Submission Time:20-Nov-12 11:19:45 PM,Completion
Time20-Nov-12 11:19:45 PM
Qn : Write a program to compute the following mathematical equation using Aneka Threads(Note: Consider
each trigonometric function in independent thread)?
6
P= sin(x) + cos(y) + tan(z)
Procedure
using System;
using System.Collections.Generic;
using System.Text;
using Aneka;
using Aneka.Threading;
using Aneka.Entity;
namespace AnekaThreadTrigonometric
{
[Serializable]
public class Sine
{
double x;
public double result;
public Sine(double x)
{
this.x = x;
}
public void sineCompute()
{
result = System.Math.Sin(x*System.Math.PI/180);
}
}
[Serializable]
public class Cosine
{
double x;
public double result;
public Cosine(double x)
{
this.x = x;
}
public void cosineCompute()
{
result = System.Math.Cos(x * System.Math.PI / 180);
}
}
[Serializable]
public class Tangent
{
double x;
public double result;
public Tangent(double x)
{
this.x = x;
}
public void tangentCompute()
{
result = System.Math.Tan(x * System.Math.PI / 180);
}
}
class Program
{
static void Main(string[] args)
{
AnekaApplication<AnekaThread, ThreadManager> app = null;
7
try
{
Logger.Start();
Configuration conf =
Configuration.GetConfiguration(@"C:\Users\raghav\Documents\
CloudComputing-Lectures\conf.xml");
app = new AnekaApplication<AnekaThread,
ThreadManager>(conf);
}
finally
{
app.StopExecution();
Logger.Stop();
}
}
}
}
Output
P = sin(10)+cos(20)+tan(20) = 1.47731103271904
Task Model
Qn : Write a program to print “Hello World” using Aneka Task Programming model ?
Procedure
Follow the below mentioned steps to create the project and the necessary references
5. Creating a C# Console Application in .NET 2.0
a. Go to File -> New -> Project.
b. From New Project Dialog select visual C# - > windows .
c. Select .NET 2.0 and Console Application.
d. Enter the Solution Name and press OK.
6. Add References to the project from the following folders under Solution Explorer
8
a. Right Click on the references
i. Click on Add Reference
ii. Click on Browse Tab
iii. Select the following folders one after the other and add them
C:\Program Files\Manjrasoft\Aneka.3.0\Tools\SDK\Common
C:\Program Files\Manjrasoft\Aneka.3.0\Tools\SDK\Runtime
C:\Program Files\Manjrasoft\Aneka.3.0\Tools\SDK\TaskModel
7. Place the conf.xml in the folder and mention the same path in the code.
8. Add the below mentioned code to .CS file by replacing the code in the appropriate place
Program
using System;
using System.Collections.Generic;
using System.Text;
using Aneka;
using Aneka.Tasks;
using Aneka.Entity;
using System.Threading;
namespace AnekaTaskDemo
{
[Serializable]
public class HelloTask : ITask
{
public string result;
public void Execute()
{
result = "Hello";
Console.WriteLine("Result is set : "+result);
Console.WriteLine("HelloWorld");
}
}
class Program
{
static AutoResetEvent semaphore = null;
static AnekaApplication<AnekaTask, TaskManager> app = null;
static void Main(string[] args)
{
try
{
Logger.Start();
semaphore = new AutoResetEvent(false);
Configuration conf = Configuration.GetConfiguration(@"C:\Users\
raghav\Documents\CloudComputing-Lectures\conf.xml");
// conf.SingleSubmission = false;
app = new AnekaApplication<AnekaTask, TaskManager>(conf);
app.WorkUnitFailed += new
EventHandler<WorkUnitEventArgs<AnekaTask>>(app_WorkUnitFailed);
app.WorkUnitFinished += new
EventHandler<WorkUnitEventArgs<AnekaTask>>(app_WorkUnitFinished);
app.ApplicationFinished += new
EventHandler<ApplicationEventArgs>(app_ApplicationFinished);
HelloTask ht = new HelloTask();
AnekaTask gt = new AnekaTask(ht);
app.ExecuteWorkUnit(gt);
semaphore.WaitOne();
9
}
finally
{
Logger.Stop();
}
}
}
}
}
Qn : Write a program to sum the two numbers using Aneka Task Programming model ?
Program
using System;
using System.Collections.Generic;
using System.Text;
using Aneka;
using Aneka.Entity;
using Aneka.Tasks;
using System.Threading;
namespace AnekaTaskPractise
{
[Serializable]
public class MyTask : ITask
{
public int a, b;
public int sum;
public MyTask(int a, int b) { this.a = a; this.b = b; }
public void Execute()
{
Console.WriteLine("Inside Execute");
sum = a + b;
}
}
class Program
10
{
static AutoResetEvent semaphore = null;
static AnekaApplication<AnekaTask, TaskManager> app = null;
static void Main(string[] args)
{
Configuration conf=null;
AnekaTask gt = null;
try
{
Logger.Start();
semaphore = new AutoResetEvent(false);
conf = Configuration.GetConfiguration(@"C:\Users\raghav\
Documents\CloudComputing-Lectures\conf.xml");
conf.SingleSubmission = false;
app = new AnekaApplication<AnekaTask, TaskManager>(conf);
app.WorkUnitFailed += new
EventHandler<WorkUnitEventArgs<AnekaTask>>(app_WorkUnitFailed);
app.WorkUnitFinished += new
EventHandler<WorkUnitEventArgs<AnekaTask>>(app_WorkUnitFinished);
app.ApplicationFinished += new
EventHandler<ApplicationEventArgs>(app_ApplicationFinished);
MyTask task = new MyTask(10,20);
gt = new AnekaTask(task);
app.ExecuteWorkUnit(gt);
semaphore.WaitOne();
}
finally
{
Logger.Stop();
}
static void app_ApplicationFinished(object sender, ApplicationEventArgs
e)
{
semaphore.Set();
}
}
}
}
11