Implement given scenario in C#
Question 1: Implement given scenario in C#
Suppose that you have n number of printers and n number of
processes to waiting in a queue to print. You are required to write a code
where a process will give a command for pages to be print and its command is
sent to a free printer. If no printer is free request a queue must be
maintained for each printer.
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Threading;
namespace PDCAssignment2Q1
{
class Task
{
public int pages { get; set; }
public String name { get; set; }
}
class printer
{
public int pid { set; get; }
public void print(int pages)
{
String name =
Thread.CurrentThread.Name;
lock (this)
{
for (int i = 0; i
< pages; i++)
{
Console.WriteLine(pid+"is printing Page" + (i+1) + "of Task" + name);
Thread.Sleep(1000);
}
Console.WriteLine(pid + "Succesfully print pages ,total pages " +pages+ "of Task");
}
}
}
class Program
{
static void Main(string[] args)
{
Random r = new Random();
List<printer> printerList = new List<printer>();
int p = r.Next(1, 50);
int t = r.Next(1, 100);
for (int i = 0; i
< p; i++)
{
printerList.Add(new printer() { pid=(1+i)});
}
List<Task> TASkPagePrintList
= new List<Task>();
for (int i = 0; i
< t; i++)
{
TASkPagePrintList.Add(new Task() { name = "task" + (i + 1), pages =
r.Next(1, 70) });
}
int pindex = 0;
for (int i = 0; i
< TASkPagePrintList.Count-1; i++)
{
printer pt =
printerList[pindex];
pindex += 1;
if (pindex == printerList.Count)
{
Console.WriteLine(pindex + "&" + printerList.Count);
pindex = 0;
}
Thread.Sleep(1000);
Thread trd = new Thread(() =>
pt.print(TASkPagePrintList[i].pages));
trd.Start();
}
}
}
}
Question 2:
You
are required to add a load balancer for the program in Question 1. Job of load
balancer is to send a request to a printer that is free at time or will get
free soon. For example, if there are three printer P1, P2 and P3. P1 is busy in
printing 100 pages P2 is busy in printing 150 pages and P3 is busy in printing
10 pages if a new request is given to print 5 pages load balancer will pass
this to P3 and if another request is for printing 3 Pages load balancer will
again pass it to P3.
Note:
Each printer prints 2 Pages per second.
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Threading;
using
System.Linq;
namespace PDCAssignmentQ2
{
class Task
{
public int pages { get; set; }
public String name { get; set; }
}
class printer
{
public List<int>
PagesList = new List<int>();
public int pid { set; get; }
public bool isfree=true;
public int pageleft
{ get; set; }
public void print(int pages)
{
String name =
Thread.CurrentThread.Name;
PagesList.Add(pages);
lock (this)
{
isfree = false;
for (int i = 0; i
< PagesList[0]; i++)
{
Console.WriteLine(pid + "is printing Page" + (i + 1) + "of Task" + name);
Thread.Sleep(1000);
pageleft = pages - (i + 1);
}
Console.WriteLine(pid + "Succesfully print pages ,total pages " + PagesList[0] + " of Task");
PagesList.Remove(0);
isfree = true;
}
}
}
class Program
{
static void Main(string[] args)
{
Random r = new Random();
List<printer> printerList = new List<printer>();
int p = r.Next(2, 7);
int t = r.Next(2, 50);
for (int i = 0; i
< p; i++)
{
// Console.WriteLine("ppppp" + (i +
1));
printerList.Add(new printer() { pid = (1 + i) });
}
List<Task> TASkPagePrintList
= new List<Task>();
for (int i = 0; i
< t; i++)
{
Task task1 = new Task();
// Console.WriteLine("tttt" + (i +
1));
task1.name = "Task " + (i + 1);
task1.pages=r.Next(1, 10);
//
Console.WriteLine("*******"+t);
TASkPagePrintList.Add(task1 /*new Task() { name = "task" + (i + 1),
PagesAddList.Add(10) }*/);
}
for (int i = 0; i
< TASkPagePrintList.Count; i++)
{
printer pt =
printerList.Where(mm => mm.isfree).FirstOrDefault();
if (pt == null)
{
pt =printerList.OrderBy(rr=>
rr.pageleft +rr.PagesList.Sum()).FirstOrDefault();
Console.WriteLine(pt.pid + " Page Left " + pt.pageleft
+ " Page Sum " +
pt.PagesList.Sum());
}
int m = TASkPagePrintList[i].pages;
Thread trd = new Thread(() => pt.print(m));
trd.Name =
TASkPagePrintList[i].name;
//TASkPagePrintList[i].PagesList.Remove(0);
trd.Start();
}
Console.ReadLine();
}
}
}
Comments
Post a Comment