diff --git a/Network Lab/HTTP Web Server/CS348-lab3.pdf b/Network Lab/HTTP Web Server/CS348-lab3.pdf new file mode 100644 index 0000000..2f51d04 Binary files /dev/null and b/Network Lab/HTTP Web Server/CS348-lab3.pdf differ diff --git a/Network Lab/HTTP Web Server/Code&File/Config.config b/Network Lab/HTTP Web Server/Code&File/Config.config new file mode 100644 index 0000000..1f74e0b --- /dev/null +++ b/Network Lab/HTTP Web Server/Code&File/Config.config @@ -0,0 +1,4 @@ +#Webserver Config File +max_concurrent_thread=3 +blockIp=172.16.191.125,172.16.191.20,172.16.191.33,172.16.186.33 +defaultFile=./index.html diff --git a/Network Lab/HTTP Web Server/Code&File/HelloWorld.html b/Network Lab/HTTP Web Server/Code&File/HelloWorld.html new file mode 100644 index 0000000..479089a --- /dev/null +++ b/Network Lab/HTTP Web Server/Code&File/HelloWorld.html @@ -0,0 +1,6 @@ + +
This is title
+ +

Hello world

+ + diff --git a/Network Lab/HTTP Web Server/Code&File/HttpServer.class b/Network Lab/HTTP Web Server/Code&File/HttpServer.class new file mode 100644 index 0000000..55df7a6 Binary files /dev/null and b/Network Lab/HTTP Web Server/Code&File/HttpServer.class differ diff --git a/Network Lab/HTTP Web Server/Code&File/HttpServer.java b/Network Lab/HTTP Web Server/Code&File/HttpServer.java new file mode 100644 index 0000000..e0e74e5 --- /dev/null +++ b/Network Lab/HTTP Web Server/Code&File/HttpServer.java @@ -0,0 +1,112 @@ +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Scanner; + +public class HttpServer +{ + public static void main(String[] args) + { + ServerSocket server = null; + int port = 1234; + Scanner in = new Scanner(System.in); + System.out.println("Press any key to start the server"); + in.next(); + try + { + server = new ServerSocket(port); + System.out.println("Server is started at port "+port); + } + catch (IOException e) + { + // TODO Auto-generated catch block + System.out.println("Error: Server with port "+port +" cannot be created"); + e.printStackTrace(); + return; + } + + while(true) + { + Socket client = null; + try + { + client = server.accept(); + String clientIp = client.getRemoteSocketAddress().toString(); + clientIp = clientIp.substring(1, clientIp.indexOf(':')); + System.out.println("client request accepted at port "+port +" and ClienIp is "+clientIp); + } + catch (IOException e) + { + // TODO Auto-generated catch block + System.out.println("ERROR : client request can't be accepted at port "+port); + e.printStackTrace(); + return; + } + + try { + processClientHtmlRequest(client); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + private static void processClientHtmlRequest(Socket client) throws IOException + { + // TODO Auto-generated method stub + DataOutputStream printStream = new DataOutputStream(client.getOutputStream()); + InputStreamReader inputReader = new InputStreamReader(client.getInputStream()); + BufferedReader bufferReader = new BufferedReader(inputReader); + + String msg = bufferReader.readLine(); + System.out.print("Request from client :" + msg+"\n"); + String[] tokens = msg.split("[ /]"); + String data = ""; + if(tokens[2].equals("HttpServer.java") || tokens[2].equals("MultiThreadedHttpServer.java")) + { + tokens[2] = "xyz"; + } + FileInputStream fi = null; + try { + String val =""+ System.getProperty("user.dir"); + //System.out.println(val); + File file = new File(val+"/"+tokens[2]); + fi= new FileInputStream(file); + } catch (Exception e) { + // TODO: handle exception + System.out.println("no file"); + printStream.write(("HTTP/1.0 404 Not Found\r\n\r\n").getBytes()); + printStream.close(); + inputReader.close(); + bufferReader.close(); + System.out.println("connection close for this client\n"); + return; + } + printStream.write(("HTTP/1.1 200 OK\r\n").getBytes()); + if(tokens[2].contains("jpg")) + printStream.write(("Content-Type: image/jpeg\r\n\r\n").getBytes()); + else + printStream.write(("Content-Type: text/html\r\n\r\n").getBytes()); + //printStream.println("Content-Length: " + data.length()); + int totalByte; + byte[] buffer = new byte[2048]; + while((totalByte = fi.read(buffer))!=-1) + { + printStream.write(buffer,0,totalByte); + } + printStream.flush(); + printStream.close(); + inputReader.close(); + bufferReader.close(); + System.out.println("connection close for this client\n"); + } +} + diff --git a/Network Lab/HTTP Web Server/Code&File/MultiThreadedHttpServer.class b/Network Lab/HTTP Web Server/Code&File/MultiThreadedHttpServer.class new file mode 100644 index 0000000..2e7ba97 Binary files /dev/null and b/Network Lab/HTTP Web Server/Code&File/MultiThreadedHttpServer.class differ diff --git a/Network Lab/HTTP Web Server/Code&File/MultiThreadedHttpServer.java b/Network Lab/HTTP Web Server/Code&File/MultiThreadedHttpServer.java new file mode 100644 index 0000000..21b8224 --- /dev/null +++ b/Network Lab/HTTP Web Server/Code&File/MultiThreadedHttpServer.java @@ -0,0 +1,231 @@ +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Properties; +import java.util.Scanner; +import java.util.Date; + + + +class htmlRequestHandler implements Runnable +{ + private Socket client ; + private String serverText; + + //constructor + htmlRequestHandler(Socket client,String serverText) + { + this.client = client; + this.serverText = serverText; + } + public htmlRequestHandler() { + + } + + //overriding run + public void run() + { + try + { + //Increasing active thread count by 1 + MultiThreadedHttpServer.threadCount++; + DataOutputStream printStream = new DataOutputStream(client.getOutputStream()); + InputStreamReader inputReader = new InputStreamReader(client.getInputStream()); + BufferedReader bufferReader = new BufferedReader(inputReader); + + //Reading Request from client + String msg = bufferReader.readLine(); + System.out.print("Request message : "+msg + "\n"); + String[] tokens = msg.split("[ /]"); + if(tokens[2].length() == 0) + { + tokens[2] = this.serverText; + } + if(tokens[2].equals("HttpServer.java") || tokens[2].equals("MultiThreadedHttpServer.java")) + { + tokens[2] = "xyz"; + } + String data = ""; + FileInputStream fi = null; + try + { + String val =""+ System.getProperty("user.dir"); + File file = new File(val+"/"+tokens[2]); + fi= new FileInputStream(file); + } + catch (Exception e) + { + System.out.println("no file"); + printStream.write(("HTTP/1.1 404 Not Found\r\n\r\n").getBytes()); + printStream.close(); + inputReader.close(); + bufferReader.close(); + Thread.sleep(10000); + MultiThreadedHttpServer.threadCount--; + return; + } + printStream.write(("HTTP/1.1 200 OK\r\n").getBytes()); + if(tokens[2].contains("jpg")) + printStream.write(("Content-Type: image/jpeg\r\n\r\n").getBytes()); + else + printStream.write(("Content-Type: text/html\r\n\r\n").getBytes()); + //printStream.write(("Content-Length: " + data.length()+"\n\n").getBytes());; + int totalByte; + byte[] buffer = new byte[2048]; + while((totalByte = fi.read(buffer))!=-1) + { + printStream.write(buffer,0,totalByte); + } + printStream.flush(); + printStream.close(); + inputReader.close(); + bufferReader.close(); + client.close(); + //make thread sleep for 10s so that we can see effect of max allowed thread + Thread.sleep(10000); + MultiThreadedHttpServer.threadCount--; + } + catch (Exception e) + { + // TODO: handle exception + } + } +} + +public class MultiThreadedHttpServer +{ + public static int threadCount = 0; + public static void main(String[] args) + { + ServerSocket server = null; + int port = 1234; + Scanner in = new Scanner(System.in); + Properties prop = new Properties(); + + //loading Config.config file + String val =""+ System.getProperty("user.dir"); + String configFile = val+"/Config.config"; + try + { + InputStream is = new FileInputStream(configFile); + prop.load(is); + } + catch (FileNotFoundException e1) + { + System.out.println("Config File not found"); + e1.printStackTrace(); + return; + } + catch (IOException e) + { + e.printStackTrace(); + return; + } + + System.out.println("Press any key to start the server........"); + in.next(); + try + { + //ServerSocket at port 1234 created + server = new ServerSocket(port); + System.out.println("Server is started at port "+port); + } + catch (IOException e) + { + System.out.println("Error: Server with port "+port +" cannot be created"); + e.printStackTrace(); + return; + } + + while(true) + { + Socket client = null; + try + { + client = server.accept(); + System.out.println("client request accepted at port "+port); + } + catch (IOException e) + { + System.out.println("ERROR : client request can't be accepted at port "+port); + e.printStackTrace(); + return; + } + String ip = prop.getProperty("blockIp"); + String[] ipTokens = ip.split(","); + String clientIp = client.getRemoteSocketAddress().toString(); + clientIp = clientIp.substring(1, clientIp.indexOf(':')); + System.out.println("Client ip is : "+clientIp); + boolean block = false; + + //Checking if Client ip is block + for(int i=0;i +
Index.html(default)
+ +

Available File

+

1.HelloWorld.html

+

2.messi.jpg

+

3.TCP.txt

+ + diff --git a/Network Lab/HTTP Web Server/Code&File/messi.jpg b/Network Lab/HTTP Web Server/Code&File/messi.jpg new file mode 100644 index 0000000..1e00e88 Binary files /dev/null and b/Network Lab/HTTP Web Server/Code&File/messi.jpg differ diff --git a/Network Lab/HTTP Web Server/ScreenShot/IP_Guest_Host_ScreenShot.png b/Network Lab/HTTP Web Server/ScreenShot/IP_Guest_Host_ScreenShot.png new file mode 100644 index 0000000..0978a84 Binary files /dev/null and b/Network Lab/HTTP Web Server/ScreenShot/IP_Guest_Host_ScreenShot.png differ diff --git a/Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_1_HelloWorld.png b/Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_1_HelloWorld.png new file mode 100644 index 0000000..a733bf8 Binary files /dev/null and b/Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_1_HelloWorld.png differ diff --git a/Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_5_ayush.jpg.png b/Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_5_ayush.jpg.png new file mode 100644 index 0000000..00771c0 Binary files /dev/null and b/Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_5_ayush.jpg.png differ diff --git a/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/DefaultPage_demo_ScreenShot.png b/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/DefaultPage_demo_ScreenShot.png new file mode 100644 index 0000000..0b1d3bf Binary files /dev/null and b/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/DefaultPage_demo_ScreenShot.png differ diff --git a/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/IpBlock_demo_ScreenShot.png b/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/IpBlock_demo_ScreenShot.png new file mode 100644 index 0000000..c7440b8 Binary files /dev/null and b/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/IpBlock_demo_ScreenShot.png differ diff --git a/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/ThreadLimit_demo_ScreenShot.png b/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/ThreadLimit_demo_ScreenShot.png new file mode 100644 index 0000000..0ae9a0d Binary files /dev/null and b/Network Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/ThreadLimit_demo_ScreenShot.png differ diff --git a/Network Lab/Mininet Basic Topology/CS348-lab5.pdf b/Network Lab/Mininet Basic Topology/CS348-lab5.pdf new file mode 100644 index 0000000..8cb9d1e Binary files /dev/null and b/Network Lab/Mininet Basic Topology/CS348-lab5.pdf differ diff --git a/Network Lab/Mininet Basic Topology/Q1.py b/Network Lab/Mininet Basic Topology/Q1.py new file mode 100644 index 0000000..6b4bc6c --- /dev/null +++ b/Network Lab/Mininet Basic Topology/Q1.py @@ -0,0 +1,53 @@ +#!/usr/bin/python + +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.util import dumpNodeConnections +from mininet.log import setLogLevel +#from threading import Thread +import thread +from time import sleep +from mininet.cli import CLI + +class CreateTopo(Topo): + def build(self,n): + switch = self.addSwitch('s1') + host1 = self.addHost('h1') + host2 = self.addHost('h2') + self.addLink(host1,switch) + self.addLink(host2,switch) + +def server(net): + h1 = net.get('h1') + h1.cmd('iperf -s -p 5111 -i 2 > throughput.txt') + +def twoHostSystem(): + topo = CreateTopo(n=2) + net = Mininet(topo=topo) + net.start() + #CLI(net) + #dumpNodeConnections(net.hosts) + h1 = net.get('h1') + h2 = net.get('h2') + thread.start_new_thread( server, (net, ) ) + h2.cmd('iperf -c 10.0.0.1 -p 5111 -t 20') + #result = h2.cmd('cat throughput.txt|tr "-" " "|grep "sec" | awk "{print $4,$8}"') + #print(result) + + +if __name__ == '__main__': + setLogLevel( 'info' ) + twoHostSystem() + cnt=0 + print("Required Throughput") + print("Time Interval Throughput") + with open("throughput.txt") as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(6,len(contents)): + line = contents[i].split() + if i<=9: + print("[%s %s %s] ---> %s %s" %(line[2],line[3],line[4],line[7],line[8])) + else: + print("[%s %s] ---> %s %s " %(line[2],line[3],line[4],line[7])) + diff --git a/Network Lab/Mininet Basic Topology/Q1.py~ b/Network Lab/Mininet Basic Topology/Q1.py~ new file mode 100644 index 0000000..6b4bc6c --- /dev/null +++ b/Network Lab/Mininet Basic Topology/Q1.py~ @@ -0,0 +1,53 @@ +#!/usr/bin/python + +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.util import dumpNodeConnections +from mininet.log import setLogLevel +#from threading import Thread +import thread +from time import sleep +from mininet.cli import CLI + +class CreateTopo(Topo): + def build(self,n): + switch = self.addSwitch('s1') + host1 = self.addHost('h1') + host2 = self.addHost('h2') + self.addLink(host1,switch) + self.addLink(host2,switch) + +def server(net): + h1 = net.get('h1') + h1.cmd('iperf -s -p 5111 -i 2 > throughput.txt') + +def twoHostSystem(): + topo = CreateTopo(n=2) + net = Mininet(topo=topo) + net.start() + #CLI(net) + #dumpNodeConnections(net.hosts) + h1 = net.get('h1') + h2 = net.get('h2') + thread.start_new_thread( server, (net, ) ) + h2.cmd('iperf -c 10.0.0.1 -p 5111 -t 20') + #result = h2.cmd('cat throughput.txt|tr "-" " "|grep "sec" | awk "{print $4,$8}"') + #print(result) + + +if __name__ == '__main__': + setLogLevel( 'info' ) + twoHostSystem() + cnt=0 + print("Required Throughput") + print("Time Interval Throughput") + with open("throughput.txt") as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(6,len(contents)): + line = contents[i].split() + if i<=9: + print("[%s %s %s] ---> %s %s" %(line[2],line[3],line[4],line[7],line[8])) + else: + print("[%s %s] ---> %s %s " %(line[2],line[3],line[4],line[7])) + diff --git a/Network Lab/Mininet Basic Topology/Q2.py b/Network Lab/Mininet Basic Topology/Q2.py new file mode 100644 index 0000000..6e673a7 --- /dev/null +++ b/Network Lab/Mininet Basic Topology/Q2.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.util import dumpNodeConnections +from mininet.log import setLogLevel +import thread +from time import sleep +from mininet.cli import CLI +from mininet.link import TCLink + +class CreateTopo(Topo): + def build(self,n): + switch1 = self.addSwitch('s1'); + switch2 = self.addSwitch('t1'); + for i in range(n): + host = self.addHost(chr(ord('a') + i)) + if i == 0 or i == 1 or i == 4: + self.addLink(host,switch1,bw=5, delay='3ms', loss=2,max_queue_size=300) + else: + self.addLink(host,switch2,bw=5, delay='3ms', loss=2,max_queue_size=300) + host = self.addHost('u') + self.addLink(host,switch2,bw=5, delay='3ms', loss=2,max_queue_size=300) + self.addLink(switch1,switch2,bw=15, delay='2ms') + +def customTopo(): + topo = CreateTopo(n=5) + net = Mininet(topo=topo,link=TCLink) + net.start() + #CLI(net) + print("Detailed Connections :") + dumpNodeConnections(net.hosts) + + print("Ping Result to all nodes :") + net.pingAll() + net.stop() + + + +if __name__ == '__main__': + setLogLevel( 'info' ) + customTopo() + diff --git a/Network Lab/Mininet Basic Topology/Q2.py~ b/Network Lab/Mininet Basic Topology/Q2.py~ new file mode 100644 index 0000000..6e673a7 --- /dev/null +++ b/Network Lab/Mininet Basic Topology/Q2.py~ @@ -0,0 +1,43 @@ +#!/usr/bin/python + +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.util import dumpNodeConnections +from mininet.log import setLogLevel +import thread +from time import sleep +from mininet.cli import CLI +from mininet.link import TCLink + +class CreateTopo(Topo): + def build(self,n): + switch1 = self.addSwitch('s1'); + switch2 = self.addSwitch('t1'); + for i in range(n): + host = self.addHost(chr(ord('a') + i)) + if i == 0 or i == 1 or i == 4: + self.addLink(host,switch1,bw=5, delay='3ms', loss=2,max_queue_size=300) + else: + self.addLink(host,switch2,bw=5, delay='3ms', loss=2,max_queue_size=300) + host = self.addHost('u') + self.addLink(host,switch2,bw=5, delay='3ms', loss=2,max_queue_size=300) + self.addLink(switch1,switch2,bw=15, delay='2ms') + +def customTopo(): + topo = CreateTopo(n=5) + net = Mininet(topo=topo,link=TCLink) + net.start() + #CLI(net) + print("Detailed Connections :") + dumpNodeConnections(net.hosts) + + print("Ping Result to all nodes :") + net.pingAll() + net.stop() + + + +if __name__ == '__main__': + setLogLevel( 'info' ) + customTopo() + diff --git a/Network Lab/Mininet Basic Topology/Q3.py b/Network Lab/Mininet Basic Topology/Q3.py new file mode 100644 index 0000000..54a62c2 --- /dev/null +++ b/Network Lab/Mininet Basic Topology/Q3.py @@ -0,0 +1,88 @@ +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.nodelib import NAT +from mininet.log import setLogLevel +from mininet.cli import CLI +from mininet.util import dumpNodeConnections +from mininet.util import irange +import thread +from time import sleep +import mininet.node + +class InternetTopo(Topo): + "Single switch connected to n hosts." + def __init__(self, n=2, **opts): + Topo.__init__(self, **opts) + + + # set up inet switch + switchS1 = self.addSwitch('s1') + switchS2 = self.addSwitch('s2') + for i in range(2): + host = self.addHost(chr(ord('s') + i)) + self.addLink(switchS2,host) + + publicIntf = "nat-eht2" + privateIntf = "nat-eht1" + localIP = '192.168.0.1' + localSubnet = '192.168.0.0/24' + natParams = { 'ip' : '192.168.0.1/24' } + nat = self.addNode('nat',cls=NAT,subnet=localSubnet,inetIntf=publicIntf, localIntf=privateIntf) + self.addLink(nat,switchS2,intfName1=publicIntf) + self.addLink(nat,switchS1,intfName1=privateIntf, params1=natParams) + + for i in range(1,4): + host = self.addHost('h%d'%i,ip='192.168.0.10%d/24' % i,defaultRoute='via 192.168.0.1') + self.addLink(host,switchS1) + +def server(net): + publicServer = net.get('s') + print("ip of server s is %s" % publicServer.IP()) + publicServer.cmd('tcpdump -i s-eth0 > connection.txt') + +def server1(net): + publicServer = net.get('t') + print("ip of server t is %s" % publicServer.IP()) + publicServer.cmd('tcpdump -i t-eth0 > connection1.txt') + +def run(): + topo = InternetTopo() + net = Mininet(topo=topo) + net.start() + nat = net.get('nat') + print("Detailed Connections :") + dumpNodeConnections(net.hosts) + print("Nat ip of public interface i.e eth2 is %s"%nat.IP()) + print("Nat ip of private interface is : 192.168.0.1") + + CLI(net) + privateHost = net.get('h1') + thread.start_new_thread( server, (net, ) ) + thread.start_new_thread( server1, (net, ) ) + for i in range(1,4): + host = net.get('h%d'%i) + print("Ip of node h%d is %s" % (i,host.IP())) + sIp = net.get('s').IP() + tIp = net.get('t').IP() + print("Node h%d is pinging server s" %i) + privateHost.cmd('ping -c3 %s'%sIp) + print("Node h%d is pinging server t" %i) + privateHost.cmd('ping -c3 %s'%tIp) + net.stop() + +if __name__ == '__main__': + setLogLevel('info') + run() + print("Network activity at server s") + with open("connection.txt") as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(len(contents)): + print(contents[i]) + + print("Network activity at server t :") + with open("connection1.txt") as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(len(contents)): + print(contents[i]) diff --git a/Network Lab/Mininet Basic Topology/Q3.py~ b/Network Lab/Mininet Basic Topology/Q3.py~ new file mode 100644 index 0000000..54a62c2 --- /dev/null +++ b/Network Lab/Mininet Basic Topology/Q3.py~ @@ -0,0 +1,88 @@ +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.nodelib import NAT +from mininet.log import setLogLevel +from mininet.cli import CLI +from mininet.util import dumpNodeConnections +from mininet.util import irange +import thread +from time import sleep +import mininet.node + +class InternetTopo(Topo): + "Single switch connected to n hosts." + def __init__(self, n=2, **opts): + Topo.__init__(self, **opts) + + + # set up inet switch + switchS1 = self.addSwitch('s1') + switchS2 = self.addSwitch('s2') + for i in range(2): + host = self.addHost(chr(ord('s') + i)) + self.addLink(switchS2,host) + + publicIntf = "nat-eht2" + privateIntf = "nat-eht1" + localIP = '192.168.0.1' + localSubnet = '192.168.0.0/24' + natParams = { 'ip' : '192.168.0.1/24' } + nat = self.addNode('nat',cls=NAT,subnet=localSubnet,inetIntf=publicIntf, localIntf=privateIntf) + self.addLink(nat,switchS2,intfName1=publicIntf) + self.addLink(nat,switchS1,intfName1=privateIntf, params1=natParams) + + for i in range(1,4): + host = self.addHost('h%d'%i,ip='192.168.0.10%d/24' % i,defaultRoute='via 192.168.0.1') + self.addLink(host,switchS1) + +def server(net): + publicServer = net.get('s') + print("ip of server s is %s" % publicServer.IP()) + publicServer.cmd('tcpdump -i s-eth0 > connection.txt') + +def server1(net): + publicServer = net.get('t') + print("ip of server t is %s" % publicServer.IP()) + publicServer.cmd('tcpdump -i t-eth0 > connection1.txt') + +def run(): + topo = InternetTopo() + net = Mininet(topo=topo) + net.start() + nat = net.get('nat') + print("Detailed Connections :") + dumpNodeConnections(net.hosts) + print("Nat ip of public interface i.e eth2 is %s"%nat.IP()) + print("Nat ip of private interface is : 192.168.0.1") + + CLI(net) + privateHost = net.get('h1') + thread.start_new_thread( server, (net, ) ) + thread.start_new_thread( server1, (net, ) ) + for i in range(1,4): + host = net.get('h%d'%i) + print("Ip of node h%d is %s" % (i,host.IP())) + sIp = net.get('s').IP() + tIp = net.get('t').IP() + print("Node h%d is pinging server s" %i) + privateHost.cmd('ping -c3 %s'%sIp) + print("Node h%d is pinging server t" %i) + privateHost.cmd('ping -c3 %s'%tIp) + net.stop() + +if __name__ == '__main__': + setLogLevel('info') + run() + print("Network activity at server s") + with open("connection.txt") as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(len(contents)): + print(contents[i]) + + print("Network activity at server t :") + with open("connection1.txt") as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(len(contents)): + print(contents[i]) diff --git a/Network Lab/Minininet With Forwarding Table/CS348-lab6.pdf b/Network Lab/Minininet With Forwarding Table/CS348-lab6.pdf new file mode 100644 index 0000000..831eded Binary files /dev/null and b/Network Lab/Minininet With Forwarding Table/CS348-lab6.pdf differ diff --git a/Network Lab/Minininet With Forwarding Table/Q1.py b/Network Lab/Minininet With Forwarding Table/Q1.py new file mode 100644 index 0000000..74af51e --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/Q1.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.nodelib import NAT +from mininet.log import setLogLevel +from mininet.cli import CLI +from mininet.util import dumpNodeConnections +from mininet.util import irange +import thread +from mininet.node import Node +from time import sleep +import mininet.node + + +class LinuxRouter( Node ): + "A Node with IP forwarding enabled." + + def config( self, **params ): + super( LinuxRouter, self).config( **params ) + # Enable forwarding on the router + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) + + def terminate( self ): + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + super( LinuxRouter, self ).terminate() + +class customTopo(Topo): + def build( self, **_opts ): + #R1 = self.addNode( 'r1', cls=LinuxRouter, ip=defaultIP ) + R1 = self.addNode( 'R1', cls=LinuxRouter, ip='10.0.1.1/24',defaultRoute='via 11.0.1.2') + R2 = self.addNode('R2',cls=LinuxRouter,ip='10.0.2.1/24',defaultRoute='via 11.0.4.2') + R3 = self.addNode('R3',cls=LinuxRouter,ip='10.0.3.1/24',defaultRoute='via 11.0.3.1') + R4 = self.addNode('R4',cls=LinuxRouter,ip='10.0.4.1/24',defaultRoute='via 11.0.2.1') + switch1 = self.addSwitch('s1') + switch2 = self.addSwitch('s2') + H1 = self.addHost('H1',ip='10.0.1.2/24',defaultRoute='via 10.0.1.1') + H7 = self.addHost('H7',ip='10.0.2.2/24',defaultRoute='via 10.0.2.1') + for i in range(2,5): + h1 = self.addHost('H%d'%i,ip='10.0.3.%d'%i,defaultRoute='via 10.0.3.1') + self.addLink(h1,switch1) + for i in range(5,7): + h1 = self.addHost('H%d'%i,ip = '10.0.4.%d'%i,defaultRoute='via 10.0.4.1') + self.addLink(h1,switch2) + + self.addLink(switch1,R3,intfName2='R3-eth0',params2={'ip':'10.0.3.1/24'}) + self.addLink(switch2,R4,intfName2='R4-eth0',params2={'ip':'10.0.4.1/24'}) + self.addLink(H1,R1,intfName2='R1-eth0',params2={'ip' : '10.0.1.1/24'}) + self.addLink(H7,R2,intfName2='R2-eth0',params2={'ip':'10.0.2.1/24'}) + + #connection among routers + self.addLink(R1,R2,intfName1='R1-eth1',intfName2='R2-eth1',params1={'ip':'11.0.1.1/24'},params2={'ip':'11.0.1.2/24'}) + self.addLink(R3,R4,intfName1='R3-eth1',intfName2='R4-eth1',params1={'ip':'11.0.2.1/24'},params2={'ip':'11.0.2.2/24'}) + self.addLink(R1,R3,intfName1='R1-eth2',intfName2='R3-eth2',params1={'ip':'11.0.3.1/24'},params2={'ip':'11.0.3.2/24'}) + self.addLink(R2,R4,intfName1='R2-eth2',intfName2='R4-eth2',params1={'ip':'11.0.4.1/24'},params2={'ip':'11.0.4.2/24'}) + +def configureR3subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.3.1') + +def configureR4subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.4.1') + +def configureRouter(routerNo,router): + with open("R{}_FwdingTbl.txt".format(routerNo)) as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(1,len(contents)): + routeInfo = contents[i] + routeInfo = routeInfo.replace('\t'," ") + routeInfo = routeInfo.split() + #print(routeInfo) + router.cmd('route add -net {} netmask {} gw {}'.format(routeInfo[0],routeInfo[2],routeInfo[1])) + + +def createTopo(): + topo = customTopo() + net = Mininet(topo=topo) + net.start() + #configure host + for i in range(2,7): + if(i<5): + configureR3subnetHost(net.get('H%i'%i)) + else: + configureR4subnetHost(net.get('H%i'%i)) + + #configure Router + for i in range(1,5): + print("configuring Router %i......."%i) + configureRouter(i,net.get('R%i'%i)) + CLI(net) + net.pingAll() + net.stop() + +if __name__ == '__main__': + setLogLevel('info') + createTopo() + diff --git a/Network Lab/Minininet With Forwarding Table/Q1.py~ b/Network Lab/Minininet With Forwarding Table/Q1.py~ new file mode 100644 index 0000000..74af51e --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/Q1.py~ @@ -0,0 +1,100 @@ +#!/usr/bin/python +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.nodelib import NAT +from mininet.log import setLogLevel +from mininet.cli import CLI +from mininet.util import dumpNodeConnections +from mininet.util import irange +import thread +from mininet.node import Node +from time import sleep +import mininet.node + + +class LinuxRouter( Node ): + "A Node with IP forwarding enabled." + + def config( self, **params ): + super( LinuxRouter, self).config( **params ) + # Enable forwarding on the router + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) + + def terminate( self ): + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + super( LinuxRouter, self ).terminate() + +class customTopo(Topo): + def build( self, **_opts ): + #R1 = self.addNode( 'r1', cls=LinuxRouter, ip=defaultIP ) + R1 = self.addNode( 'R1', cls=LinuxRouter, ip='10.0.1.1/24',defaultRoute='via 11.0.1.2') + R2 = self.addNode('R2',cls=LinuxRouter,ip='10.0.2.1/24',defaultRoute='via 11.0.4.2') + R3 = self.addNode('R3',cls=LinuxRouter,ip='10.0.3.1/24',defaultRoute='via 11.0.3.1') + R4 = self.addNode('R4',cls=LinuxRouter,ip='10.0.4.1/24',defaultRoute='via 11.0.2.1') + switch1 = self.addSwitch('s1') + switch2 = self.addSwitch('s2') + H1 = self.addHost('H1',ip='10.0.1.2/24',defaultRoute='via 10.0.1.1') + H7 = self.addHost('H7',ip='10.0.2.2/24',defaultRoute='via 10.0.2.1') + for i in range(2,5): + h1 = self.addHost('H%d'%i,ip='10.0.3.%d'%i,defaultRoute='via 10.0.3.1') + self.addLink(h1,switch1) + for i in range(5,7): + h1 = self.addHost('H%d'%i,ip = '10.0.4.%d'%i,defaultRoute='via 10.0.4.1') + self.addLink(h1,switch2) + + self.addLink(switch1,R3,intfName2='R3-eth0',params2={'ip':'10.0.3.1/24'}) + self.addLink(switch2,R4,intfName2='R4-eth0',params2={'ip':'10.0.4.1/24'}) + self.addLink(H1,R1,intfName2='R1-eth0',params2={'ip' : '10.0.1.1/24'}) + self.addLink(H7,R2,intfName2='R2-eth0',params2={'ip':'10.0.2.1/24'}) + + #connection among routers + self.addLink(R1,R2,intfName1='R1-eth1',intfName2='R2-eth1',params1={'ip':'11.0.1.1/24'},params2={'ip':'11.0.1.2/24'}) + self.addLink(R3,R4,intfName1='R3-eth1',intfName2='R4-eth1',params1={'ip':'11.0.2.1/24'},params2={'ip':'11.0.2.2/24'}) + self.addLink(R1,R3,intfName1='R1-eth2',intfName2='R3-eth2',params1={'ip':'11.0.3.1/24'},params2={'ip':'11.0.3.2/24'}) + self.addLink(R2,R4,intfName1='R2-eth2',intfName2='R4-eth2',params1={'ip':'11.0.4.1/24'},params2={'ip':'11.0.4.2/24'}) + +def configureR3subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.3.1') + +def configureR4subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.4.1') + +def configureRouter(routerNo,router): + with open("R{}_FwdingTbl.txt".format(routerNo)) as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(1,len(contents)): + routeInfo = contents[i] + routeInfo = routeInfo.replace('\t'," ") + routeInfo = routeInfo.split() + #print(routeInfo) + router.cmd('route add -net {} netmask {} gw {}'.format(routeInfo[0],routeInfo[2],routeInfo[1])) + + +def createTopo(): + topo = customTopo() + net = Mininet(topo=topo) + net.start() + #configure host + for i in range(2,7): + if(i<5): + configureR3subnetHost(net.get('H%i'%i)) + else: + configureR4subnetHost(net.get('H%i'%i)) + + #configure Router + for i in range(1,5): + print("configuring Router %i......."%i) + configureRouter(i,net.get('R%i'%i)) + CLI(net) + net.pingAll() + net.stop() + +if __name__ == '__main__': + setLogLevel('info') + createTopo() + diff --git a/Network Lab/Minininet With Forwarding Table/Q1_1.py~ b/Network Lab/Minininet With Forwarding Table/Q1_1.py~ new file mode 100644 index 0000000..e0a726e --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/Q1_1.py~ @@ -0,0 +1,99 @@ +#!/usr/bin/python +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.nodelib import NAT +from mininet.log import setLogLevel +from mininet.cli import CLI +from mininet.util import dumpNodeConnections +from mininet.util import irange +import thread +from mininet.node import Node +from time import sleep +import mininet.node + + +class LinuxRouter( Node ): + "A Node with IP forwarding enabled." + + def config( self, **params ): + super( LinuxRouter, self).config( **params ) + # Enable forwarding on the router + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) + + def terminate( self ): + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + super( LinuxRouter, self ).terminate() + +class customTopo(Topo): + def build( self, **_opts ): + #R1 = self.addNode( 'r1', cls=LinuxRouter, ip=defaultIP ) + R1 = self.addNode( 'R1', cls=LinuxRouter, ip='10.0.1.1/24',defaultRoute='via 11.0.1.2') + R2 = self.addNode('R2',cls=LinuxRouter,ip='10.0.2.1/24',defaultRoute='via 11.0.4.2') + R3 = self.addNode('R3',cls=LinuxRouter,ip='10.0.3.1/24',defaultRoute='via 11.0.3.1') + R4 = self.addNode('R4',cls=LinuxRouter,ip='10.0.4.1/24',defaultRoute='via 11.0.2.1') + switch1 = self.addSwitch('s1') + switch2 = self.addSwitch('s2') + H1 = self.addHost('H1',ip='10.0.1.2/24',defaultRoute='via 10.0.1.1') + H7 = self.addHost('H7',ip='10.0.2.2/24',defaultRoute='via 10.0.2.1') + for i in range(2,5): + h1 = self.addHost('H%d'%i,ip = '10.0.3.%d'%i,defaultRoute='via 10.0.3.1') + self.addLink(h1,switch2) + + for i in range(5,7): + h1 = self.addHost('H%d'%i,ip = '10.0.4.%d'%i,defaultRoute='via 10.0.4.1') + self.addLink(h1,switch2) + + self.addLink(switch1,R3,intfName2='R3-eth0',params2={'ip':'10.0.3.1/24'}) + self.addLink(switch2,R4,intfName2='R4-eth0',params2={'ip':'10.0.4.1/24'}) + self.addLink(H1,R1,intfName2='R1-eth0',params2={'ip' : '10.0.1.1/24'}) + self.addLink(H7,R2,intfName2='R2-eth0',params2={'ip':'10.0.2.1/24'}) + + #connection among routers + self.addLink(R1,R2,intfName1='R1-eth1',intfName2='R2-eth1',params1={'ip':'11.0.1.1/24'},params2={'ip':'11.0.1.2/24'}) + self.addLink(R3,R4,intfName1='R3-eth1',intfName2='R4-eth1',params1={'ip':'11.0.2.1/24'},params2={'ip':'11.0.2.2/24'}) + self.addLink(R1,R3,intfName1='R1-eth2',intfName2='R3-eth2',params1={'ip':'11.0.3.1/24'},params2={'ip':'11.0.3.2/24'}) + self.addLink(R2,R4,intfName1='R2-eth2',intfName2='R4-eth2',params1={'ip':'11.0.4.1/24'},params2={'ip':'11.0.4.2/24'}) + +def configureR3subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.3.1') + +def configureR4subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.4.1') + +def configureRouter(routerNo,router): + with open("R{}_FwdingTbl.txt".format(routerNo)) as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(1,len(contents)): + routeInfo = contents[i] + routeInfo = routeInfo.replace('\t'," ") + routeInfo = routeInfo.split() + #print(routeInfo) + router.cmd('route add -net {} netmask {} gw {}'.format(routeInfo[0],routeInfo[2],routeInfo[1])) + +def createTopo(): + topo = customTopo() + net = Mininet(topo=topo) + net.start() + #configure host + for i in range(2,7): + if(i<5): + configureR3subnetHost(net.get('H%i'%i)) + else: + configureR4subnetHost(net.get('H%i'%i)) + + #configure Router + for i in range(1,5): + print("configuring Router %i......."%i) + configureRouter(i,net.get('R%i'%i)) + CLI(net) + net.pingAll() + net.stop() + +if __name__ == '__main__': + setLogLevel('info') + createTopo() diff --git a/Network Lab/Minininet With Forwarding Table/Q1_old.py b/Network Lab/Minininet With Forwarding Table/Q1_old.py new file mode 100644 index 0000000..fefa9d5 --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/Q1_old.py @@ -0,0 +1,64 @@ +#!/usr/bin/python +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.nodelib import NAT +from mininet.log import setLogLevel +from mininet.cli import CLI +from mininet.util import dumpNodeConnections +from mininet.util import irange +import thread +from mininet.node import Node +from time import sleep +import mininet.node + +class LinuxRouter( Node ): + "A Node with IP forwarding enabled." + + def config( self, **params ): + super( LinuxRouter, self).config( **params ) + # Enable forwarding on the router + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) + + def terminate( self ): + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + super( LinuxRouter, self ).terminate() + +class customTopo(Topo): + def build( self, **_opts ): + R1 = self.addNode( 'R1', cls=LinuxRouter, ip='10.0.1.0/24') + R2 = self.addNode('R2',cls=LinuxRouter,ip='10.0.2.0/24') + R3 = self.addNode('R3',cls=LinuxRouter,ip='10.0.3.0/24') + R4 = self.addNode('R4',cls=LinuxRouter,ip='10.0.4.0/24') + + switch1 = self.addSwitch('s1') + switch2 = self.addSwitch('s2') + H1 = self.addHost('H1',ip='10.0.1.1/24',defaultRoute='via 10.0.1.0') + H7 = self.addHost('H7',ip='10.0.2.1/24',defaultRoute='via 10.0.2.0') + for i in range(2,5): + h1 = self.addHost('H%d'%i,ip='10.0.3.%d'%i,defaultRoute='via 10.0.3.0') + self.addLink(h1,switch1) + for i in range(5,7): + h1 = self.addHost('H%d'%i,ip = '10.0.4.%d'%i,defaultRoute='via 10.0.4.0') + self.addLink(h1,switch2) + + self.addLink(switch1,R3,intfName2='R3-eth0',params2={'ip':'10.0.3.0/24'}) + self.addLink(switch2,R4,intfName2='R4-eth0',params2={'ip':'10.0.4.0/24'}) + self.addLink(H1,R1,intfName2='R1-eth0',params2={'ip':'10.0.1.0/24'}) + self.addLink(H7,R2,intfName2='R2-eth0',params2={'ip':'10.0.2.0/24'}) + + #connection among routers + self.addLink(R1,R2,intfName1='R1-eth1',intfName2='R2-eth1',params1={'ip':'11.0.1.0/24'},params2={'ip':'11.0.1.1/24'}) + self.addLink(R3,R4,intfName1='R3-eth1',intfName2='R4-eth1',params1={'ip':'11.0.2.0/24'},params2={'ip':'11.0.2.1/24'}) + self.addLink(R1,R3,intfName1='R1-eth2',intfName2='R3-eth2',params1={'ip':'11.0.3.0/24'},params2={'ip':'11.0.3.1/24'}) + self.addLink(R2,R4,intfName1='R2-eth2',intfName2='R4-eth2',params1={'ip':'11.0.4.0/24'},params2={'ip':'11.0.4.1/24'}) + +def createTopo(): + topo = customTopo() + net = Mininet(topo=topo) + net.start() + CLI(net) + net.stop() + +if __name__ == '__main__': + setLogLevel('info') + createTopo() \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/R1_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/R1_FwdingTbl.txt new file mode 100644 index 0000000..c019ed4 --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/R1_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 10.0.1.1 255.255.255.0 R1-eth0 +10.0.2.0 11.0.1.2 255.255.255.0 R1-eth1 +10.0.3.0 11.0.3.2 255.255.255.0 R1-eth2 +10.0.4.0 11.0.1.2 255.255.255.0 R1-eth1 +11.0.1.0 11.0.1.1 255.255.255.0 R1-eth1 +11.0.2.0 11.0.1.2 255.255.255.0 R1-eth1 +11.0.3.0 11.0.3.1 255.255.255.0 R1-eth2 +11.0.4.0 11.0.1.2 255.255.255.0 R1-eth1 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/R2_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/R2_FwdingTbl.txt new file mode 100644 index 0000000..d7da1ec --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/R2_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 11.0.1.1 255.255.255.0 R2-eth1 +10.0.2.0 10.0.2.1 255.255.255.0 R2-eth0 +10.0.3.0 11.0.4.2 255.255.255.0 R2-eth2 +10.0.4.0 11.0.4.2 255.255.255.0 R2-eth2 +11.0.1.0 11.0.1.2 255.255.255.0 R2-eth1 +11.0.2.0 11.0.4.2 255.255.255.0 R2-eth2 +11.0.3.0 11.0.4.2 255.255.255.0 R2-eth2 +11.0.4.0 11.0.4.1 255.255.255.0 R2-eth2 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/R3_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/R3_FwdingTbl.txt new file mode 100644 index 0000000..a5dfb0e --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/R3_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 11.0.3.1 255.255.255.0 R3-eth2 +10.0.2.0 11.0.2.2 255.255.255.0 R3-eth1 +10.0.3.0 10.0.3.1 255.255.255.0 R3-eth0 +10.0.4.0 11.0.2.2 255.255.255.0 R3-eth1 +11.0.1.0 11.0.3.1 255.255.255.0 R3-eth2 +11.0.2.0 11.0.2.1 255.255.255.0 R3-eth1 +11.0.3.0 11.0.3.2 255.255.255.0 R3-eth2 +11.0.4.0 11.0.2.2 255.255.255.0 R3-eth1 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/R4_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/R4_FwdingTbl.txt new file mode 100644 index 0000000..0e5d92d --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/R4_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 11.0.4.1 255.255.255.0 R4-eth2 +10.0.2.0 11.0.4.1 255.255.255.0 R4-eth2 +10.0.3.0 11.0.2.1 255.255.255.0 R4-eth1 +10.0.4.0 10.0.4.1 255.255.255.0 R4-eth0 +11.0.1.0 11.0.4.1 255.255.255.0 R4-eth2 +11.0.2.0 11.0.2.2 255.255.255.0 R4-eth1 +11.0.3.0 11.0.2.1 255.255.255.0 R4-eth1 +11.0.4.0 11.0.4.2 255.255.255.0 R4-eth2 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/assign6/Q1.py b/Network Lab/Minininet With Forwarding Table/assign6/Q1.py new file mode 100644 index 0000000..74af51e --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/assign6/Q1.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +from mininet.topo import Topo +from mininet.net import Mininet +from mininet.nodelib import NAT +from mininet.log import setLogLevel +from mininet.cli import CLI +from mininet.util import dumpNodeConnections +from mininet.util import irange +import thread +from mininet.node import Node +from time import sleep +import mininet.node + + +class LinuxRouter( Node ): + "A Node with IP forwarding enabled." + + def config( self, **params ): + super( LinuxRouter, self).config( **params ) + # Enable forwarding on the router + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) + + def terminate( self ): + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + super( LinuxRouter, self ).terminate() + +class customTopo(Topo): + def build( self, **_opts ): + #R1 = self.addNode( 'r1', cls=LinuxRouter, ip=defaultIP ) + R1 = self.addNode( 'R1', cls=LinuxRouter, ip='10.0.1.1/24',defaultRoute='via 11.0.1.2') + R2 = self.addNode('R2',cls=LinuxRouter,ip='10.0.2.1/24',defaultRoute='via 11.0.4.2') + R3 = self.addNode('R3',cls=LinuxRouter,ip='10.0.3.1/24',defaultRoute='via 11.0.3.1') + R4 = self.addNode('R4',cls=LinuxRouter,ip='10.0.4.1/24',defaultRoute='via 11.0.2.1') + switch1 = self.addSwitch('s1') + switch2 = self.addSwitch('s2') + H1 = self.addHost('H1',ip='10.0.1.2/24',defaultRoute='via 10.0.1.1') + H7 = self.addHost('H7',ip='10.0.2.2/24',defaultRoute='via 10.0.2.1') + for i in range(2,5): + h1 = self.addHost('H%d'%i,ip='10.0.3.%d'%i,defaultRoute='via 10.0.3.1') + self.addLink(h1,switch1) + for i in range(5,7): + h1 = self.addHost('H%d'%i,ip = '10.0.4.%d'%i,defaultRoute='via 10.0.4.1') + self.addLink(h1,switch2) + + self.addLink(switch1,R3,intfName2='R3-eth0',params2={'ip':'10.0.3.1/24'}) + self.addLink(switch2,R4,intfName2='R4-eth0',params2={'ip':'10.0.4.1/24'}) + self.addLink(H1,R1,intfName2='R1-eth0',params2={'ip' : '10.0.1.1/24'}) + self.addLink(H7,R2,intfName2='R2-eth0',params2={'ip':'10.0.2.1/24'}) + + #connection among routers + self.addLink(R1,R2,intfName1='R1-eth1',intfName2='R2-eth1',params1={'ip':'11.0.1.1/24'},params2={'ip':'11.0.1.2/24'}) + self.addLink(R3,R4,intfName1='R3-eth1',intfName2='R4-eth1',params1={'ip':'11.0.2.1/24'},params2={'ip':'11.0.2.2/24'}) + self.addLink(R1,R3,intfName1='R1-eth2',intfName2='R3-eth2',params1={'ip':'11.0.3.1/24'},params2={'ip':'11.0.3.2/24'}) + self.addLink(R2,R4,intfName1='R2-eth2',intfName2='R4-eth2',params1={'ip':'11.0.4.1/24'},params2={'ip':'11.0.4.2/24'}) + +def configureR3subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.3.1') + host.cmd('route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.3.1') + +def configureR4subnetHost(host): + host.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.4.1') + host.cmd('route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.4.1') + +def configureRouter(routerNo,router): + with open("R{}_FwdingTbl.txt".format(routerNo)) as f: + contents = f.readlines() + contents = [x.strip() for x in contents] + for i in range(1,len(contents)): + routeInfo = contents[i] + routeInfo = routeInfo.replace('\t'," ") + routeInfo = routeInfo.split() + #print(routeInfo) + router.cmd('route add -net {} netmask {} gw {}'.format(routeInfo[0],routeInfo[2],routeInfo[1])) + + +def createTopo(): + topo = customTopo() + net = Mininet(topo=topo) + net.start() + #configure host + for i in range(2,7): + if(i<5): + configureR3subnetHost(net.get('H%i'%i)) + else: + configureR4subnetHost(net.get('H%i'%i)) + + #configure Router + for i in range(1,5): + print("configuring Router %i......."%i) + configureRouter(i,net.get('R%i'%i)) + CLI(net) + net.pingAll() + net.stop() + +if __name__ == '__main__': + setLogLevel('info') + createTopo() + diff --git a/Network Lab/Minininet With Forwarding Table/assign6/R1_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/assign6/R1_FwdingTbl.txt new file mode 100644 index 0000000..c019ed4 --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/assign6/R1_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 10.0.1.1 255.255.255.0 R1-eth0 +10.0.2.0 11.0.1.2 255.255.255.0 R1-eth1 +10.0.3.0 11.0.3.2 255.255.255.0 R1-eth2 +10.0.4.0 11.0.1.2 255.255.255.0 R1-eth1 +11.0.1.0 11.0.1.1 255.255.255.0 R1-eth1 +11.0.2.0 11.0.1.2 255.255.255.0 R1-eth1 +11.0.3.0 11.0.3.1 255.255.255.0 R1-eth2 +11.0.4.0 11.0.1.2 255.255.255.0 R1-eth1 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/assign6/R2_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/assign6/R2_FwdingTbl.txt new file mode 100644 index 0000000..d7da1ec --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/assign6/R2_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 11.0.1.1 255.255.255.0 R2-eth1 +10.0.2.0 10.0.2.1 255.255.255.0 R2-eth0 +10.0.3.0 11.0.4.2 255.255.255.0 R2-eth2 +10.0.4.0 11.0.4.2 255.255.255.0 R2-eth2 +11.0.1.0 11.0.1.2 255.255.255.0 R2-eth1 +11.0.2.0 11.0.4.2 255.255.255.0 R2-eth2 +11.0.3.0 11.0.4.2 255.255.255.0 R2-eth2 +11.0.4.0 11.0.4.1 255.255.255.0 R2-eth2 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/assign6/R3_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/assign6/R3_FwdingTbl.txt new file mode 100644 index 0000000..a5dfb0e --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/assign6/R3_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 11.0.3.1 255.255.255.0 R3-eth2 +10.0.2.0 11.0.2.2 255.255.255.0 R3-eth1 +10.0.3.0 10.0.3.1 255.255.255.0 R3-eth0 +10.0.4.0 11.0.2.2 255.255.255.0 R3-eth1 +11.0.1.0 11.0.3.1 255.255.255.0 R3-eth2 +11.0.2.0 11.0.2.1 255.255.255.0 R3-eth1 +11.0.3.0 11.0.3.2 255.255.255.0 R3-eth2 +11.0.4.0 11.0.2.2 255.255.255.0 R3-eth1 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/assign6/R4_FwdingTbl.txt b/Network Lab/Minininet With Forwarding Table/assign6/R4_FwdingTbl.txt new file mode 100644 index 0000000..0e5d92d --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/assign6/R4_FwdingTbl.txt @@ -0,0 +1,9 @@ +Destination IP Next hop Netmask Interface +10.0.1.0 11.0.4.1 255.255.255.0 R4-eth2 +10.0.2.0 11.0.4.1 255.255.255.0 R4-eth2 +10.0.3.0 11.0.2.1 255.255.255.0 R4-eth1 +10.0.4.0 10.0.4.1 255.255.255.0 R4-eth0 +11.0.1.0 11.0.4.1 255.255.255.0 R4-eth2 +11.0.2.0 11.0.2.2 255.255.255.0 R4-eth1 +11.0.3.0 11.0.2.1 255.255.255.0 R4-eth1 +11.0.4.0 11.0.4.2 255.255.255.0 R4-eth2 \ No newline at end of file diff --git a/Network Lab/Minininet With Forwarding Table/routeTable.txt b/Network Lab/Minininet With Forwarding Table/routeTable.txt new file mode 100644 index 0000000..d53a467 --- /dev/null +++ b/Network Lab/Minininet With Forwarding Table/routeTable.txt @@ -0,0 +1,238 @@ +//R1 +mininet> R1 route del default gw 11.0.1.2 +mininet> R1 route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.1.1 +mininet> R1 route add -net 10.0.2.0 netmask 255.255.255.0 gw 11.0.1.2 +mininet> R1 route add -net 10.0.3.0 netmask 255.255.255.0 gw 11.0.3.2 +mininet> R1 route add -net 10.0.4.0 netmask 255.255.255.0 gw 11.0.1.2 +mininet> R1 route add -net 11.0.1.0 netmask 255.255.255.0 gw 11.0.1.1 +mininet> R1 route add -net 11.0.2.0 netmask 255.255.255.0 gw 11.0.1.2 +mininet> R1 route add -net 11.0.3.0 netmask 255.255.255.0 gw 11.0.3.1 +mininet> R1 route add -net 11.0.4.0 netmask 255.255.255.0 gw 11.0.1.2 + +mininet> R1 route + +mininet> R1 route +Kernel IP routing table +Destination Gateway Genmask Flags Metric Ref Use Iface +10.0.1.0 10.0.1.1 255.255.255.0 UG 0 0 0 R1-eth0 +10.0.1.0 * 255.255.255.0 U 0 0 0 R1-eth0 +10.0.2.0 11.0.1.2 255.255.255.0 UG 0 0 0 R1-eth1 +10.0.3.0 11.0.3.2 255.255.255.0 UG 0 0 0 R1-eth2 +10.0.4.0 11.0.1.2 255.255.255.0 UG 0 0 0 R1-eth1 +11.0.1.0 11.0.1.1 255.255.255.0 UG 0 0 0 R1-eth1 +11.0.1.0 * 255.255.255.0 U 0 0 0 R1-eth1 +11.0.2.0 11.0.1.2 255.255.255.0 UG 0 0 0 R1-eth1 +11.0.3.0 11.0.3.1 255.255.255.0 UG 0 0 0 R1-eth2 +11.0.3.0 * 255.255.255.0 U 0 0 0 R1-eth2 +11.0.4.0 11.0.1.2 255.255.255.0 UG 0 0 0 R1-eth1 + + +//R2 +mininet> R2 route del default gw 11.0.4.2 + R2 route add -net 10.0.1.0 netmask 255.255.255.0 gw 11.0.1.1 + R2 route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.2.1 +mininet> R2 route add -net 10.0.3.0 netmask 255.255.255.0 gw 11.0.4.2 +mininet> R2 route add -net 10.0.4.0 netmask 255.255.255.0 gw 11.0.4.2 +mininet> R2 route add -net 11.0.1.0 netmask 255.255.255.0 gw 11.0.1.2 +mininet> R2 route add -net 11.0.2.0 netmask 255.255.255.0 gw 11.0.4.2 +mininet> R2 route add -net 11.0.3.0 netmask 255.255.255.0 gw 11.0.4.2 +mininet> R2 route add -net 11.0.4.0 netmask 255.255.255.0 gw 11.0.4.1 + +mininet> R2 route +Kernel IP routing table +Destination Gateway Genmask Flags Metric Ref Use Iface +10.0.1.0 11.0.1.1 255.255.255.0 UG 0 0 0 R2-eth1 +10.0.2.0 10.0.2.1 255.255.255.0 UG 0 0 0 R2-eth0 +10.0.2.0 * 255.255.255.0 U 0 0 0 R2-eth0 +10.0.3.0 11.0.4.2 255.255.255.0 UG 0 0 0 R2-eth2 +10.0.4.0 11.0.4.2 255.255.255.0 UG 0 0 0 R2-eth2 +11.0.1.0 11.0.1.2 255.255.255.0 UG 0 0 0 R2-eth1 +11.0.1.0 * 255.255.255.0 U 0 0 0 R2-eth1 +11.0.2.0 11.0.4.2 255.255.255.0 UG 0 0 0 R2-eth2 +11.0.3.0 11.0.4.2 255.255.255.0 UG 0 0 0 R2-eth2 +11.0.4.0 11.0.4.1 255.255.255.0 UG 0 0 0 R2-eth2 +11.0.4.0 * 255.255.255.0 U 0 0 0 R2-eth2 + +//R3 route +mininet> R3 route del default gw 11.0.3.1 +mininet> R3 route add -net 10.0.1.0 netmask 255.255.255.0 gw 11.0.3.1 +mininet> R3 route add -net 10.0.2.0 netmask 255.255.255.0 gw 11.0.3.1 +mininet> R3 route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> R3 route add -net 10.0.4.0 netmask 255.255.255.0 gw 11.0.2.2 +mininet> R3 route add -net 11.0.1.0 netmask 255.255.255.0 gw 11.0.3.1 +mininet> R3 route add -net 11.0.2.0 netmask 255.255.255.0 gw 11.0.2.1 +mininet> R3 route add -net 11.0.3.0 netmask 255.255.255.0 gw 11.0.3.2 +mininet> R3 route add -net 11.0.4.0 netmask 255.255.255.0 gw 11.0.2.2 +mininet> R3 route del -net 10.0.2.0 netmask 255.255.255.0 gw 11.0.3.1 +mininet> R3 route add -net 10.0.2.0 netmask 255.255.255.0 gw 11.0.2.2 + +mininet> R3 route +Kernel IP routing table +Destination Gateway Genmask Flags Metric Ref Use Iface +10.0.1.0 11.0.3.1 255.255.255.0 UG 0 0 0 R3-eth2 +10.0.2.0 11.0.3.1 255.255.255.0 UG 0 0 0 R3-eth2 +10.0.3.0 10.0.3.1 255.255.255.0 UG 0 0 0 R3-eth0 +10.0.3.0 * 255.255.255.0 U 0 0 0 R3-eth0 +10.0.4.0 11.0.2.2 255.255.255.0 UG 0 0 0 R3-eth1 +11.0.1.0 11.0.3.1 255.255.255.0 UG 0 0 0 R3-eth2 +11.0.2.0 11.0.2.1 255.255.255.0 UG 0 0 0 R3-eth1 +11.0.2.0 * 255.255.255.0 U 0 0 0 R3-eth1 +error ->11.0.3.0 11.0.3.1 255.255.255.0 UG 0 0 0 R3-eth2 +11.0.3.0 * 255.255.255.0 U 0 0 0 R3-eth2 +11.0.4.0 11.0.2.2 255.255.255.0 UG 0 0 0 R3-eth1 + + +//R4 route +mininet> R4 route del default gw 11.0.2.1 +R4 route add -net 10.0.1.0 netmask 255.255.255.0 gw 11.0.4.1 +R4 route add -net 10.0.2.0 netmask 255.255.255.0 gw 11.0.4.1 + +mininet> R4 route add -net 10.0.3.0 netmask 255.255.255.0 gw 11.0.2.1 +mininet> R4 route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.4.1 +mininet> R4 route add -net 11.0.1.0 netmask 255.255.255.0 gw 11.0.4.1 +mininet> R4 route add -net 11.0.2.0 netmask 255.255.255.0 gw 11.0.2.2 +mininet> R4 route add -net 11.0.3.0 netmask 255.255.255.0 gw 11.0.2.1 +mininet> R4 route add -net 11.0.4.0 netmask 255.255.255.0 gw 11.0.4.2 + +mininet> R4 route +Kernel IP routing table +Destination Gateway Genmask Flags Metric Ref Use Iface +10.0.1.0 11.0.4.1 255.255.255.0 UG 0 0 0 R4-eth2 +10.0.2.0 11.0.4.1 255.255.255.0 UG 0 0 0 R4-eth2 +10.0.3.0 11.0.2.1 255.255.255.0 UG 0 0 0 R4-eth1 +10.0.4.0 10.0.4.1 255.255.255.0 UG 0 0 0 R4-eth0 +10.0.4.0 * 255.255.255.0 U 0 0 0 R4-eth0 +11.0.1.0 11.0.4.1 255.255.255.0 UG 0 0 0 R4-eth2 +11.0.2.0 11.0.2.2 255.255.255.0 UG 0 0 0 R4-eth1 +11.0.2.0 * 255.255.255.0 U 0 0 0 R4-eth1 +11.0.3.0 11.0.2.1 255.255.255.0 UG 0 0 0 R4-eth1 +11.0.4.0 11.0.4.2 255.255.255.0 UG 0 0 0 R4-eth2 +11.0.4.0 * 255.255.255.0 U 0 0 0 R4-eth2 + + +//last result +mininet> pingall +*** Ping: testing ping reachability +H1 -> X X X X X X R1 X X X +H2 -> X H3 H4 X X X X X R3 X +H3 -> X H2 H4 X X X X X R3 X +H4 -> X H2 H3 X X X X X R3 X +H5 -> X X X X H6 X X X X R4 +H6 -> X X X X H5 X X X X R4 +H7 -> X X X X X X X R2 X X +R1 -> H1 X X X X X H7 R2 X X +R2 -> X X X X H5 H6 H7 X X R4 +R3 -> H1 H2 H3 H4 X X X R1 X X +R4 -> X H2 H3 H4 H5 H6 X X X R3 +*** Results: 70% dropped (33/110 received) + +//improvement +mininet> pingall +*** Ping: testing ping reachability +H1 -> X X X X X H7 R1 R2 R3 R4 +H2 -> X H3 H4 X X X X X R3 X +H3 -> X H2 H4 X X X X X R3 X +H4 -> X H2 H3 X X X X X R3 X +H5 -> X X X X H6 X X X X R4 +H6 -> X X X X H5 X X X X R4 +H7 -> H1 X X X X X R1 R2 X R4 +R1 -> H1 H2 H3 H4 H5 H6 H7 R2 R3 R4 +R2 -> H1 H2 H3 H4 H5 H6 H7 R1 R3 R4 +R3 -> H1 H2 H3 H4 H5 H6 X R1 X R4 +R4 -> H1 H2 H3 H4 H5 H6 H7 R1 R2 R3 +*** Results: 45% dropped (60/110 received) + + +mininet> H2 route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> H3 route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> H4 route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> pingall +*** Ping: testing ping reachability +H1 -> H2 H3 H4 X X H7 R1 R2 R3 R4 +H2 -> H1 H3 H4 X X X R1 X R3 X +H3 -> H1 H2 H4 X X X R1 X R3 X +H4 -> H1 H2 H3 X X X R1 X R3 X +H5 -> X X X X H6 X X X X R4 +H6 -> X X X X H5 X X X X R4 +H7 -> H1 X X X X X R1 R2 X R4 +R1 -> H1 H2 H3 H4 H5 H6 H7 R2 R3 R4 +R2 -> H1 H2 H3 H4 H5 H6 H7 R1 R3 R4 +R3 -> H1 H2 H3 H4 H5 H6 X R1 X R4 +R4 -> H1 H2 H3 H4 H5 H6 H7 R1 R2 R3 +*** Results: 37% dropped (69/110 received) + +mininet> H5 route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.4.1 +mininet> H6 route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.4.1 + +mininet> pingall +*** Ping: testing ping reachability +H1 -> H2 H3 H4 H5 H6 H7 R1 R2 R3 R4 +H2 -> H1 H3 H4 X X X R1 X R3 X +H3 -> H1 H2 H4 X X X R1 X R3 X +H4 -> H1 H2 H3 X X X R1 X R3 X +H5 -> H1 X X X H6 X R1 X X R4 +H6 -> H1 X X X H5 X R1 X X R4 +H7 -> H1 X X X X X R1 R2 X R4 +R1 -> H1 H2 H3 H4 H5 H6 H7 R2 R3 R4 +R2 -> H1 H2 H3 H4 H5 H6 H7 R1 R3 R4 +R3 -> H1 H2 H3 H4 H5 H6 X R1 X R4 +R4 -> H1 H2 H3 H4 H5 H6 H7 R1 R2 R3 +*** Results: 31% dropped (75/110 received) + +mininet> H5 route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.4.1 +mininet> H6 route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.4.1 +*** Ping: testing ping reachability +H1 -> H2 H3 H4 H5 H6 H7 R1 R2 R3 R4 +H2 -> H1 H3 H4 X X X R1 X R3 X +H3 -> H1 H2 H4 X X X R1 X R3 X +H4 -> H1 H2 H3 X X X R1 X R3 X +H5 -> H1 X X X H6 H7 R1 R2 X R4 +H6 -> H1 X X X H5 H7 R1 R2 X R4 +H7 -> H1 X X X H5 H6 R1 R2 X R4 +R1 -> H1 H2 H3 H4 H5 H6 H7 R2 R3 R4 +R2 -> H1 H2 H3 H4 H5 H6 H7 R1 R3 R4 +R3 -> H1 H2 H3 H4 H5 H6 X R1 X R4 +R4 -> H1 H2 H3 H4 H5 H6 H7 R1 R2 R3 +*** Results: 26% dropped (81/110 received) + +mininet> H2 route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> H5 route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.4.1 +mininet> H3 route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> H4 route add -net 10.0.4.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> H6 route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.4.1 +mininet> pingall +*** Ping: testing ping reachability +H1 -> H2 H3 H4 H5 H6 H7 R1 R2 R3 R4 +H2 -> H1 H3 H4 H5 H6 X R1 X R3 R4 +H3 -> H1 H2 H4 H5 H6 X R1 X R3 R4 +H4 -> H1 H2 H3 H5 H6 X R1 X R3 R4 +H5 -> H1 H2 H3 H4 H6 H7 R1 R2 R3 R4 +H6 -> H1 H2 H3 H4 H5 H7 R1 R2 R3 R4 +H7 -> H1 X X X H5 H6 R1 R2 X R4 +R1 -> H1 H2 H3 H4 H5 H6 H7 R2 R3 R4 +R2 -> H1 H2 H3 H4 H5 H6 H7 R1 R3 R4 +R3 -> H1 H2 H3 H4 H5 H6 X R1 X R4 +R4 -> H1 H2 H3 H4 H5 H6 H7 R1 R2 R3 +*** Results: 10% dropped (98/110 received) + +mininet> H3 route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> H4 route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.3.1 +mininet> pingall +*** Ping: testing ping reachability +H1 -> H2 H3 H4 H5 H6 H7 R1 R2 R3 R4 +H2 -> H1 H3 H4 H5 H6 H7 R1 R2 R3 R4 +H3 -> H1 H2 H4 H5 H6 H7 R1 R2 R3 R4 +H4 -> H1 H2 H3 H5 H6 H7 R1 R2 R3 R4 +H5 -> H1 H2 H3 H4 H6 H7 R1 R2 R3 R4 +H6 -> H1 H2 H3 H4 H5 H7 R1 R2 R3 R4 +H7 -> H1 H2 H3 H4 H5 H6 R1 R2 R3 R4 +R1 -> H1 H2 H3 H4 H5 H6 H7 R2 R3 R4 +R2 -> H1 H2 H3 H4 H5 H6 H7 R1 R3 R4 +R3 -> H1 H2 H3 H4 H5 H6 H7 R1 R2 R4 +R4 -> H1 H2 H3 H4 H5 H6 H7 R1 R2 R3 +*** Results: 0% dropped (110/110 received) + + +//Don't know why changed path from R3 to R2 +//Don't Know why need to add route in switch network +//Don't know why interface ip can't be .0 \ No newline at end of file diff --git a/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/assign2.iml b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/assign2.iml new file mode 100644 index 0000000..6711606 --- /dev/null +++ b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/assign2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/misc.xml b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/misc.xml new file mode 100644 index 0000000..5bbe586 --- /dev/null +++ b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/modules.xml b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/modules.xml new file mode 100644 index 0000000..5775c8a --- /dev/null +++ b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/workspace.xml b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/workspace.xml new file mode 100644 index 0000000..f29b9bf --- /dev/null +++ b/Network Lab/Packet Switching with Poisson Packet generation rate/.idea/workspace.xml @@ -0,0 +1,338 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1516178488921 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Network Lab/Switching Network Simulation/CS348-lab1.pdf b/Network Lab/Switching Network Simulation/CS348-lab1.pdf new file mode 100644 index 0000000..f0d2111 Binary files /dev/null and b/Network Lab/Switching Network Simulation/CS348-lab1.pdf differ diff --git a/Network Lab/Switching Network Simulation/Packetdrop.py~ b/Network Lab/Switching Network Simulation/Packetdrop.py~ new file mode 100644 index 0000000..f8f59e4 --- /dev/null +++ b/Network Lab/Switching Network Simulation/Packetdrop.py~ @@ -0,0 +1,177 @@ +import matplotlib.pyplot as plt +import queue as Q + +utilNum = 0 + +pktDrop = 0 + +# class for pcket +class DATAPACKET: + def __init__(self,pid=0,gtime=0.0,sourceid=0): + self.packetId = pid + self.sourceId = sourceid + self.gentime = gtime + self.qInTIme = -1 + self.qOutTime = -1 + +# class for source +class DATASOURCE: + def __init__(self,lamda,sid,bs): + self.genRate = lamda + self.sourceId = sid + self.bs = bs + +# class for switch +class DATASWITCH: + def __init__(self,bwidth): + self.bss = bwidth + self.qSize = 0 + +# class for various Event +class EVENT: + def __init__(self,eid,pid,t): + self.eId = eid + self.pId = pid + self.curTime = t + + def __lt__(self, other): + return self.curTime < other.curTime + +# function to calculate pktLossRate +def calculatePktLossRate(nSource,bs,bss,pktLength,source,simTime,fixedQueueSize): + packet = [] + avgQueuingDelay = 0.0 + swich = DATASWITCH(bss) + global pktDrop + pktDrop = 0 + # pq is priority queue on the basis of event current time + pq = Q.PriorityQueue() + ttime = 0.0 + + # generating first packet from every source at an fixed interval + for i in range(nSource): + packet.append(DATAPACKET(i, ttime, i)) + pq.put(EVENT(0, i, ttime)) + ttime += 0.000001 + + pcount = 0 + pktTot = nSource + lastLeftTime = 0 + executionTime = 0.0 + packetarrived=0 + # Simulating for fixed time + # Event0 = generation of packet + # Event1 = reaching Queue time + # Event2 = leaving queue time + # Event3 = reaching sink time + + while (pcount < 1000): + x = pq.get() + pid = x.pId + curTime = x.curTime + #executionTime = max(executionTime,curTime) + + # Event 0 -> (Event0,Event1) + if x.eId == 0: + rate = source[packet[pid].sourceId].genRate + pq.put(EVENT(0, pktTot, curTime + 1 / rate)) + packet.append(DATAPACKET(pktTot, curTime + 1 / rate, packet[pid].sourceId)) + pktTot = pktTot + 1 + pq.put(EVENT(1, pid, curTime + pktLength/bs)) + packet[pid].qInTime = curTime + pktLength / bs + # Event 1 -> Event2,and if queue is full packet drop + elif x.eId == 1: + packetarrived = packetarrived+1 + if(swich.qSize<=fixedQueueSize): + rtime = (swich.qSize * pktLength) / bss + tx = 0 + if packet[pid].qInTime - lastLeftTime < (pktLength / bss): + tx = max(0, (pktLength / bss) - (packet[pid].qInTime - lastLeftTime)) + if lastLeftTime == 0: + tx = 0 + pq.put(EVENT(2, pid, curTime + rtime + tx)) + packet[pid].qOutTime = curTime + rtime + tx + swich.qSize = swich.qSize + 1 + else: + #counting pkt drop + pktDrop = pktDrop+1 + + # Event2 -> Event3 + elif x.eId == 2: + swich.qSize = swich.qSize - 1 + lastLeftTime = curTime + sTime = curTime + pktLength / bss + pq.put(EVENT(3, pid, sTime)) + else: + avgQueuingDelay = packet[pid].qOutTime - packet[pid].qInTime + avgQueuingDelay + pcount = pcount + 1 + #return pktDroprate + return pktDrop/packetarrived + +def main(): + print("Enter 0 to use default value or 1 to use own") + resp = int(input()) + if resp == 0: + # nsource = number of source + nSource = 4 + # bs = bandwidth between source and switch in bit + bs = 1e2 + # bss = bandwidth between switch and sink in bit + bsslow = 10 + bsshigh = 500 + # pktLength = size of each packet in bit + pktLength = 2 + + # grate = packet genrate + grate = 20 + + # simulation time + simTime = 200 + + #max queue size in switch + fixedQueueSize = 50 + else : + nSource = int(input("Enter Number of Source :")) + bs = float(input("Enter bandwdth between Source and switch(bs) in bit:")) + bsslow = float(input("Enter bandwidth(lower bound) between switch and sink(bss) in bit:")) + bsshigh = float(input("Enter bandwidth(upper bound) between switch and sink(bss) in bit:")) + pktLength = int(input("Enter packet length in bit(pktlength) :")) + grate = int(input("Enter packet generation rate:")) + if pktLength * grate >= bs: + print("pktLength*grater should be less than bs") + return 0 + simTime = int(input("Enter Simulation time:")) + fixedQueueSize = int(input("Enter Queue size in switch:")) + + # utilNum = numerator of utilization factor i.e arrival rate + global utilNum + # x and y holds value of delay and utilizationfactor + x = [] + y = [] + source = [] + for i in range(nSource): + # 16(pkt/s) is pkt generation rate from each source + source.append(DATASOURCE(grate, i, bs)) + utilNum = utilNum+grate + # varying nbss i.e bss for plotting + nbss = bsslow + while(nbss<=bsshigh): + loadfactor=(utilNum*pktLength)/nbss + pktDropRate = calculatePktLossRate(nSource,bs,nbss,pktLength,source,simTime,fixedQueueSize) + x.append(loadfactor) + y.append(pktDropRate) + print(loadfactor," ",pktDropRate) + nbss = nbss+(bsshigh-bsslow)/980 + + + # plotting curve + plt.plot(x,y) + plt.xlabel("Utilization Factor") + plt.ylabel("Packet loss rate") + #plt.text(x[len(x) - 1], y[len(y) - 1], "nSource=4,bs(bit)=1e2,lambda(gen rate)=20,pktlength=2 bit, \n bsslow(bit/s)=10,bsshigh=500") + plt.title("Packet loss rate vs Utilization Factor") + plt.show() + +if __name__=="__main__": + main() + diff --git a/Network Lab/Switching Network Simulation/QueuingDelay.py~ b/Network Lab/Switching Network Simulation/QueuingDelay.py~ new file mode 100644 index 0000000..65035ae --- /dev/null +++ b/Network Lab/Switching Network Simulation/QueuingDelay.py~ @@ -0,0 +1,184 @@ +import matplotlib.pyplot as plt +import queue as Q + +utilNum = 0 + + +# class for pcket +class DATAPACKET: + def __init__(self, pid=0, gtime=0.0, sourceid=0): + self.packetId = pid + self.sourceId = sourceid + self.gentime = gtime + self.qInTIme = -1 + self.qOutTime = -1 + self.sinkReachTime = -1 + + +# class for source +class DATASOURCE: + def __init__(self, lamda, sid, bs): + self.genRate = lamda + self.sourceId = sid + self.bs = bs + + +# class for switch +class DATASWITCH: + def __init__(self, bwidth): + self.bss = bwidth + self.qSize = 0 + + +# class for various Event +class EVENT: + def __init__(self, eid, pid, t): + self.eId = eid + self.pId = pid + self.curTime = t + + def __lt__(self, other): + return self.curTime < other.curTime + + +# function to calculate avg delay +def calculateAvgDelay(nSource, bs, bss, pktLength, source,simTime): + packet = [] + avgQueuingDelay = 0.0 + swich = DATASWITCH(bss) + + # pq is priority queue on the basis of event current time + pq = Q.PriorityQueue() + + # generating first packet from every source at an fixed interval + ttime = 0.0 + for i in range(nSource): + packet.append(DATAPACKET(i, ttime, i)) + pq.put(EVENT(0, i, ttime)) + ttime += 0.000001 + + pcount = 0 + pktTot = nSource + lastLeftTime = 0.0 + + # Simulating for fixed time + # Event0 = generation of packet + # Event1 = reaching Queue time + # Event2 = leaving queue time + # Event3 = reaching sink time + curTime = 0 + pkt_reach_sink = 0 + while (pkt_reach_sink < 5000): + x = pq.get() + pid = x.pId + curTime = x.curTime + # print(curTime) + # Event 0 -> (Event0,Event1) + if x.eId == 0: + rate = source[packet[pid].sourceId].genRate + pq.put(EVENT(0, pktTot, curTime + 1 / rate)) + packet.append(DATAPACKET(pktTot, curTime + 1 / rate, packet[pid].sourceId)) + pktTot = pktTot + 1 + pq.put(EVENT(1, pid, curTime + pktLength / bs)) + packet[pid].qInTime = curTime + pktLength / bs + # Event 1 -> Event2 + elif x.eId == 1: + rtime = (swich.qSize * pktLength) / bss + tx = 0 + if packet[pid].qInTime - lastLeftTime < (pktLength / bss): + tx = max(0, (pktLength / bss) - (packet[pid].qInTime - lastLeftTime)) + if lastLeftTime == 0: + tx = 0 + pq.put(EVENT(2, pid, curTime + rtime + tx)) + packet[pid].qOutTime = curTime + rtime + tx + avgQueuingDelay = avgQueuingDelay+packet[pid].qOutTime-packet[pid].gentime+pktLength/bss + swich.qSize = swich.qSize + 1 + pcount = pcount + 1 + # Event2 -> Event3 + elif x.eId == 2: + swich.qSize = swich.qSize - 1 + lastLeftTime = curTime + sTime = curTime + pktLength / bss + packet[pid].sinkReachTime = sTime + pq.put(EVENT(3, pid, sTime)) + else: + pkt_reach_sink = pkt_reach_sink + 1 + #pcount = pcount + 1 + + avgQueuingDelay = avgQueuingDelay / pcount + return avgQueuingDelay + + +def main(): + + print ("Enter 0 to use default value or 1 to use own") + resp = int(input()) + if resp == 0: + # nsource = number of source + nSource = 4 + + # bs = bandwidth between source and switch in bit + bs = 2e6 + + # bss = bandwidth between switch and sink in bit + bsshigh = 8e6 + bsslow = 25e3 + # pktLength = size of each packet in bit + pktLength = 1.2e4 + + #grate = packet genrate + grate = 16 + + #simulation time + simTime = 200 + else : + + nSource = int(input("Enter Number of Source :")) + bs = float(input("Enter bandwdth between Source and switch(bs) in bit:")) + bsslow = float(input("Enter bandwidth(lower bound) between switch and sink(bss) in bit:")) + bsshigh = float(input("Enter bandwidth(upper bound) between switch and sink(bss) in bit:")) + pktLength = int(input("Enter packet length in bit(pktlength) :")) + grate = int(input("Enter packet generation rate for source(same for all):")) + if pktLength*grate >= bs: + print("pktLength*grater should be less than bs") + return 0 + simTime =int(input("Enter Simulation time:")) + + # utilNum = numerator of utilization factor i.e arrival rate + global utilNum + # x and y holds value of delay and utilizationfactor + x = [] + y = [] + + # creating source object + source = [] + for i in range(nSource): + # 16(pkt/s) is pkt generation rate from each source + source.append(DATASOURCE(grate, i, bs)) + utilNum = utilNum + grate + + nbss = bsslow + # varying nbss i.e bss for plotting + while (nbss<=bsshigh): + loadfactor = (utilNum * pktLength) / nbss + avgDelay = calculateAvgDelay(nSource, bs, nbss, pktLength, source,simTime) + x.append(loadfactor) + y.append(avgDelay) + print(loadfactor, " ", avgDelay) + if(loadfactor>1): + loadfactor -= 1 + nbss = utilNum*pktLength/loadfactor + else: + nbss = nbss + 25000 + + # plotting curve + plt.plot(x, y) + plt.xlabel("Utilization Factor") + plt.ylabel("Average Delay") + #plt.text(x[len(x)-1],y[len(y)-1],"nSource=4,bs(bit)=2e6,lambda(gen rate)=16,\n pktlength=12000 bit,bsslow(bit/s)=5e5,bsshigh=8e6") + plt.title("Average delay vs utilizationFactor") + plt.show() + + +if __name__ == "__main__": + main() diff --git a/Network Lab/Switching Network Simulation/part1.py b/Network Lab/Switching Network Simulation/part1.py new file mode 100644 index 0000000..55cb4da --- /dev/null +++ b/Network Lab/Switching Network Simulation/part1.py @@ -0,0 +1,182 @@ +import matplotlib.pyplot as plt +import queue as Q + +utilNum = 0 + + +# class for pcket +class DATAPACKET: + def __init__(self, pid=0, gtime=0.0, sourceid=0): + self.packetId = pid + self.sourceId = sourceid + self.gentime = gtime + self.qInTIme = -1 + self.qOutTime = -1 + self.sinkReachTime = -1 + + +# class for source +class DATASOURCE: + def __init__(self, lamda, sid, bs): + self.genRate = lamda + self.sourceId = sid + self.bs = bs + + +# class for switch +class DATASWITCH: + def __init__(self, bwidth): + self.bss = bwidth + self.qSize = 0 + + +# class for various Event +class EVENT: + def __init__(self, eid, pid, t): + self.eId = eid + self.pId = pid + self.curTime = t + + def __lt__(self, other): + return self.curTime < other.curTime + + +# function to calculate avg delay +def calculateAvgDelay(nSource, bs, bss, pktLength, source,simTime): + packet = [] + avgDelay = 0.0 + swich = DATASWITCH(bss) + + # pq is priority queue on the basis of event current time + pq = Q.PriorityQueue() + + # generating first packet from every source at an fixed interval + ttime = 0.0 + for i in range(nSource): + packet.append(DATAPACKET(i, ttime, i)) + pq.put(EVENT(0, i, ttime)) + ttime += 0.000001 + + pcount = 0 + pktTot = nSource + lastLeftTime = 0.0 + simTime = 5000 + # Simulating for fixed time + # Event0 = generation of packet + # Event1 = reaching Queue time + # Event2 = leaving queue time + # Event3 = reaching sink time + curTime = 0 + pkt_reach_sink = 0 + while (pkt_reach_sink < simTime): + x = pq.get() + pid = x.pId + curTime = x.curTime + # print(curTime) + # Event 0 -> (Event0,Event1) + if x.eId == 0: + rate = source[packet[pid].sourceId].genRate + pq.put(EVENT(0, pktTot, curTime + 1 / rate)) + packet.append(DATAPACKET(pktTot, curTime + 1 / rate, packet[pid].sourceId)) + pktTot = pktTot + 1 + pq.put(EVENT(1, pid, curTime + pktLength / bs)) + packet[pid].qInTime = curTime + pktLength / bs + # Event 1 -> Event2 + elif x.eId == 1: + rtime = (swich.qSize * pktLength) / bss + tx = 0 + if packet[pid].qInTime - lastLeftTime < (pktLength / bss): + tx = max(0, (pktLength / bss) - (packet[pid].qInTime - lastLeftTime)) + if lastLeftTime == 0: + tx = 0 + pq.put(EVENT(2, pid, curTime + rtime + tx)) + packet[pid].qOutTime = curTime + rtime + tx + avgDelay = avgDelay+packet[pid].qOutTime-packet[pid].gentime+pktLength/bss + swich.qSize = swich.qSize + 1 + pcount = pcount + 1 + # Event2 -> Event3 + elif x.eId == 2: + swich.qSize = swich.qSize - 1 + lastLeftTime = curTime + sTime = curTime + pktLength / bss + packet[pid].sinkReachTime = sTime + pq.put(EVENT(3, pid, sTime)) + else: + pkt_reach_sink = pkt_reach_sink + 1 + #pcount = pcount + 1 + + avgDelay = avgDelay / pcount + return avgDelay + + +def main(): + + print ("Enter 0 to use default value or 1 to use own") + resp = int(input()) + if resp == 0: + # nsource = number of source + nSource = 4 + + # bs = bandwidth between source and switch in bit + bs = 2e6 + + # bss = bandwidth between switch and sink in bit + bsshigh = 8e6 + bsslow = 25e3 + # pktLength = size of each packet in bit + pktLength = 1.2e4 + + #grate = packet genrate + grate = 16 + + #simulation time + simTime = 5000 + else : + + nSource = int(input("Enter Number of Source :")) + bs = float(input("Enter bandwdth between Source and switch(bs) in bit:")) + bsslow = float(input("Enter bandwidth(lower bound) between switch and sink(bss) in bit:")) + bsshigh = float(input("Enter bandwidth(upper bound) between switch and sink(bss) in bit:")) + pktLength = int(input("Enter packet length in bit(pktlength) :")) + grate = int(input("Enter packet generation rate for source(same for all):")) + if pktLength*grate >= bs: + print("pktLength*grater should be less than bs") + return 0 + simTime =int(input("Enter Simulation time:")) + + # utilNum = numerator of utilization factor i.e arrival rate + global utilNum + # x and y holds value of delay and utilizationfactor + x = [] + y = [] + + # creating source object + source = [] + for i in range(nSource): + source.append(DATASOURCE(grate, i, bs)) + utilNum = utilNum + grate + + nbss = bsslow + # varying nbss i.e bss for plotting + while (nbss<=bsshigh): + loadfactor = (utilNum * pktLength) / nbss + avgDelay = calculateAvgDelay(nSource, bs, nbss, pktLength, source,simTime) + x.append(loadfactor) + y.append(avgDelay) + print(loadfactor, " ", avgDelay) + if(loadfactor>1): + nbss = nbss + (bsshigh-bsslow)/2000 + else: + nbss = nbss + (bsshigh-bsslow)/150 + + # plotting curve + plt.plot(x, y) + plt.xlabel("Utilization Factor") + plt.ylabel("Average Delay") + #plt.text(x[len(x)-1],y[len(y)-1],"nSource=4,bs(bit)=2e6,\nlambda(gen rate)=16,\n pktlength=12000 bit,\nbsslow(bit/s)=25000,bsshigh=8e6") + plt.title("Average delay vs utilizationFactor") + plt.show() + + +if __name__ == "__main__": + main() diff --git a/Network Lab/Switching Network Simulation/part1.py~ b/Network Lab/Switching Network Simulation/part1.py~ new file mode 100644 index 0000000..55cb4da --- /dev/null +++ b/Network Lab/Switching Network Simulation/part1.py~ @@ -0,0 +1,182 @@ +import matplotlib.pyplot as plt +import queue as Q + +utilNum = 0 + + +# class for pcket +class DATAPACKET: + def __init__(self, pid=0, gtime=0.0, sourceid=0): + self.packetId = pid + self.sourceId = sourceid + self.gentime = gtime + self.qInTIme = -1 + self.qOutTime = -1 + self.sinkReachTime = -1 + + +# class for source +class DATASOURCE: + def __init__(self, lamda, sid, bs): + self.genRate = lamda + self.sourceId = sid + self.bs = bs + + +# class for switch +class DATASWITCH: + def __init__(self, bwidth): + self.bss = bwidth + self.qSize = 0 + + +# class for various Event +class EVENT: + def __init__(self, eid, pid, t): + self.eId = eid + self.pId = pid + self.curTime = t + + def __lt__(self, other): + return self.curTime < other.curTime + + +# function to calculate avg delay +def calculateAvgDelay(nSource, bs, bss, pktLength, source,simTime): + packet = [] + avgDelay = 0.0 + swich = DATASWITCH(bss) + + # pq is priority queue on the basis of event current time + pq = Q.PriorityQueue() + + # generating first packet from every source at an fixed interval + ttime = 0.0 + for i in range(nSource): + packet.append(DATAPACKET(i, ttime, i)) + pq.put(EVENT(0, i, ttime)) + ttime += 0.000001 + + pcount = 0 + pktTot = nSource + lastLeftTime = 0.0 + simTime = 5000 + # Simulating for fixed time + # Event0 = generation of packet + # Event1 = reaching Queue time + # Event2 = leaving queue time + # Event3 = reaching sink time + curTime = 0 + pkt_reach_sink = 0 + while (pkt_reach_sink < simTime): + x = pq.get() + pid = x.pId + curTime = x.curTime + # print(curTime) + # Event 0 -> (Event0,Event1) + if x.eId == 0: + rate = source[packet[pid].sourceId].genRate + pq.put(EVENT(0, pktTot, curTime + 1 / rate)) + packet.append(DATAPACKET(pktTot, curTime + 1 / rate, packet[pid].sourceId)) + pktTot = pktTot + 1 + pq.put(EVENT(1, pid, curTime + pktLength / bs)) + packet[pid].qInTime = curTime + pktLength / bs + # Event 1 -> Event2 + elif x.eId == 1: + rtime = (swich.qSize * pktLength) / bss + tx = 0 + if packet[pid].qInTime - lastLeftTime < (pktLength / bss): + tx = max(0, (pktLength / bss) - (packet[pid].qInTime - lastLeftTime)) + if lastLeftTime == 0: + tx = 0 + pq.put(EVENT(2, pid, curTime + rtime + tx)) + packet[pid].qOutTime = curTime + rtime + tx + avgDelay = avgDelay+packet[pid].qOutTime-packet[pid].gentime+pktLength/bss + swich.qSize = swich.qSize + 1 + pcount = pcount + 1 + # Event2 -> Event3 + elif x.eId == 2: + swich.qSize = swich.qSize - 1 + lastLeftTime = curTime + sTime = curTime + pktLength / bss + packet[pid].sinkReachTime = sTime + pq.put(EVENT(3, pid, sTime)) + else: + pkt_reach_sink = pkt_reach_sink + 1 + #pcount = pcount + 1 + + avgDelay = avgDelay / pcount + return avgDelay + + +def main(): + + print ("Enter 0 to use default value or 1 to use own") + resp = int(input()) + if resp == 0: + # nsource = number of source + nSource = 4 + + # bs = bandwidth between source and switch in bit + bs = 2e6 + + # bss = bandwidth between switch and sink in bit + bsshigh = 8e6 + bsslow = 25e3 + # pktLength = size of each packet in bit + pktLength = 1.2e4 + + #grate = packet genrate + grate = 16 + + #simulation time + simTime = 5000 + else : + + nSource = int(input("Enter Number of Source :")) + bs = float(input("Enter bandwdth between Source and switch(bs) in bit:")) + bsslow = float(input("Enter bandwidth(lower bound) between switch and sink(bss) in bit:")) + bsshigh = float(input("Enter bandwidth(upper bound) between switch and sink(bss) in bit:")) + pktLength = int(input("Enter packet length in bit(pktlength) :")) + grate = int(input("Enter packet generation rate for source(same for all):")) + if pktLength*grate >= bs: + print("pktLength*grater should be less than bs") + return 0 + simTime =int(input("Enter Simulation time:")) + + # utilNum = numerator of utilization factor i.e arrival rate + global utilNum + # x and y holds value of delay and utilizationfactor + x = [] + y = [] + + # creating source object + source = [] + for i in range(nSource): + source.append(DATASOURCE(grate, i, bs)) + utilNum = utilNum + grate + + nbss = bsslow + # varying nbss i.e bss for plotting + while (nbss<=bsshigh): + loadfactor = (utilNum * pktLength) / nbss + avgDelay = calculateAvgDelay(nSource, bs, nbss, pktLength, source,simTime) + x.append(loadfactor) + y.append(avgDelay) + print(loadfactor, " ", avgDelay) + if(loadfactor>1): + nbss = nbss + (bsshigh-bsslow)/2000 + else: + nbss = nbss + (bsshigh-bsslow)/150 + + # plotting curve + plt.plot(x, y) + plt.xlabel("Utilization Factor") + plt.ylabel("Average Delay") + #plt.text(x[len(x)-1],y[len(y)-1],"nSource=4,bs(bit)=2e6,\nlambda(gen rate)=16,\n pktlength=12000 bit,\nbsslow(bit/s)=25000,bsshigh=8e6") + plt.title("Average delay vs utilizationFactor") + plt.show() + + +if __name__ == "__main__": + main() diff --git a/Network Lab/Switching Network Simulation/part1_plot.png b/Network Lab/Switching Network Simulation/part1_plot.png new file mode 100644 index 0000000..0874521 Binary files /dev/null and b/Network Lab/Switching Network Simulation/part1_plot.png differ diff --git a/Network Lab/Switching Network Simulation/part2.py b/Network Lab/Switching Network Simulation/part2.py new file mode 100644 index 0000000..d2983f0 --- /dev/null +++ b/Network Lab/Switching Network Simulation/part2.py @@ -0,0 +1,177 @@ +import matplotlib.pyplot as plt +import queue as Q + +utilNum = 0 + +pktDrop = 0 + +# class for pcket +class DATAPACKET: + def __init__(self,pid=0,gtime=0.0,sourceid=0): + self.packetId = pid + self.sourceId = sourceid + self.gentime = gtime + self.qInTIme = -1 + self.qOutTime = -1 + +# class for source +class DATASOURCE: + def __init__(self,lamda,sid,bs): + self.genRate = lamda + self.sourceId = sid + self.bs = bs + +# class for switch +class DATASWITCH: + def __init__(self,bwidth): + self.bss = bwidth + self.qSize = 0 + +# class for various Event +class EVENT: + def __init__(self,eid,pid,t): + self.eId = eid + self.pId = pid + self.curTime = t + + def __lt__(self, other): + return self.curTime < other.curTime + +# function to calculate pktLossRate +def calculatePktLossRate(nSource,bs,bss,pktLength,source,simTime,fixedQueueSize): + packet = [] + avgQueuingDelay = 0.0 + swich = DATASWITCH(bss) + global pktDrop + pktDrop = 0 + # pq is priority queue on the basis of event current time + pq = Q.PriorityQueue() + ttime = 0.0 + + # generating first packet from every source at an fixed interval + for i in range(nSource): + packet.append(DATAPACKET(i, ttime, i)) + pq.put(EVENT(0, i, ttime)) + ttime += 0.000001 + + pcount = 0 + pktTot = nSource + lastLeftTime = 0 + executionTime = 0.0 + packetarrived=0 + # Simulating for fixed time + # Event0 = generation of packet + # Event1 = reaching Queue time + # Event2 = leaving queue time + # Event3 = reaching sink time + curTime = 0 + + while (curTime < simTime): + x = pq.get() + pid = x.pId + curTime = x.curTime + #executionTime = max(executionTime,curTime) + + # Event 0 -> (Event0,Event1) + if x.eId == 0: + rate = source[packet[pid].sourceId].genRate + pq.put(EVENT(0, pktTot, curTime + 1 / rate)) + packet.append(DATAPACKET(pktTot, curTime + 1 / rate, packet[pid].sourceId)) + pktTot = pktTot + 1 + pq.put(EVENT(1, pid, curTime + pktLength/bs)) + packet[pid].qInTime = curTime + pktLength / bs + # Event 1 -> Event2,and if queue is full packet drop + elif x.eId == 1: + packetarrived = packetarrived+1 + if(swich.qSize<=fixedQueueSize): + rtime = (swich.qSize * pktLength) / bss + tx = 0 + if packet[pid].qInTime - lastLeftTime < (pktLength / bss): + tx = max(0, (pktLength / bss) - (packet[pid].qInTime - lastLeftTime)) + if lastLeftTime == 0: + tx = 0 + pq.put(EVENT(2, pid, curTime + rtime + tx)) + packet[pid].qOutTime = curTime + rtime + tx + swich.qSize = swich.qSize + 1 + else: + #counting pkt drop + pktDrop = pktDrop+1 + + # Event2 -> Event3 + elif x.eId == 2: + swich.qSize = swich.qSize - 1 + lastLeftTime = curTime + sTime = curTime + pktLength / bss + pq.put(EVENT(3, pid, sTime)) + else: + avgQueuingDelay = packet[pid].qOutTime - packet[pid].qInTime + avgQueuingDelay + pcount = pcount + 1 + #return pktDroprate + return pktDrop/packetarrived + +def main(): + print("Enter 0 to use default value or 1 to use own") + resp = int(input()) + if resp == 0: + # nsource = number of source + nSource = 4 + # bs = bandwidth between source and switch in bit + bs = 1e2 + # bss = bandwidth between switch and sink in bit + bsslow = 10 + bsshigh = 500 + # pktLength = size of each packet in bit + pktLength = 2 + + # grate = packet genrate + grate = 20 + + # simulation time + simTime = 200 + + #max queue size in switch + fixedQueueSize = 50 + else : + nSource = int(input("Enter Number of Source :")) + bs = float(input("Enter bandwdth between Source and switch(bs) in bit:")) + bsslow = float(input("Enter bandwidth(lower bound) between switch and sink(bss) in bit:")) + bsshigh = float(input("Enter bandwidth(upper bound) between switch and sink(bss) in bit:")) + pktLength = int(input("Enter packet length in bit(pktlength) :")) + grate = int(input("Enter packet generation rate:")) + if pktLength * grate >= bs: + print("pktLength*grater should be less than bs") + return 0 + simTime = int(input("Enter Simulation time:")) + fixedQueueSize = int(input("Enter Queue size in switch:")) + + # utilNum = numerator of utilization factor i.e arrival rate + global utilNum + # x and y holds value of delay and utilizationfactor + x = [] + y = [] + source = [] + for i in range(nSource): + source.append(DATASOURCE(grate, i, bs)) + utilNum = utilNum+grate + # varying nbss i.e bss for plotting + nbss = bsslow + while(nbss<=bsshigh): + loadfactor=(utilNum*pktLength)/nbss + pktDropRate = calculatePktLossRate(nSource,bs,nbss,pktLength,source,simTime,fixedQueueSize) + x.append(loadfactor) + y.append(pktDropRate) + print(loadfactor," ",pktDropRate) + nbss = nbss+(bsshigh-bsslow)/980 + + + # plotting curve + plt.plot(x,y) + plt.xlabel("Utilization Factor") + plt.ylabel("Packet loss rate") + plt.text(x[len(x) - 1], y[len(y) - 1], "nSource=4,bs(bit)=1e2,lambda(gen rate)=20,pktlength=2 bit, \n bsslow(bit/s)=10,bsshigh=500") + plt.title("Packet loss rate vs Utilization Factor") + plt.show() + +if __name__=="__main__": + main() + diff --git a/Network Lab/Switching Network Simulation/part2.py~ b/Network Lab/Switching Network Simulation/part2.py~ new file mode 100644 index 0000000..d2983f0 --- /dev/null +++ b/Network Lab/Switching Network Simulation/part2.py~ @@ -0,0 +1,177 @@ +import matplotlib.pyplot as plt +import queue as Q + +utilNum = 0 + +pktDrop = 0 + +# class for pcket +class DATAPACKET: + def __init__(self,pid=0,gtime=0.0,sourceid=0): + self.packetId = pid + self.sourceId = sourceid + self.gentime = gtime + self.qInTIme = -1 + self.qOutTime = -1 + +# class for source +class DATASOURCE: + def __init__(self,lamda,sid,bs): + self.genRate = lamda + self.sourceId = sid + self.bs = bs + +# class for switch +class DATASWITCH: + def __init__(self,bwidth): + self.bss = bwidth + self.qSize = 0 + +# class for various Event +class EVENT: + def __init__(self,eid,pid,t): + self.eId = eid + self.pId = pid + self.curTime = t + + def __lt__(self, other): + return self.curTime < other.curTime + +# function to calculate pktLossRate +def calculatePktLossRate(nSource,bs,bss,pktLength,source,simTime,fixedQueueSize): + packet = [] + avgQueuingDelay = 0.0 + swich = DATASWITCH(bss) + global pktDrop + pktDrop = 0 + # pq is priority queue on the basis of event current time + pq = Q.PriorityQueue() + ttime = 0.0 + + # generating first packet from every source at an fixed interval + for i in range(nSource): + packet.append(DATAPACKET(i, ttime, i)) + pq.put(EVENT(0, i, ttime)) + ttime += 0.000001 + + pcount = 0 + pktTot = nSource + lastLeftTime = 0 + executionTime = 0.0 + packetarrived=0 + # Simulating for fixed time + # Event0 = generation of packet + # Event1 = reaching Queue time + # Event2 = leaving queue time + # Event3 = reaching sink time + curTime = 0 + + while (curTime < simTime): + x = pq.get() + pid = x.pId + curTime = x.curTime + #executionTime = max(executionTime,curTime) + + # Event 0 -> (Event0,Event1) + if x.eId == 0: + rate = source[packet[pid].sourceId].genRate + pq.put(EVENT(0, pktTot, curTime + 1 / rate)) + packet.append(DATAPACKET(pktTot, curTime + 1 / rate, packet[pid].sourceId)) + pktTot = pktTot + 1 + pq.put(EVENT(1, pid, curTime + pktLength/bs)) + packet[pid].qInTime = curTime + pktLength / bs + # Event 1 -> Event2,and if queue is full packet drop + elif x.eId == 1: + packetarrived = packetarrived+1 + if(swich.qSize<=fixedQueueSize): + rtime = (swich.qSize * pktLength) / bss + tx = 0 + if packet[pid].qInTime - lastLeftTime < (pktLength / bss): + tx = max(0, (pktLength / bss) - (packet[pid].qInTime - lastLeftTime)) + if lastLeftTime == 0: + tx = 0 + pq.put(EVENT(2, pid, curTime + rtime + tx)) + packet[pid].qOutTime = curTime + rtime + tx + swich.qSize = swich.qSize + 1 + else: + #counting pkt drop + pktDrop = pktDrop+1 + + # Event2 -> Event3 + elif x.eId == 2: + swich.qSize = swich.qSize - 1 + lastLeftTime = curTime + sTime = curTime + pktLength / bss + pq.put(EVENT(3, pid, sTime)) + else: + avgQueuingDelay = packet[pid].qOutTime - packet[pid].qInTime + avgQueuingDelay + pcount = pcount + 1 + #return pktDroprate + return pktDrop/packetarrived + +def main(): + print("Enter 0 to use default value or 1 to use own") + resp = int(input()) + if resp == 0: + # nsource = number of source + nSource = 4 + # bs = bandwidth between source and switch in bit + bs = 1e2 + # bss = bandwidth between switch and sink in bit + bsslow = 10 + bsshigh = 500 + # pktLength = size of each packet in bit + pktLength = 2 + + # grate = packet genrate + grate = 20 + + # simulation time + simTime = 200 + + #max queue size in switch + fixedQueueSize = 50 + else : + nSource = int(input("Enter Number of Source :")) + bs = float(input("Enter bandwdth between Source and switch(bs) in bit:")) + bsslow = float(input("Enter bandwidth(lower bound) between switch and sink(bss) in bit:")) + bsshigh = float(input("Enter bandwidth(upper bound) between switch and sink(bss) in bit:")) + pktLength = int(input("Enter packet length in bit(pktlength) :")) + grate = int(input("Enter packet generation rate:")) + if pktLength * grate >= bs: + print("pktLength*grater should be less than bs") + return 0 + simTime = int(input("Enter Simulation time:")) + fixedQueueSize = int(input("Enter Queue size in switch:")) + + # utilNum = numerator of utilization factor i.e arrival rate + global utilNum + # x and y holds value of delay and utilizationfactor + x = [] + y = [] + source = [] + for i in range(nSource): + source.append(DATASOURCE(grate, i, bs)) + utilNum = utilNum+grate + # varying nbss i.e bss for plotting + nbss = bsslow + while(nbss<=bsshigh): + loadfactor=(utilNum*pktLength)/nbss + pktDropRate = calculatePktLossRate(nSource,bs,nbss,pktLength,source,simTime,fixedQueueSize) + x.append(loadfactor) + y.append(pktDropRate) + print(loadfactor," ",pktDropRate) + nbss = nbss+(bsshigh-bsslow)/980 + + + # plotting curve + plt.plot(x,y) + plt.xlabel("Utilization Factor") + plt.ylabel("Packet loss rate") + plt.text(x[len(x) - 1], y[len(y) - 1], "nSource=4,bs(bit)=1e2,lambda(gen rate)=20,pktlength=2 bit, \n bsslow(bit/s)=10,bsshigh=500") + plt.title("Packet loss rate vs Utilization Factor") + plt.show() + +if __name__=="__main__": + main() + diff --git a/Network Lab/Switching Network Simulation/part2_plot.png b/Network Lab/Switching Network Simulation/part2_plot.png new file mode 100644 index 0000000..bb52a6c Binary files /dev/null and b/Network Lab/Switching Network Simulation/part2_plot.png differ diff --git a/Operating System Lab/Assignment-1/1501CS16_part2/OS/1.txt b/Operating System Lab/Assignment-1/1501CS16_part2/OS/1.txt new file mode 100644 index 0000000..4325de5 --- /dev/null +++ b/Operating System Lab/Assignment-1/1501CS16_part2/OS/1.txt @@ -0,0 +1 @@ +An operating system (OS) is system software that manages hardware, software resources, provides common services for computer programs. Every general-purpose computer must have operating system to run other applications. \ No newline at end of file diff --git a/Operating System Lab/Assignment-1/1501CS16_part2/OS/2.txt b/Operating System Lab/Assignment-1/1501CS16_part2/OS/2.txt new file mode 100644 index 0000000..fb22761 --- /dev/null +++ b/Operating System Lab/Assignment-1/1501CS16_part2/OS/2.txt @@ -0,0 +1,3 @@ +Suppose you have a fibonacci sequence of length n, where n is a positive integer and +multiple of 3. Now you decide to cut down the sequence in three equal segments and do +an element-wise sum of the first and third segments. \ No newline at end of file diff --git a/Operating System Lab/Assignment-1/1501CS16_part2/OS/3.txt b/Operating System Lab/Assignment-1/1501CS16_part2/OS/3.txt new file mode 100644 index 0000000..1b75d87 --- /dev/null +++ b/Operating System Lab/Assignment-1/1501CS16_part2/OS/3.txt @@ -0,0 +1,4 @@ +A folder named ​OS contains four non-empty and one empty text files. Each of the +non-empty files contains different number of sentences. Write a program in shell script to +copy the first sentence from each non-empty file to the empty file. The sentences should +be placed based on the ascending order of the size of the non-empty files. \ No newline at end of file diff --git a/Operating System Lab/Assignment-1/1501CS16_part2/OS/4.txt b/Operating System Lab/Assignment-1/1501CS16_part2/OS/4.txt new file mode 100644 index 0000000..d83cd0e --- /dev/null +++ b/Operating System Lab/Assignment-1/1501CS16_part2/OS/4.txt @@ -0,0 +1 @@ +Keep forced air, radiant and gas heaters at least 3 feet away from flammable materials, including the walls. Oil heaters and those with infrared bulbs can be placed within a foot of the walls, but never allow them to be in direct contact with anything other than the surface on which they stand. \ No newline at end of file diff --git a/Operating System Lab/Assignment-1/1501CS16_part2/OS/result.txt b/Operating System Lab/Assignment-1/1501CS16_part2/OS/result.txt new file mode 100644 index 0000000..61fbe47 --- /dev/null +++ b/Operating System Lab/Assignment-1/1501CS16_part2/OS/result.txt @@ -0,0 +1,4 @@ +An operating system (OS) is system software that manages hardware, software resources, provides common services for computer programs. +Suppose you have a fibonacci sequence of length n, where n is a positive integer and multiple of 3. +Keep forced air, radiant and gas heaters at least 3 feet away from flammable materials, including the walls. +A folder named ​OS contains four non-empty and one empty text files. diff --git a/Operating System Lab/Assignment-1/1501CS16_part2/q3.sh b/Operating System Lab/Assignment-1/1501CS16_part2/q3.sh new file mode 100755 index 0000000..9e17df8 --- /dev/null +++ b/Operating System Lab/Assignment-1/1501CS16_part2/q3.sh @@ -0,0 +1,45 @@ +#! /bin/bash +# fibonacci sequence 0 1 1 2 3 5 8 ....... +echo "Enter an odd number n for Fibonacci series " +read n +x=0 +y=1 +i=2 +#array array stores the fibonacci sequencce +#code segment for fibonacci sequence generation +array[0]=0 +array[1]=1 +while [ $i -lt $n ] +do + z=`expr $x + $y ` + array[$i]=$z + # echo ${array[$i]} + i=`expr $i + 1 ` + x=$y + y=$z +done + +echo "Requred series" +# calculate n/3 +# array arr stores the element wise sum of first and third segment +#code segment for arr generation +p=`expr $n / 3` +i=0 +while [ $i -lt $p ] +do + ((val=i+p+p)) + ((arr[$i]=array[$i]+array[$val])) + # echo ${arr[$i]} + ((i=i+1)) +done +i=$p +((x=p+p)) +# storing the mid segement array array in array arr +while [ $i -lt $x ] +do + ((arr[$i]=array[$i])) + # echo ${array[$i]} + ((i=i+1)) +done +#Printing the required result +echo ${arr[*]} diff --git a/Operating System Lab/Assignment-1/1501CS16_part2/q4.sh b/Operating System Lab/Assignment-1/1501CS16_part2/q4.sh new file mode 100755 index 0000000..8a8c846 --- /dev/null +++ b/Operating System Lab/Assignment-1/1501CS16_part2/q4.sh @@ -0,0 +1,19 @@ +#! /bin/bash +####sentence : anything ending with full stop is considered sentence here +#this conditional if will check whether our empty file i.e result.txt is empty or not if not make empty + +if [ -s ./OS/result.txt ] +then + echo -n "" > ./OS/result.txt +fi +# os folder contai four nonempty file :-1)1.txt,2)2.txt,3)3.txt,4)4.txt and empty file namely result.txt +#file contain the file in ascending order of size +file=`ls -r -S ./OS/ | tail -4` +# for each file in file var it opened it copy the first sentence which is marked by full stop +# and append it to empty file result.txt +for line in $file +do + cat < ./OS/$line | tr "\n" " " | tr "." "\n" | head -1 |tr "\n" "." >> ./OS/result.txt + #this helps to print every sentence in new line + echo " " >>./OS/result.txt +done \ No newline at end of file diff --git a/Operating System Lab/Assignment-1/Assignment 1.pdf b/Operating System Lab/Assignment-1/Assignment 1.pdf new file mode 100644 index 0000000..cbad266 Binary files /dev/null and b/Operating System Lab/Assignment-1/Assignment 1.pdf differ diff --git a/Operating System Lab/Assignment-1/Q1.txt b/Operating System Lab/Assignment-1/Q1.txt new file mode 100644 index 0000000..e1fae55 --- /dev/null +++ b/Operating System Lab/Assignment-1/Q1.txt @@ -0,0 +1,25 @@ +1. +a : cpu = 4 + core = 2 + +b. freq1 = 1331.531 Mhz + freq2 = 1700.062 Mhz + freq3 = 1130.625 Mhz + freq4 = 800.531 Mhz + +c. memory = 3883 MB + +d. free = 489 MB + available = 1476 MB + difference : + available memory = free memory + cached memory + free memory = free memory + + free memory = This is memory that is currently unused by the system and contains no useful data at all. + It is free to be used by the system at any time. + available memory = While this sounds extremely similar to free memory, + it is actually a broader category that includes both free memory and cached memory. + +e. total number of user-level processes = 100 + + \ No newline at end of file diff --git a/Operating System Lab/Assignment-1/bash-progming.pdf b/Operating System Lab/Assignment-1/bash-progming.pdf new file mode 100644 index 0000000..22d6814 Binary files /dev/null and b/Operating System Lab/Assignment-1/bash-progming.pdf differ diff --git a/Operating System Lab/Assignment-1/q2.sh b/Operating System Lab/Assignment-1/q2.sh new file mode 100755 index 0000000..bed5268 --- /dev/null +++ b/Operating System Lab/Assignment-1/q2.sh @@ -0,0 +1,7 @@ +#! /bin/sh +echo "Number of Unique words " +cat < welcome.txt|tr -d ['.''('')'',']|tr ' ' '\n'| sort -u | wc -w + +echo "Maximum frequency and Occurence word " + +tr ['.''('')'','] '\0' < welcome.txt|tr ' ' '\n'| sort | uniq -c | sort -nr | head -1 \ No newline at end of file diff --git a/Operating System Lab/Assignment-1/q3.sh b/Operating System Lab/Assignment-1/q3.sh new file mode 100755 index 0000000..4bafc22 --- /dev/null +++ b/Operating System Lab/Assignment-1/q3.sh @@ -0,0 +1,37 @@ +#! /bin/bash +echo "Enter an odd number n for Fibonacci series " +read n +x=0 +y=1 +i=2 +array[0]=0 +array[1]=1 +while [ $i -lt $n ] +do + z=`expr $x + $y ` + array[$i]=$z + # echo ${array[$i]} + i=`expr $i + 1 ` + x=$y + y=$z +done + +echo "Requred series" +p=`expr $n / 3` +i=0 +while [ $i -lt $p ] +do + ((val=i+p+p)) + ((arr[$i]=array[$i]+array[$val])) + # echo ${arr[$i]} + ((i=i+1)) +done +i=$p +((x=p+p)) +while [ $i -lt $x ] +do + ((arr[$i]=array[$i])) + # echo ${array[$i]} + ((i=i+1)) +done +echo ${arr[*]} diff --git a/Operating System Lab/Assignment-1/welcome.txt b/Operating System Lab/Assignment-1/welcome.txt new file mode 100644 index 0000000..39f4472 --- /dev/null +++ b/Operating System Lab/Assignment-1/welcome.txt @@ -0,0 +1 @@ +An operating system (OS) is system OS software that manages hardware, software resources, provides common services for computer programs. Every general-purpose computer must have operating system to run other applications. \ No newline at end of file diff --git a/Operating System Lab/Assignment-10/Assignment 10/1501cs16/MMU.c b/Operating System Lab/Assignment-10/Assignment 10/1501cs16/MMU.c new file mode 100644 index 0000000..9cd0830 --- /dev/null +++ b/Operating System Lab/Assignment-10/Assignment 10/1501cs16/MMU.c @@ -0,0 +1,123 @@ +//----------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "PageTable.h" +//----------------------------------------------------------------------------- +//----Used for delayed tasks +void ContinueHandler(int Signal) { +//----Nothing to do +} +//----------------------------------------------------------------------------- +void PrintPageTable(page_table_entry PageTable[],int NumberOfPages) { + + int Index; + + for (Index = 0;Index < NumberOfPages;Index++) + { + printf("%2d: Valid=%1d Frame=%2d Dirty=%1d Requested=%1d\n",Index, +PageTable[Index].Valid,PageTable[Index].Frame,PageTable[Index].Dirty, +PageTable[Index].Requested); + } + +} +//----------------------------------------------------------------------------- +int main(int argc,char *argv[]) +{ + int SharedMemoryKey; + int NumberOfPages; + int OSPID; + int SegmentID; + page_table_pointer PageTable; + int RSIndex; + int Mode; + int Page; + + if (argc < 2 ||(OSPID = SharedMemoryKey = atoi(argv[argc-1])) == 0 ||(NumberOfPages = atoi(argv[1])) == 0) + { + printf("Usage:"); + exit(EXIT_FAILURE); + } + +//----Create the page table + if ((SegmentID = shmget(SharedMemoryKey,NumberOfPages*sizeof(page_table_entry),0)) == -1 || +(PageTable = (page_table_pointer)shmat(SegmentID,NULL,0)) == NULL) + { + perror("ERROR: Could not get page table"); + exit(EXIT_FAILURE); + } + +//----Handler for page fault + if (signal(SIGCONT,ContinueHandler) == SIG_ERR) + { + printf("ERROR: Could not initialize continue handler\n"); + exit(EXIT_FAILURE); + } + + printf("Initialized page table:\n"); + PrintPageTable(PageTable,NumberOfPages); + printf("\n"); +//----Deal with the page requests + for (RSIndex = 2;RSIndex < argc-1;RSIndex++) { + Mode = argv[RSIndex][0]; + Page = atoi(&argv[RSIndex][1]); +//----Check that it's within the process + if (Page >= NumberOfPages) { + printf("ERROR: That page number in %c%d is outside the process\n", +Mode,Page); + } else { + printf("Request for page %d in %c mode\n",Page,Mode); +//----Check if in memory + if (!PageTable[Page].Valid) { + printf("It's not in RAM - page fault\n"); + PageTable[Page].Requested = getpid(); +//----Sleep a bit to allow OS to get ready for another signal + sleep(1); + if (kill(OSPID,SIGUSR1) == -1) { + perror("Kill to OS"); + exit(EXIT_FAILURE); + } + pause(); + if (!PageTable[Page].Valid) { + printf("Bugger, something wrong\n"); + } + } else { + printf("It's in RAM\n"); + } +//----If write mode, set the dirty bit + if (Mode == 'W') { + printf("Set the dirty bit for page %d\n",Page); + PageTable[Page].Dirty = 1; + } + PrintPageTable(PageTable,NumberOfPages); + printf("\n"); + } + } + +//----Free the shared memory + if (shmdt(PageTable) == -1) { + perror("ERROR: Error detaching segment"); + exit(EXIT_FAILURE); + } + +//----Alert OS + printf("Tell OS that I'm finished\n"); +//----Sleep a bit to allow OS to get ready for another signal + sleep(1); + if (kill(OSPID,SIGUSR1) == -1) { + perror("Kill to OS"); + exit(EXIT_FAILURE); + } + + return(EXIT_SUCCESS); +} +//----------------------------------------------------------------------------- diff --git a/Operating System Lab/Assignment-10/Assignment 10/1501cs16/OS.c b/Operating System Lab/Assignment-10/Assignment 10/1501cs16/OS.c new file mode 100644 index 0000000..1f19cfa --- /dev/null +++ b/Operating System Lab/Assignment-10/Assignment 10/1501cs16/OS.c @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "PageTable.h" +#include + +int page,frame; +page_table_pointer PageTable; +int diskaccess=0; +int * FrameTable; + +//Function to initialize Page Table +void initializePageTable() +{ + for(int i=0;i +#include +#include + +char* fileMap[100000]; +int mapIdx[100000]; +int mapEndIdx[100000]; +void printMenu() +{ + printf("Enter : \n"); + printf("1. To add File\n"); + printf("2. To Print Directory \n"); + printf("3. TO exit\n"); + +} + +int main() +{ + int numBlock; + printf("Welcome to the Sequential File\n"); + printf("Enter the number of Block in disk\n"); + scanf("%d",&numBlock); + int* disk = malloc(sizeof(int)*numBlock); + for(int i=0;i +#include +#include + +int* arr[1000][100000]; +char* fileMap[100000]; +int mapIdx[100000]; +int sz[100000]; + +void printMenu() +{ + printf("Enter : \n"); + printf("1. To add File\n"); + printf("2. To Print Directory \n"); + printf("3. TO exit\n"); + +} + +int main() +{ + int numBlock; + printf("Welcome to the indexed File\n"); + printf("Enter the number of Block in disk\n"); + scanf("%d",&numBlock); + int* disk = malloc(sizeof(int)*numBlock); + for(int i=0;i=numBlock ||disk[iBlock]!=-1) + { + printf("Index Block is not empty or invalid\n"); + continue; + } + + int* freeBlock = malloc(sizeof(int)*tBlock); + int j=0; + int cnt = 0; + disk[iBlock] = totFile; + for(int i=0;i +#include +#include + +struct node +{ + struct node* ptr; + int val; +}; + +struct node* arr[100]; +char* fileMap[100]; +int mapIdx[100]; +int sz[100]; + +void printMenu() +{ + printf("Enter : \n"); + printf("1. To add File\n"); + printf("2. To Print Directory \n"); + printf("3. TO exit\n"); + +} + +int main() +{ + int numBlock; + printf("Welcome to the linked list File\n"); + printf("Enter the number of Block in disk\n"); + scanf("%d",&numBlock); + int* disk = malloc(sizeof(int)*numBlock); + for(int i=0;i=numBlock ||disk[sBlock]!=-1) + { + printf("Start Block is not empty or invalid\n"); + continue; + } + int* freeBlock = malloc(sizeof(int)*tBlock); + int j=0; + int cnt = 0; + freeBlock[0] = sBlock; + cnt++; + for(int i=1;ival = freeBlock[i]; + idx->ptr = NULL; + if(head == NULL) + { + arr[totFile] =idx; + head = idx; + } + else + { + head->ptr = idx; + head = idx; + } + disk[freeBlock[i]] = totFile; + } + printf("\n"); + fileMap[totFile] = fName; + sz[totFile]=tBlock; + totFile++; + } + else + { + printf("Not Enough free Space\n"); + } + } + else if(choice == 2) + { + printf("File name Block Stored\n"); + for(int i=0;i",head->val); + head = head->ptr; + } + printf("NULL\n"); + } + } + else if(choice == 3) + { + break; + } + else + { + printf("Invalid Input\n"); + } + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-11/1501cs16/Q3_Output.txt b/Operating System Lab/Assignment-11/1501cs16/Q3_Output.txt new file mode 100644 index 0000000..9092928 --- /dev/null +++ b/Operating System Lab/Assignment-11/1501cs16/Q3_Output.txt @@ -0,0 +1,60 @@ +//Output +darkmatter@hp-15:~/Dropbox/oslab/Assignment-11$ gcc Q3.c +darkmatter@hp-15:~/Dropbox/oslab/Assignment-11$ ./a.out +Welcome to the linked list File +Enter the number of Block in disk +30 +Enter : +1. To add File +2. To Print Directory +3. TO exit +1 +Enter File name,start block and total number of block in file +f1 23 6 + +Enter : +1. To add File +2. To Print Directory +3. TO exit +2 +File name Block Stored +f1 23 ->0 ->1 ->2 ->3 ->4 ->NULL +Enter : +1. To add File +2. To Print Directory +3. TO exit +1 +Enter File name,start block and total number of block in file +f2 4 5 +Start Block is not empty or invalid +Enter : +1. To add File +2. To Print Directory +3. TO exit +1 +Enter File name,start block and total number of block in file +f3 29 24 + +Enter : +1. To add File +2. To Print Directory +3. TO exit +2 +File name Block Stored +f1 23 ->0 ->1 ->2 ->3 ->4 ->NULL +f3 29 ->5 ->6 ->7 ->8 ->9 ->10 ->11 ->12 ->13 ->14 ->15 ->16 ->17 ->18 ->19 ->20 ->21 ->22 ->24 ->25 ->26 ->27 ->28 ->NULL +Enter : +1. To add File +2. To Print Directory +3. TO exit +4 +Invalid Input +Enter : +1. To add File +2. To Print Directory +3. TO exit +3 +darkmatter@hp-15:~/Dropbox/oslab/Assignment-11$ + +//Explanation: +In linked file system a start block is used which stores the address of next block for the corresponding file. \ No newline at end of file diff --git a/Operating System Lab/Assignment-11/Assignment 11.pdf b/Operating System Lab/Assignment-11/Assignment 11.pdf new file mode 100644 index 0000000..a49e3c7 Binary files /dev/null and b/Operating System Lab/Assignment-11/Assignment 11.pdf differ diff --git a/Operating System Lab/Assignment-11/File Allocation.pptx b/Operating System Lab/Assignment-11/File Allocation.pptx new file mode 100644 index 0000000..9b10f70 Binary files /dev/null and b/Operating System Lab/Assignment-11/File Allocation.pptx differ diff --git a/Operating System Lab/Assignment-12/Assignment 12.pdf b/Operating System Lab/Assignment-12/Assignment 12.pdf new file mode 100644 index 0000000..bc5384c Binary files /dev/null and b/Operating System Lab/Assignment-12/Assignment 12.pdf differ diff --git a/Operating System Lab/Assignment-12/Q1.cpp b/Operating System Lab/Assignment-12/Q1.cpp new file mode 100644 index 0000000..7eb4bfe --- /dev/null +++ b/Operating System Lab/Assignment-12/Q1.cpp @@ -0,0 +1,299 @@ +/************* + Author - am10 + ******************/ +#include + +#define sync ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(0); +#define endl '\n' +#define ll long long +#define pb push_back +#define PI acos(-1) +#define pii pair +#define FI first +#define SE second + +/* +//D-S-U +int root(int v){return par[v] < 0 ? v : (par[v] = root(par[v]));} +void merge(int x,int y){ // x and y are some tools (vertices) + if((x = root(x)) == (y = root(y)) return ; + if(par[y] < par[x]) // balancing the height of the tree + swap(x, y); + par[x] += par[y]; + par[y] = x; +} +*/ + + +using namespace std; + +int cylinderCount = 200; +//number of request +int sz = 8; +void FCFS(int dHead,int* workQueue) +{ + cout << "FCFS disk Scheduling Algorithm :" << endl; + cout << "Request Processed Order :" << endl; + //Total Head movement + int THM = 0; + //Serving each request in order of arrival + for(int i=0;i "; + } + cout <<"null"<< endl; + cout << "Total Head movement is " << THM << " and seek time is " << THM*5 << "ms\n" << endl; +} + +void SCAN(int dHead,int* workQueue) +{ + cout << "SCAN disk Scheduling Algorithm: " << endl; + cout << "Request Processed Order :" << endl; + int dir = 0; // 0 for left and 1 for right + int st = INT_MAX; + //Finding which direction to move + for(int i=0;i "; + tot++; + } + //if left movement + if(dir == 0) + { + for(int i=dHead-1;i>=0&&tot "; + } + } + //moving extra left + if(tot!=sz) + { + THM+=dHead; + dHead = 0; + } + for(int i=0;i<200 && tot < sz;i++) + { + if(available[i]==1) + { + tot++; + THM+=abs(dHead-i); + dHead = i; + cout << dHead << "--> "; + available[i]--; + } + } + } + else + { + for(int i=dHead+1;i<200 && tot "; + } + } + //moving extra right + if(tot!=sz) + { + THM+=abs(199-dHead); + dHead = 199; + } + for(int i=199;i>=0 && tot < sz;i--) + { + if(available[i]==1) + { + tot++; + THM+=abs(dHead-i); + dHead = i; + available[i]--; + cout << dHead << "--> "; + } + } + } + cout<<"null" << endl; + cout << "Total Head movement is " << THM << " and seek time is " << THM*5 << "ms\n" << endl; + +} + +void SSTF(int dHead,int* workQueue) +{ + cout << "SSTF disk Scheduling Algorithm: " << endl; + cout << "Request Processed Order :" << endl; + int size = sz; + int THM = 0; + int pRequest; + //to check which request has been processed + int* available = new int[sz]; + for(int i=0;i "; + } + cout <<"null"<< endl; + cout << "Total Head movement is " << THM << " and seek time is " << THM*5 << "ms\n" << endl; + +} + +void CSCAN(int dHead,int* workQueue) +{ + cout << "CSCAN disk Scheduling Algorithm: " << endl; + cout << "Request Processed Order :" << endl; + int dir = 0; // 0 for left and 1 for right + int st = INT_MAX; + for(int i=0;i "; + tot++; + } + //if left movement + if(dir == 0) + { + for(int i=dHead-1;i>=0&&tot "; + } + } + //extra left + if(tot!=sz) + { + THM+=dHead; + dHead = 0; + } + //as CSCAN can move from 0 to 199 in one movement but no head moveement + dHead = 199; + for(int i=199;i>=0&& tot "; + } + } + } + else + { + for(int i=dHead+1;i<200 && tot "; + } + } + if(tot!=sz) + { + THM+=abs(199-dHead); + dHead = 0; + } + //as CSCAN move to 0 from 199 + dHead = 0; + for(int i=0;i<200 && tot < sz;i++) + { + if(available[i]==1) + { + tot++; + THM+=abs(dHead-i); + dHead = i; + available[i]--; + cout << dHead << "--> "; + } + } + } + cout <<"null" << endl; + cout << "Total Head movement is " << THM << " and seek time is " << THM*5 << "ms\n" << endl; + +} + +int main() +{ + sync; + int diskHead = 50; + //input request + int workQueue[] = {95, 180, 34, 119, 11, 123, 62, 64 }; + + //Calling Various Algorithm + FCFS(diskHead,workQueue); + SCAN(diskHead,workQueue); + CSCAN(diskHead,workQueue); + SSTF(diskHead,workQueue); +} diff --git a/Operating System Lab/Assignment-12/Q1_Output.txt b/Operating System Lab/Assignment-12/Q1_Output.txt new file mode 100644 index 0000000..3c8d6de --- /dev/null +++ b/Operating System Lab/Assignment-12/Q1_Output.txt @@ -0,0 +1,30 @@ +//Output +darkmatter@hp-15:~/Dropbox/oslab/Assignment-12$ g++ Q1.cpp +darkmatter@hp-15:~/Dropbox/oslab/Assignment-12$ ./a.out +FCFS disk Scheduling Algorithm : +Request Processed Order : +95--> 180--> 34--> 119--> 11--> 123--> 62--> 64--> null +Total Head movement is 644 and seek time is 3220ms + +SCAN disk Scheduling Algorithm: +Request Processed Order : +62--> 64--> 95--> 119--> 123--> 180--> 34--> 11--> null +Total Head movement is 337 and seek time is 1685ms + +CSCAN disk Scheduling Algorithm: +Request Processed Order : +62--> 64--> 95--> 119--> 123--> 180--> 11--> 34--> null +Total Head movement is 183 and seek time is 915ms + +SSTF disk Scheduling Algorithm: +Request Processed Order : +62--> 64--> 34--> 11--> 95--> 119--> 123--> 180--> null +Total Head movement is 236 and seek time is 1180ms + + + +//Explanation : +FCFS : Job which requested first is served first. +SCAN : Tie is broken to move left or right by finding the direction with shortest seek time. +CSCAN : Same as SCAN but when at one end it reaches the other by just one movement. +SSTF : job with shortest seek time is served at each instance. \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Assignment 2.pdf b/Operating System Lab/Assignment-2/Assignment 2.pdf new file mode 100644 index 0000000..3785cff Binary files /dev/null and b/Operating System Lab/Assignment-2/Assignment 2.pdf differ diff --git a/Operating System Lab/Assignment-2/Q1.c b/Operating System Lab/Assignment-2/Q1.c new file mode 100644 index 0000000..911e590 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q1.c @@ -0,0 +1,29 @@ +#include +#include +#include + +int main() +{ + //Creating new process and pid holds the child process id in parent process and + //0 in child process + pid_t pid = fork(); + + //If process is not created + if(pid<0) + { + printf("Child Process can't be created\n"); + return 0; + } + //If pid == 0 child process + if(pid==0) + { + //getppid() gets process id of parent + pid_t parentid= getppid(); + printf("Parent Process ID from child process is %d\n",parentid); + } + else //Block for parent process + { + // pid holds child process + printf("Child Process id from parent process is %d\n",pid); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q1_Explanation.txt b/Operating System Lab/Assignment-2/Q1_Explanation.txt new file mode 100644 index 0000000..31aff2e --- /dev/null +++ b/Operating System Lab/Assignment-2/Q1_Explanation.txt @@ -0,0 +1,2 @@ +// Explain : +To get the parent process id we used getppid() command in child process,and in parent process pid gives the process id for child process. diff --git a/Operating System Lab/Assignment-2/Q1_output.txt b/Operating System Lab/Assignment-2/Q1_output.txt new file mode 100644 index 0000000..74dec30 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q1_output.txt @@ -0,0 +1,4 @@ +Output : +Child Process id from parent process is 14768 +Parent Process ID from child process is 14767 + diff --git a/Operating System Lab/Assignment-2/Q2.c b/Operating System Lab/Assignment-2/Q2.c new file mode 100644 index 0000000..bfe0dab --- /dev/null +++ b/Operating System Lab/Assignment-2/Q2.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +int factorial() +{ + //Blocks parent till child executes + wait(NULL); + //Printing current process id + printf("Current Process id is %d\n",getpid()); + int n; + printf("Enter the value of n to find factorial \n"); + //taking input + scanf("%d",&n); + if(n==0) + return 1; + //iterative function to return factorial + int factorial=1; + for(int i=1;i<=n;i++) + factorial = factorial*i; + return factorial; +} + +int main() +{ + //Factorial function call befor fork + printf("The factorial before fork is %d\n",factorial()); + //Fork() system call to create new process + fork(); + //Factorial call after fork(),now it will be called twice one for child + //and one for parent process + printf("The factorial after fork is %d\n",factorial()); +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q2_Explanation.txt b/Operating System Lab/Assignment-2/Q2_Explanation.txt new file mode 100644 index 0000000..ec887e2 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q2_Explanation.txt @@ -0,0 +1,3 @@ +// Explanation : +Factorial() function used before fork() command is run in parent process as till now only parent process exist. +After fork a new child process is created which will then run separately with parent process, so after fork we get twice two enter the value of n twice one for parent process and one for child process. \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q2_output.txt b/Operating System Lab/Assignment-2/Q2_output.txt new file mode 100644 index 0000000..2e4e06b --- /dev/null +++ b/Operating System Lab/Assignment-2/Q2_output.txt @@ -0,0 +1,13 @@ +OUTPUT: +Current Process id is 14940 +Enter the value of n to find factorial +5 +The factorial before fork is 120 +Current Process id is 14942 +Enter the value of n to find factorial +4 +The factorial after fork is 24 +Current Process id is 14940 +Enter the value of n to find factorial +6 +The factorial after fork is 720 \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q3.c b/Operating System Lab/Assignment-2/Q3.c new file mode 100644 index 0000000..96038e7 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q3.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +int glob = 50; + +int main() +{ + //Creating new process and pid holds the child process id in parent process and + //0 in child process + pid_t pid = fork(); + + //if child process can't be created + if(pid<0) + { + printf("Child Process can't be created\n"); + return 0; + } + + // As both process uses separate address space that's why the change in + // global variabe glob is different. + //If pid == 0 child process + if(pid==0) + { + //getppid() gets process id of parent + pid_t curid= getpid(); + printf("child Process ID is %d\n",curid); + printf("value of glob currently (c1) is %d \n",glob); + //updating global variable + glob = 10; + printf("value of glob currently after updating (c2) is %d \n",glob); + + } + else //Block for parent process + { + //wait blocks the current execution of parent process and wait + //for child to finish + wait(NULL); + // pid holds child process + printf("Parent Process id is %d\n",getpid()); + printf("value of glob currently (p1) is %d \n",glob); + //updating global variable + glob = 13; + printf("value of glob currently (p2) after updating is %d \n",glob); + } + //printf("%d\n",glob); +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q3_Explanation.txt b/Operating System Lab/Assignment-2/Q3_Explanation.txt new file mode 100644 index 0000000..da59f78 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q3_Explanation.txt @@ -0,0 +1,2 @@ +//Explanation: +As before fork() there was only one process with glob initialized to 50.After fork() two identical address space is created one for parent and other for child process.both have glob initialized to 50,But as they have separate address space the change in glob variable will be independent i.e they do not interact with each other. \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q3_output.txt b/Operating System Lab/Assignment-2/Q3_output.txt new file mode 100644 index 0000000..481fd9b --- /dev/null +++ b/Operating System Lab/Assignment-2/Q3_output.txt @@ -0,0 +1,8 @@ +OUTPUT: + child Process ID is 15389 +value of glob currently (c1) is 50 +value of glob currently after updating (c2) is 10 +Parent Process id is 15388 +value of glob currently (p1) is 50 +value of glob currently (p2) after updating is 13 + diff --git a/Operating System Lab/Assignment-2/Q4.c b/Operating System Lab/Assignment-2/Q4.c new file mode 100644 index 0000000..0569869 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q4.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +int main(int argc ,char** argv) +{ + + //Checking for valid input + if(argc != 2) + { + printf("Invalid Input\n"); + } + char* c = argv[1]; + int t = atoi(c); + + //Creating new process and pid holds the child process id in parent process and + //0 in child process + pid_t pid = fork(); + + if(pid<0) + { + printf("Child Process can't be created\n"); + return 0; + } + //If pid == 0 child process + if(pid==0) + { + //make child process for t second + sleep(t); + //getppid() gets process id of parent + pid_t curid= getpid(); + printf("child Process ID is %d\n",curid); + //Listing process inside child process + system("ps"); + + } + else //Block for parent process + { + printf("Parent Process id is %d\n",getpid()); + //Listing process inside child process + system("ps"); + wait(NULL); + // pid holds child process + printf("Parent Process id is %d\n",getpid()); + //Listing process inside child process + system("ps"); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q4_Explanation.txt b/Operating System Lab/Assignment-2/Q4_Explanation.txt new file mode 100644 index 0000000..e969181 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q4_Explanation.txt @@ -0,0 +1,2 @@ +//Explanation : +First there is a parent process which creates the child process using fork() command.In order to display the process we have used system("ps") command which display the process running in child or parent process.And input is in sec and it is taken by command line. \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q4_output.txt b/Operating System Lab/Assignment-2/Q4_output.txt new file mode 100644 index 0000000..ac27c12 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q4_output.txt @@ -0,0 +1,45 @@ +OUTPUT: +Parent Process id is 16016 + PID TTY TIME CMD + 7060 pts/2 00:00:00 bash + 8748 pts/2 00:00:00 a.out + 8819 pts/2 00:00:00 a.out + 8833 pts/2 00:00:00 a.out +13485 pts/2 00:00:00 man +13497 pts/2 00:00:00 pager +14064 pts/2 00:00:00 Q5.sh +14065 pts/2 00:00:00 cat +14140 pts/2 00:00:00 cat +16016 pts/2 00:00:00 a.out +16017 pts/2 00:00:00 a.out +16018 pts/2 00:00:00 sh +16019 pts/2 00:00:00 ps +child Process ID is 16017 + PID TTY TIME CMD + 7060 pts/2 00:00:00 bash + 8748 pts/2 00:00:00 a.out + 8819 pts/2 00:00:00 a.out + 8833 pts/2 00:00:00 a.out +13485 pts/2 00:00:00 man +13497 pts/2 00:00:00 pager +14064 pts/2 00:00:00 Q5.sh +14065 pts/2 00:00:00 cat +14140 pts/2 00:00:00 cat +16016 pts/2 00:00:00 a.out +16017 pts/2 00:00:00 a.out +16030 pts/2 00:00:00 sh +16031 pts/2 00:00:00 ps +Parent Process id is 16016 + PID TTY TIME CMD + 7060 pts/2 00:00:00 bash + 8748 pts/2 00:00:00 a.out + 8819 pts/2 00:00:00 a.out + 8833 pts/2 00:00:00 a.out +13485 pts/2 00:00:00 man +13497 pts/2 00:00:00 pager +14064 pts/2 00:00:00 Q5.sh +14065 pts/2 00:00:00 cat +14140 pts/2 00:00:00 cat +16016 pts/2 00:00:00 a.out +16032 pts/2 00:00:00 sh +16033 pts/2 00:00:00 ps \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q5.sh b/Operating System Lab/Assignment-2/Q5.sh new file mode 100755 index 0000000..e2180bc --- /dev/null +++ b/Operating System Lab/Assignment-2/Q5.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +cat < pipe1.txt | sed "s/peeps/pipes/g" > pipenew.txt \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/Q5_Explanation.txt b/Operating System Lab/Assignment-2/Q5_Explanation.txt new file mode 100644 index 0000000..44e82f4 --- /dev/null +++ b/Operating System Lab/Assignment-2/Q5_Explanation.txt @@ -0,0 +1 @@ +First we used cat command to extract text and pass it to sed command through pipe which substitute "peeps" with "pipes" and finally write to pipenew.txt diff --git a/Operating System Lab/Assignment-2/pipe1.txt b/Operating System Lab/Assignment-2/pipe1.txt new file mode 100644 index 0000000..d367d22 --- /dev/null +++ b/Operating System Lab/Assignment-2/pipe1.txt @@ -0,0 +1,3 @@ +This is an example file testing the use of peeps on the command line. peeps +provide the user with a synchronized means of interprocess communication. +Two kinds of peeps exist: named or unnamed. \ No newline at end of file diff --git a/Operating System Lab/Assignment-2/pipenew.txt b/Operating System Lab/Assignment-2/pipenew.txt new file mode 100644 index 0000000..0a4dd15 --- /dev/null +++ b/Operating System Lab/Assignment-2/pipenew.txt @@ -0,0 +1,3 @@ +This is an example file testing the use of pipes on the command line. pipes +provide the user with a synchronized means of interprocess communication. +Two kinds of pipes exist: named or unnamed. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16/Q1.c b/Operating System Lab/Assignment-3/1501cs16/Q1.c new file mode 100644 index 0000000..23a4eab --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q1.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +int main() +{ + //creating new process + pid_t pid = fork(); + if(pid < 0) + { + printf("Child Process can't be created\n"); + return 0; + } + + if(pid == 0) + { + // if child process exit + exit(0); + } + else + { + // wait for child to become zombie + sleep(5); + //printing process table for zombie + printf("Child Process Id is %d\n and parent process id %d\n",pid,getpid()); + system("ps -lA | grep '^. Z'"); + wait(NULL); + printf("Checking after zombie is cleaned\n"); + //No zombie with process id of child process now + system("ps -lA | grep '^. Z'"); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16/Q1_Explanation.txt b/Operating System Lab/Assignment-3/1501cs16/Q1_Explanation.txt new file mode 100644 index 0000000..f2f8186 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q1_Explanation.txt @@ -0,0 +1,3 @@ +/* Explanation */ + +Zombie process are those process which are not cleared from process table.Here in this case 12707 become the zombie process as it exited before parent wait() is call so it become zombie. After parent wait() is called process table is cleared and no zombie child is avalable now. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16/Q1_output.txt b/Operating System Lab/Assignment-3/1501cs16/Q1_output.txt new file mode 100644 index 0000000..1ff2014 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q1_output.txt @@ -0,0 +1,10 @@ +//Output : + +Child Process Id is 12707 + and parent process id 12706 + PID PPID +1 Z 1000 12707 12706 0 80 0 - 0 exit pts/4 00:00:00 a.out + +Checking after zombie is cleaned // no process + + diff --git a/Operating System Lab/Assignment-3/1501cs16/Q2.c b/Operating System Lab/Assignment-3/1501cs16/Q2.c new file mode 100644 index 0000000..108f72e --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q2.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +int main() +{ + // creating new process + pid_t pid = fork(); + if(pid < 0) + { + printf("Child Process can't be created\n"); + return 0; + } + //child process + if(pid == 0) + { + sleep(5); + printf("Parent Process is killed so child process is Orphan process"); + //printing current process + system("ps -l"); + } + else + { + // printing child process and parent process id + printf("Child Process id is %d\n and Parent process id is %d\n", pid,getpid()); + printf("This is Parent process\n"); + // exiting parent process to make child orphan + exit(0); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16/Q2_Explanation.txt b/Operating System Lab/Assignment-3/1501cs16/Q2_Explanation.txt new file mode 100644 index 0000000..22caeef --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q2_Explanation.txt @@ -0,0 +1,8 @@ +//my init() has id 1638 by default 1 + +/*Explanation*/ +Orphan process are those process which exist even after its parent is dead.Here parent process id is 8330 which is killed before child process so orphan child is handled by init() process which can be seen in the output above i.e pid 1638(in my case) for orphan process pid 8331. + +Significance of Orphan process : +A process may also be intentionally orphaned so that it becomes detached from the user’s session and left running in the background. +Usually to allow a long-running job to complete without further user attention, or to start an indefinitely running service \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16/Q2_output.txt b/Operating System Lab/Assignment-3/1501cs16/Q2_output.txt new file mode 100644 index 0000000..a380e1a --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q2_output.txt @@ -0,0 +1,17 @@ +//Output +Child Process id is 8331 + and Parent process id is 8330 +This is Parent process + +F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD +0 S 1000 4544 4537 0 80 0 - 5675 wait_w pts/2 00:00:00 bash +0 T 1000 4575 4544 0 80 0 - 1056 signal pts/2 00:00:00 a.out +1 Z 1000 4576 4575 0 80 0 - 0 exit pts/2 00:00:00 a.ou +1 S 1000 6663 1638 0 80 0 - 1089 pipe_w pts/2 00:00:00 a.out +1 S 1000 8247 1638 0 80 0 - 1056 pipe_w pts/2 00:00:00 a.out +0 T 1000 8297 4544 0 80 0 - 1089 signal pts/2 00:00:00 a.out +1 Z 1000 8298 8297 0 80 0 - 0 exit pts/2 00:00:00 a.ou +1 S 1000 8331 1638 0 80 0 - 1089 wait pts/2 00:00:00 a.out +0 S 1000 8342 8331 0 80 0 - 1127 wait pts/2 00:00:00 sh +4 R 1000 8343 8342 0 80 0 - 7229 - pts/2 00:00:00 ps +Parent Process is killed so child process is Orphan process diff --git a/Operating System Lab/Assignment-3/1501cs16/Q3.c b/Operating System Lab/Assignment-3/1501cs16/Q3.c new file mode 100644 index 0000000..66b8a7d --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q3.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +int main() +{ + int pip[2]; + //Creating pipe + pipe(pip); + //Creating child process + pid_t pid = fork(); + if(pid < 0) + { + printf("Child process can't be created\n"); + return 0; + } + if(pid > 0) + { + //closing reading end of pipe + close(pip[0]); + //Writing to child process hardcoded + write(pip[1],"Hello Ayush Mishra",sizeof("Hello Ayush Mishra")); + } + else + { + // creating string buffer + char string[25]; + // closing writing end in child process + close(pip[1]); + // reading from parent process + read(pip[0],string,19); + // printing read input + printf("%s\n",string ); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16/Q3_Explanation.txt b/Operating System Lab/Assignment-3/1501cs16/Q3_Explanation.txt new file mode 100644 index 0000000..7713ab2 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q3_Explanation.txt @@ -0,0 +1,2 @@ +/*Explanation*/ +here We have created a one way traffic from parent process to child process for passing value use pipe() system call.In parent process we are writing text i.e "Hello Ayush Mishra" which we are extracting and printing in child process. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16/Q3_output.txt b/Operating System Lab/Assignment-3/1501cs16/Q3_output.txt new file mode 100644 index 0000000..f88d780 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16/Q3_output.txt @@ -0,0 +1,2 @@ +//Output +Hello Ayush Mishra diff --git a/Operating System Lab/Assignment-3/1501cs16/a.out b/Operating System Lab/Assignment-3/1501cs16/a.out new file mode 100755 index 0000000..ee4a5e6 Binary files /dev/null and b/Operating System Lab/Assignment-3/1501cs16/a.out differ diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/Q4_Explanation.txt b/Operating System Lab/Assignment-3/1501cs16_part2/Q4_Explanation.txt new file mode 100644 index 0000000..62cacd9 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16_part2/Q4_Explanation.txt @@ -0,0 +1,6 @@ +//Explanation +1.dstring.c +In this we take input from command line and read it word by word after opening the file. if number of count of word is divisible by 3 we print the corresponding word. + +2.pstring.c +In this we first take the file name from command line and used "xterm" to run the command in newly open window.system() is used to execute corresponding command. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/Q4_output.png b/Operating System Lab/Assignment-3/1501cs16_part2/Q4_output.png new file mode 100644 index 0000000..6c093ab Binary files /dev/null and b/Operating System Lab/Assignment-3/1501cs16_part2/Q4_output.png differ diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/dstring b/Operating System Lab/Assignment-3/1501cs16_part2/dstring new file mode 100755 index 0000000..fbae51a Binary files /dev/null and b/Operating System Lab/Assignment-3/1501cs16_part2/dstring differ diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/dstring.c b/Operating System Lab/Assignment-3/1501cs16_part2/dstring.c new file mode 100644 index 0000000..39bd9b1 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16_part2/dstring.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +int main(int argc,char* argv[]) +{ + char* fileName = argv[1]; + if(argc>2) + { + printf("Invalid input,please mention text file\n"); + return 0; + } + FILE* ifp = fopen(fileName,"r"); + char iWord[20]; + int cnt = 0; + while(fscanf(ifp,"%s",iWord)!=EOF) + { + cnt++; + if(cnt%3==0) + { + printf("%s \n",iWord); + } + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/process.txt b/Operating System Lab/Assignment-3/1501cs16_part2/process.txt new file mode 100644 index 0000000..eb7821f --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16_part2/process.txt @@ -0,0 +1,2 @@ +In computing, a process is an instance of a computer program that is being executed. +It contains the program code and its current activity. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/pstring b/Operating System Lab/Assignment-3/1501cs16_part2/pstring new file mode 100755 index 0000000..872216a Binary files /dev/null and b/Operating System Lab/Assignment-3/1501cs16_part2/pstring differ diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/pstring.c b/Operating System Lab/Assignment-3/1501cs16_part2/pstring.c new file mode 100644 index 0000000..b482456 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16_part2/pstring.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include +#include +int main(int argc,char* argv[]) +{ + if(argc>2) + { + printf("Invalid input,please mention text file\n"); + return 0; + } + //taking input from terminal + char *s = argv[1]; + char baseCommand[30] = "xterm -hold -e ./dstring "; + //concating baseCommand and input file name + strcat(baseCommand,s); + //system call with command + system(baseCommand); +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/1501cs16_part2/thread.txt b/Operating System Lab/Assignment-3/1501cs16_part2/thread.txt new file mode 100644 index 0000000..ab35753 --- /dev/null +++ b/Operating System Lab/Assignment-3/1501cs16_part2/thread.txt @@ -0,0 +1,2 @@ +In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. +The implementation of threads and processes differs between operating systems, but in most cases a thread is a component of a process. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/Assignment 3.pdf b/Operating System Lab/Assignment-3/Assignment 3.pdf new file mode 100644 index 0000000..a250224 Binary files /dev/null and b/Operating System Lab/Assignment-3/Assignment 3.pdf differ diff --git a/Operating System Lab/Assignment-3/Assignment-3-Q4.pdf b/Operating System Lab/Assignment-3/Assignment-3-Q4.pdf new file mode 100644 index 0000000..dbf8051 Binary files /dev/null and b/Operating System Lab/Assignment-3/Assignment-3-Q4.pdf differ diff --git a/Operating System Lab/Assignment-3/Q1.c b/Operating System Lab/Assignment-3/Q1.c new file mode 100644 index 0000000..71b1129 --- /dev/null +++ b/Operating System Lab/Assignment-3/Q1.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +int main() +{ + //creating new process + pid_t pid = fork(); + if(pid < 0) + { + printf("Child Process can't be created\n"); + } + + if(pid == 0) + { + // if child process exit + exit(0); + } + else + { + // wait for child to become zombie + sleep(5); + //printing process table for zombie + printf("Child Process Id is %d\n and parent process id %d\n",pid,getpid()); + system("ps -lA | grep '^. Z'"); + wait(NULL); + printf("Checking after zombie is cleaned\n"); + //No zombie with process id of child process now + system("ps -lA | grep '^. Z'"); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/Q1_Explanation.txt b/Operating System Lab/Assignment-3/Q1_Explanation.txt new file mode 100644 index 0000000..2186923 --- /dev/null +++ b/Operating System Lab/Assignment-3/Q1_Explanation.txt @@ -0,0 +1,14 @@ +//Output : +Child Process Id is 7790 + and parent process id 7789 + + PID PPID +1 Z 1000 4576 4575 0 80 0 - 0 exit pts/2 00:00:00 a.out +1 Z 1000 7790 7789 0 80 0 - 0 exit pts/2 00:00:00 a.out /*Zombie child process*/ +Checking after zombie is cleaned +1 Z 1000 4576 4575 0 80 0 - 0 exit pts/2 00:00:00 a.out /*No zombie child process*/ + + +/* Explanation */ + +Zombie process are those process which are not cleared from process table.Here in this case 7790 become the zombie process as it exited before parent wait() is call so it become zombie. After parent wait() is called process table is cleared and no zombie child is avalable now. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/Q2.c b/Operating System Lab/Assignment-3/Q2.c new file mode 100644 index 0000000..5caaecd --- /dev/null +++ b/Operating System Lab/Assignment-3/Q2.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +int main() +{ + // creating new process + pid_t pid = fork(); + if(pid < 0) + { + printf("Child Process can't be created\n"); + } + //child process + if(pid == 0) + { + sleep(5); + printf("Parent Process is killed so child process is Orphan process"); + //printing current process + system("ps -l"); + } + else + { + // printing child process and parent process id + printf("Child Process id is %d\n and Parent process id is %d\n", pid,getpid()); + printf("This is Parent process\n"); + // exiting parent process to make child orphan + exit(0); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/Q2_Explanation.txt b/Operating System Lab/Assignment-3/Q2_Explanation.txt new file mode 100644 index 0000000..7dbd499 --- /dev/null +++ b/Operating System Lab/Assignment-3/Q2_Explanation.txt @@ -0,0 +1,22 @@ +//Output +Child Process id is 8331 + and Parent process id is 8330 +This is Parent process + +F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD +0 S 1000 4544 4537 0 80 0 - 5675 wait_w pts/2 00:00:00 bash +0 T 1000 4575 4544 0 80 0 - 1056 signal pts/2 00:00:00 a.out +1 Z 1000 4576 4575 0 80 0 - 0 exit pts/2 00:00:00 a.ou +1 S 1000 6663 1638 0 80 0 - 1089 pipe_w pts/2 00:00:00 a.out +1 S 1000 8247 1638 0 80 0 - 1056 pipe_w pts/2 00:00:00 a.out +0 T 1000 8297 4544 0 80 0 - 1089 signal pts/2 00:00:00 a.out +1 Z 1000 8298 8297 0 80 0 - 0 exit pts/2 00:00:00 a.ou +1 S 1000 8331 1638 0 80 0 - 1089 wait pts/2 00:00:00 a.out +0 S 1000 8342 8331 0 80 0 - 1127 wait pts/2 00:00:00 sh +4 R 1000 8343 8342 0 80 0 - 7229 - pts/2 00:00:00 ps +Parent Process is killed so child process is Orphan process + +//my init() has id 1638 + +/*Explanation*/ +Orphan process are those process which exist even after its parent is dead.Here parent process id is 8330 which is killed before child process so orphan child is handled by init() process which can be seen in the output above i.e pid 1638(in my case) for orphan process pid 8331. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/Q3.c b/Operating System Lab/Assignment-3/Q3.c new file mode 100644 index 0000000..e4b0184 --- /dev/null +++ b/Operating System Lab/Assignment-3/Q3.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +int main() +{ + int pip[2]; + pipe(pip); + pid_t pid = fork(); + if(pid < 0) + { + printf("Child process can't be created\n"); + return 0; + } + if(pid > 0) + { + close(pip[0]); + write(pip[1],"Hello Ayush Mishra",19); + } + else + { + char string[25]; + close(pip[1]); + read(pip[0],string,19); + printf("%s\n",string ); + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/Q3_Explanation.txt b/Operating System Lab/Assignment-3/Q3_Explanation.txt new file mode 100644 index 0000000..3abcf4f --- /dev/null +++ b/Operating System Lab/Assignment-3/Q3_Explanation.txt @@ -0,0 +1,5 @@ +//Output +Hello Ayush Mishra + +/*Explanation*/ +here We have created a one way traffic from parent process to child process for passing value use pipe() system call.In parent process we are writing text i.e "Hello Ayush Mishra" which we are extracting and printing in child process. \ No newline at end of file diff --git a/Operating System Lab/Assignment-3/Tutorial 3.pdf b/Operating System Lab/Assignment-3/Tutorial 3.pdf new file mode 100644 index 0000000..2f14acd Binary files /dev/null and b/Operating System Lab/Assignment-3/Tutorial 3.pdf differ diff --git a/Operating System Lab/Assignment-3/dstring b/Operating System Lab/Assignment-3/dstring new file mode 100755 index 0000000..fbae51a Binary files /dev/null and b/Operating System Lab/Assignment-3/dstring differ diff --git a/Operating System Lab/Assignment-3/pstring b/Operating System Lab/Assignment-3/pstring new file mode 100755 index 0000000..872216a Binary files /dev/null and b/Operating System Lab/Assignment-3/pstring differ diff --git a/Operating System Lab/Assignment-4/Assignment 4.pdf b/Operating System Lab/Assignment-4/Assignment 4.pdf new file mode 100644 index 0000000..c935424 Binary files /dev/null and b/Operating System Lab/Assignment-4/Assignment 4.pdf differ diff --git a/Operating System Lab/Assignment-4/Q1.c b/Operating System Lab/Assignment-4/Q1.c new file mode 100644 index 0000000..b574676 --- /dev/null +++ b/Operating System Lab/Assignment-4/Q1.c @@ -0,0 +1,23 @@ +#include +#include +#include + +void* threadFunc(void* arg) +{ + char* msg = (char*) arg; + pthread_t id = pthread_self(); + printf("%s :\n CSE OS LAB, by Thread ID: %lld\n",msg,(long long)id); + return NULL; +} + +int main() +{ + pthread_t thread1,thread2,thread3; + char* message1 = "Thread1"; + char* message2 = "Thread2"; + char* message3 = "Thread3"; + int iret1 = pthread_create(&thread1, NULL, threadFunc, (void*) message1); + int iret2 = pthread_create(&thread2, NULL, threadFunc, (void*) message2); + int iret3 = pthread_create(&thread3, NULL, threadFunc, (void*) message3); + pthread_exit(NULL); +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-4/Q1_Output.txt b/Operating System Lab/Assignment-4/Q1_Output.txt new file mode 100644 index 0000000..4ff4f6e --- /dev/null +++ b/Operating System Lab/Assignment-4/Q1_Output.txt @@ -0,0 +1,13 @@ +//Output +darkmatter@hp-15:~/Desktop/oslab/Assignment-4$ gcc -pthread Q1.c +darkmatter@hp-15:~/Desktop/oslab/Assignment-4$ ./a.out +Thread1 : + CSE OS LAB, by Thread ID: 140311973226240 +Thread3 : + CSE OS LAB, by Thread ID: 140311956440832 +Thread2 : + CSE OS LAB, by Thread ID: 140311964833536 + + +//Explanation: +In this we are creatng 3 thread and printing there thread id in threadFunc using pthread_self().finally we have used pthread_exit(NULL) which terminate main thread although its child thread may be running. \ No newline at end of file diff --git a/Operating System Lab/Assignment-4/Q2.c b/Operating System Lab/Assignment-4/Q2.c new file mode 100644 index 0000000..b36d1f0 --- /dev/null +++ b/Operating System Lab/Assignment-4/Q2.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#define sz 5 + +int globVariable = 5; + +void* threadFunc(void* arg) +{ + static int staticVariable = 0; + char* msg = (char*) arg; + int localVariable = 0; + printf("%s: localVariable = %d,staticVariable=%d,globVariable=%d\n",msg,localVariable,staticVariable,globVariable); + localVariable += 1; + staticVariable += 1; + globVariable += 1; + printf("%s: localVariable = %d,staticVariable=%d,globVariable=%d\n",msg,localVariable,staticVariable,globVariable); + return NULL; +} + +int main() +{ + pthread_t threadID[sz]; + char base[12] = "threadID["; + char num[1]; + char brace[] = "]"; + for(int i=0;i +#include +#include +#include +#define sz 5 + +void* Busy(void* arg) +{ + char* msg = (char*) arg; + printf("%s : creating thread = %lld\n",msg,(long long)pthread_self()); + sleep(2); +} + +int main() +{ + pthread_t threadID[sz]; + char base[12] = "threadID["; + char num[1]; + char brace[] = "]"; + for(int i=0;i +#include +#include +#include + +#define sz 2 +sem_t semaphores; + +//common variable for reading and writing +int sharedVariable = 0; + +void* myFunc(void* arg) +{ + + // printing current threadID + printf("Current threadId = %lld\n",(long long)pthread_self()); + + /* + sem_wait() decrements (locks) the semaphore pointed to by sem. + If the semaphore's value is greater than zero, then the decrement proceeds, + and the function returns, immediately. If the semaphore currently has the + value zero, then the call blocks until either it becomes possible to perform + the decrement (i.e., the semaphore value rises above zero), or a signal handler + interrupts the call. + */ + sem_wait(&semaphores); + sleep(4); + //Reading shared variable + printf("ThreadId = %lld,Reading sharedVariable = %d\n",(long long)pthread_self(),sharedVariable); + + printf("Updating sharedVariable by threadId = %lld\n",(long long)pthread_self()); + + //updating shared variable + sharedVariable+=1; + + //Printing updated shared variable + printf("ThreadId = %lld,updated sharedVariable = %d\n",(long long)pthread_self(),sharedVariable); + + /* + The sem_post() function unlocks the specified semaphore by performing a semaphore unlock operation on that + semaphore.When this operation results in a positive semaphore value, no threads were blocked waiting for the + semaphore to be unlocked; the semaphore value is simply incremented.When this operation results in a + semaphore value of zero, one of the threads waiting for the semaphore is allowed to return successfully + from its invocation of the sem_wait() function. + */ + sem_post(&semaphores); +} + + +int main() +{ + //initializing semaphore + sem_init(&semaphores, 0, 1); + + //creating array for thread + pthread_t threadId[sz]; + + for(int i=0;i +#include +#include +#include + +#define sz 2 + + +// variuous func +pthread_mutex_t funMutex; +pthread_t threadId[sz]; +int lock=0; +int counter =-1; + +void* myFunc(void* arg) +{ + int check; + + //converting attribute to type + int val = (int)arg; + + //check whether locked or not + if(lock) + printf("pthread%d cannot access function f() lock acquired by pthread%d\n",val+1,counter); + + //if lock failed + if(check = pthread_mutex_lock(&funMutex)) + { + printf("Lock on this critical function Failed\n"); + return NULL; + } + printf("pthread%d is accessing function f()\n",val+1); + + //lock variable to decide whether locked or not + lock = 1; + counter = val+1; + printf("Lock acquired by pthread%d\n",val+1); + sleep(5); + + //unlocking this section + pthread_mutex_unlock(&funMutex); + printf("Lock released by pthread%d\n",val+1 ); + + //making lock variable 0 + lock = 0; +} + +int main() +{ + counter = 0; + int ret; + //initializing mutex + if(ret = pthread_mutex_init(&funMutex,NULL)) + { + printf("Mutex initialization Failed\n"); + return 1; + } + for(int i=0;i +#include +#include +#include +#include +#include + +#define num_philosopher 5 +#define num_Fork 5 + +int allocationMatrix[num_philosopher][num_philosopher]; +int requestMatrix[num_philosopher][num_philosopher]; +sem_t checkForkAvail[num_Fork]; +pthread_mutex_t lockPrint; +int preemptedId=-1; + +//Method to Print allocation matrix and request matrix +void printMatrix() +{ + pthread_mutex_lock(&lockPrint); + printf("Resource allocationMatrix :\n"); + for(int i=0;i + +#define pb push_back + +using namespace std; + +vector arrivalTime,burstTime,QueueNumber,copyBurstTime; +vector > info; +vector processNo; +priority_queue,vector< pair >, greater< pair > >Q1; +deque Q2; +int lastAddedTime; +int* turnAroundTime; + +//method to add process to respective queue +int add_process(int cTime,int fTime) +{ + int flag = 1; + for(int i=0;i=cTime && arrivalTime[info[i].second]t) + return info[i].first; + } + return 0; +} +int main() +{ + ifstream infile("scheduler.txt.txt"); + string a; + infile >> a >> a >> a >> a; + arrivalTime.pb(-1); + burstTime.pb(-1); + QueueNumber.pb(-1); + copyBurstTime.pb(-1); + processNo.pb(a); + int idx=1,d,b,c; + //Reading from file + while(infile>> a >> b >> c >> d) + { + cout << "Reading Row " << idx << ".... \n"; + arrivalTime.pb(b); + burstTime.pb(c); + processNo.pb(a); + QueueNumber.pb(d); + copyBurstTime.pb(c); + idx++; + } + + turnAroundTime = new int[arrivalTime.size()]; + + //sorting given process as arrival time + for(int i=1;i val = Q1.top(); + int processId = val.second; + Q1.pop(); + add_process(curTime+1,curTime+burstTime[processId]+1); + curTime+=burstTime[processId]; + cout << "-->"<< processNo[processId] <<"--->" << curTime; + turnAroundTime[processId] = curTime-arrivalTime[processId]; + } + + //For Round Robin + while(1) + { + //indication for Q1 process + int k = 0,p=0; + while(Q2.size()>0) + { + flag = 1; + burstTime[Q2[idx]]--; + curTime++; + cout << "---" << processNo[Q2[idx]] << "--->" << curTime ; + if(burstTime[Q2[idx]] == 0) + { + turnAroundTime[Q2[idx]] = curTime-arrivalTime[Q2[idx]]; + } + int val = Q2[idx]; + Q2.pop_front(); + + if(burstTime[val]>0) + Q2.push_back(val); + + if(curTime>lastAddedTime) + { + if(!add_process(curTime,curTime+1)) + { + k = 1; + break; + } + } + } + + //Q1 process is added + if(k==1||p==0) + break; + } + if(flag == 0 && lastAddedTime == finalTime) + break; + else if(flag == 0 && lastAddedTime" << lastAddedTime; + curTime = lastAddedTime; + add_process(lastAddedTime,lastAddedTime+1); + } + } + int totTurnAroundTime=0; + int totWaitingTime =0; + + //calculating turnaround and waiting time + for(int i=1;i1-->P2--->6-->P3--->9---P1--->10---P4--->11---P1--->12---P4--->13---P1--->14---P4--->15---P1--->16---P4--->17---P1--->18---P1--->19---P1--->20---P1--->21 + + +Required Measures : +Avg turnAroundTime 11.75 +Avg waitingTime 6.5 + +//Explanation : +Here we have two queue of which one uses SJF i.e queue 1 and other uses RR i.e queue 2 with time +quantum 1 sec.So whenever there is job in queue 1 we preempt queue 2 and schedule one which has +smallest burst time from queue 1.After this we again resume our process of queue 2 till all +process has been scheduled. + diff --git a/Operating System Lab/Assignment-6/Q1_Output.png b/Operating System Lab/Assignment-6/Q1_Output.png new file mode 100644 index 0000000..586deb3 Binary files /dev/null and b/Operating System Lab/Assignment-6/Q1_Output.png differ diff --git a/Operating System Lab/Assignment-6/Q1old.cpp b/Operating System Lab/Assignment-6/Q1old.cpp new file mode 100644 index 0000000..3907735 --- /dev/null +++ b/Operating System Lab/Assignment-6/Q1old.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +int arrivalTime[5],burstTime[5],QueueNumber[5]; +vector > info; +int cBurstTime[5]; +priority_queue,vector< pair >, greater< pair > >Q1; +vector Q2; +int turnAroundTime[5]; +int taken[5]; + +int add_process(int cTime,int fTime) +{ + int flag = 1; + for(int i=0;i<5;i++) + { + if(taken[info[i].second]==0 && arrivalTime[info[i].second]>=cTime && arrivalTime[info[i].second]> a >> a >> a >> a; + int idx=1; + while(infile>> a >> arrivalTime[idx] >> burstTime[idx] >> QueueNumber[idx]) + { + cout << "Reading Row " << idx << ".... \n"; + cBurstTime[idx]=burstTime[idx]; + idx++; + } + for(int i=1;i<5;i++) + info.push_back({arrivalTime[i],i}); + sort(info.begin(),info.end()); + int curTime = info[0].first; + int finalTime = info[4].first; + add_process(curTime,curTime+1); + cout << curTime ; + while(curTime<=finalTime) + { + while(!Q1.empty()) + { + pair val = Q1.top(); + int processId = val.second; + Q1.pop(); + add_process(curTime,curTime+burstTime[processId]); + cout << "-->P" << processId <<"--->" << curTime+burstTime[processId]; + curTime+=burstTime[processId]; + turnAroundTime[processId] = curTime-arrivalTime[processId]; + } + while(1) + { + int k=0; + int pre=0; + for(int i=0;i0) + { + pre=1; + burstTime[Q2[i]]--; + curTime++; + cout << "---P" << Q2[i] << "--->" << curTime ; + if(burstTime[Q2[i]]==0) + { + turnAroundTime[Q2[i]] = curTime-arrivalTime[Q2[i]]; + } + if(!add_process(curTime,curTime+1)) + { + k = 1; + break; + } + } + } + if(k==1||pre==0) + break; + if(pre==0 && Q1.empty()) + { + if(curTime val = Q1.top(); + int processId = val.second; + Q1.pop(); + add_process(curTime,curTime+burstTime[processId]); + cout << "-->P" << processId <<"--->" << curTime+burstTime[processId]; + curTime+=burstTime[processId]; + turnAroundTime[processId] = curTime-arrivalTime[processId]; + } + while(1) + { + int pre = 0; + for(int i=0;i0) + { + pre=1; + burstTime[Q2[i]]--; + cout << "---P" << Q2[i] << "--->" << curTime+1 ; + curTime++; + if(burstTime[Q2[i]]==0) + { + turnAroundTime[Q2[i]] = curTime-arrivalTime[Q2[i]]; + } + } + } + if(pre == 0) + break; + } + int totTurnAroundTime=0; + int totWaitingTime =0; + for(int i=1;i<5;i++) + { + totTurnAroundTime+=turnAroundTime[i]; + totWaitingTime+=(turnAroundTime[i]-cBurstTime[i]); + //cout << turnAroundTime[i] << " " << burstTime[i] << endl; + } + cout << endl; + cout << "Avg turnAroundTime " << totTurnAroundTime/4.0 << endl; + cout << "Avg waitingTime " << totWaitingTime/4.0 << endl; + +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-6/a.out b/Operating System Lab/Assignment-6/a.out new file mode 100755 index 0000000..02165b3 Binary files /dev/null and b/Operating System Lab/Assignment-6/a.out differ diff --git a/Operating System Lab/Assignment-6/scheduler.txt.txt b/Operating System Lab/Assignment-6/scheduler.txt.txt new file mode 100644 index 0000000..28feb01 --- /dev/null +++ b/Operating System Lab/Assignment-6/scheduler.txt.txt @@ -0,0 +1,5 @@ +Process Arrival_Time Burst_Time Queue_number +P1 0 9 2 +P2 1 5 1 +P3 2 3 1 +P4 3 4 2 \ No newline at end of file diff --git a/Operating System Lab/Assignment-7/Assignment 7.pdf b/Operating System Lab/Assignment-7/Assignment 7.pdf new file mode 100644 index 0000000..8e12eb6 Binary files /dev/null and b/Operating System Lab/Assignment-7/Assignment 7.pdf differ diff --git a/Operating System Lab/Assignment-7/NachosPrimer.pdf b/Operating System Lab/Assignment-7/NachosPrimer.pdf new file mode 100644 index 0000000..7e33ef6 Binary files /dev/null and b/Operating System Lab/Assignment-7/NachosPrimer.pdf differ diff --git a/Operating System Lab/Assignment-7/Q2.c b/Operating System Lab/Assignment-7/Q2.c new file mode 100644 index 0000000..6ad1eaa --- /dev/null +++ b/Operating System Lab/Assignment-7/Q2.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include + +#define RAND 10 +pthread_mutex_t lockStudent; +sem_t checkQuesEnd; +sem_t checkAnsEnd; +int id; + + +void QuesStart(int studentId) +{ + printf("Student %d started asking question\n", studentId); + id = studentId; + return; +} + +void QuesEnd(int studentId) +{ + //question ends + sem_post(&checkQuesEnd); + + //waiting for professor to finish answering + sem_wait(&checkAnsEnd); + printf("Student %d question ends\n", studentId); + return; +} + +void* students(void* arg) +{ + int stud = *((int*)arg); + printf("Student number %d enter the class\n",stud); + while(1) + { + //acquiring mutex lock so that one student can ask question only + pthread_mutex_lock(&lockStudent); + + //student start asking question + QuesStart(stud); + + //asking question + sleep(1); + + //student end asking question + QuesEnd(stud); + + //releasing mutex lock for other student + pthread_mutex_unlock(&lockStudent); + //random sleep so that each student can ask question randomly + sleep((rand())%RAND); + } +} + +void AnsStart() +{ + //waiting for student to finish asking question + sem_wait(&checkQuesEnd); + printf("Professor started answering question of student %d\n",id ); + return; +} + +void AnsEnd() +{ + printf("Professor ended answering question of student %d\n",id ); + + //professor finished answering question + sem_post(&checkAnsEnd); + return; +} +int main() +{ + srand(time(NULL)); + + //mutex for only single student to ask question + pthread_mutex_init(&lockStudent,NULL); + //Semaphore to check whether question ends or ans ends + sem_init(&checkQuesEnd,0,0); + sem_init(&checkAnsEnd,0,0); + + //Taking number of student from user + int numStudent; + printf("Enter the number of students present in professor class\n"); + scanf("%d",&numStudent); + + //creating as many number of thread as student + pthread_t threadId[numStudent+1]; + + //professor thread is main thread + printf("Professor enter the class\n"); + for(int student=1;student<=numStudent;student++) + { + + //creating thread + int iret = pthread_create(&threadId[student],NULL,students,&student); + sleep(0.001); + if(iret == -1) + { + printf("Thread creation failed for student %d\n",student); + } + } + + while(1) + { + + //professor start answering question + AnsStart(); + + //answering question + sleep(1); + + //professor ends answering question + AnsEnd(); + + } +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-7/Q2_Output.txt b/Operating System Lab/Assignment-7/Q2_Output.txt new file mode 100644 index 0000000..1bb0909 --- /dev/null +++ b/Operating System Lab/Assignment-7/Q2_Output.txt @@ -0,0 +1,53 @@ +//OUTPUT +darkmatter@hp-15:~/Dropbox/oslab/Assignment-7$ gcc -pthread Q2.c +darkmatter@hp-15:~/Dropbox/oslab/Assignment-7$ ./a.out +Enter the number of students present in professor class +5 +Professor enter the class +Student number 1 enter the class +Student 1 started asking question +Student number 2 enter the class +Student number 3 enter the class +Student number 4 enter the class +Student number 5 enter the class +Professor started answering question of student 1 +Professor ended answering question of student 1 +Student 1 question ends +Student 2 started asking question +Professor started answering question of student 2 +Professor ended answering question of student 2 +Student 2 question ends +Student 3 started asking question +Professor started answering question of student 3 +Professor ended answering question of student 3 +Student 3 question ends +Student 4 started asking question +Professor started answering question of student 4 +Professor ended answering question of student 4 +Student 4 question ends +Student 5 started asking question +Professor started answering question of student 5 +Professor ended answering question of student 5 +Student 5 question ends +Student 3 started asking question +Professor started answering question of student 3 +Professor ended answering question of student 3 +Student 3 question ends +Student 1 started asking question +Professor started answering question of student 1 +Professor ended answering question of student 1 +Student 1 question ends +Student 4 started asking question +Professor started answering question of student 4 +Professor ended answering question of student 4 +Student 4 question ends +Student 2 started asking question +Professor started answering question of student 2 +Professor ended answering question of student 2 +Student 2 question ends +Student 5 started asking question +Professor started answering question of student 5 +and many more.... + +//Explanation : +To ensure that only one student ask a question a mutex lock is used ,where as to ensure that only one person is speaking two semaphore is used.Student are represented as thread whereas professor is represented in main thread. \ No newline at end of file diff --git a/Operating System Lab/Assignment-7/a.out b/Operating System Lab/Assignment-7/a.out new file mode 100755 index 0000000..a35d44a Binary files /dev/null and b/Operating System Lab/Assignment-7/a.out differ diff --git a/Operating System Lab/Assignment-8/Assignment 8.pdf b/Operating System Lab/Assignment-8/Assignment 8.pdf new file mode 100644 index 0000000..38be352 Binary files /dev/null and b/Operating System Lab/Assignment-8/Assignment 8.pdf differ diff --git a/Operating System Lab/Assignment-8/Q1.c b/Operating System Lab/Assignment-8/Q1.c new file mode 100644 index 0000000..1d3610f --- /dev/null +++ b/Operating System Lab/Assignment-8/Q1.c @@ -0,0 +1,33 @@ +#include +#include + +int *heapSegmentVar; +//variable in bss segment as uninitialized +int bssSegmentVar; + +void textSegmentVar() +{ + return; +} + +int main() +{ + //variable in data segment + static int dataSegmentVar = 5; + //var in stack + int stackSegmentVar = 10; + + //allocation in heap + heapSegmentVar = (int*) malloc(5*sizeof(int)); + + //code in text section + textSegmentVar(); + printf("Address of Variable in text section %p\n",&textSegmentVar); + printf("Address of Variable in bssSegmentVar %p\n",&bssSegmentVar); + printf("Address of Variable in stackSegmentVar %p\n",&stackSegmentVar); + printf("Address of Variable in heapSection %p\n",heapSegmentVar); + + printf("Size of each segment : \n"); + system("size a.out"); + +} \ No newline at end of file diff --git a/Operating System Lab/Assignment-8/Q1_OUTPUT.txt b/Operating System Lab/Assignment-8/Q1_OUTPUT.txt new file mode 100644 index 0000000..ae279d4 --- /dev/null +++ b/Operating System Lab/Assignment-8/Q1_OUTPUT.txt @@ -0,0 +1,13 @@ +//OUTPUT +darkmatter@hp-15:~/Dropbox/oslab/Assignment-8$ gcc Q1.c +darkmatter@hp-15:~/Dropbox/oslab/Assignment-8$ ./a.out +Address of Variable in text section 0x400666 +Address of Variable in bssSegmentVar 0x601068 +Address of Variable in stackSegmentVar 0x7ffdd9e34784 +Address of Variable in heapSection 0xbe7010 +Size of each segment : + text data bss dec hex filename + 1928 588 24 2540 9ec a.out + +//EXPLANATION : +Defining variable in each segment and using & to print it's corresponding addresses. \ No newline at end of file diff --git a/Operating System Lab/Assignment-8/Q2.cpp b/Operating System Lab/Assignment-8/Q2.cpp new file mode 100644 index 0000000..f36a0c2 --- /dev/null +++ b/Operating System Lab/Assignment-8/Q2.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +bool checkAvailable(int i,vector& v) +{ + for(int j=0;j&sequences) +{ + int ans[frameSize][sequences.size()]; + memset(ans,0,sizeof(ans)); + int oldest = 0; + int pageFault = 0; + + vector pattern; + cout << "FIFO :" << endl; + + //iterating over sequences + for(int start=0;startframeSize) + oldest = 0; + pageFault++; + pattern.push_back(start); + } + } + + //Printing the output. + for(int k=0;k sequences; + int x; + while(fscanf(fp,"%d",&x)!=EOF) + { + if(x == -1) + break; + sequences.push_back(x); + } + //calling FIFO scheme + FIFO(frameSize,sequences); + + } +} diff --git a/Operating System Lab/Assignment-8/Q2_Output.txt b/Operating System Lab/Assignment-8/Q2_Output.txt new file mode 100644 index 0000000..c0fd6b0 --- /dev/null +++ b/Operating System Lab/Assignment-8/Q2_Output.txt @@ -0,0 +1,33 @@ +//OUTPUT +darkmatter@hp-15:~/Dropbox/oslab/Assignment-8$ g++ -std=c++0x Q2.cpp +darkmatter@hp-15:~/Dropbox/oslab/Assignment-8$ ./a.out +FIFO : +1 1 1 2 2 2 6 6 6 1 1 1 4 4 +0 6 6 6 1 1 1 2 2 2 6 6 6 3 +0 0 3 3 3 5 5 5 3 3 3 2 2 2 + +Using 3frames ,the reference string yielding : +Scheme #faults +FIFO 14 +FIFO : +1 1 1 1 6 6 6 6 3 +0 2 2 2 2 1 1 1 1 +0 0 3 3 3 3 2 2 2 +0 0 0 5 5 5 5 4 4 + +Using 4frames ,the reference string yielding : +Scheme #faults +FIFO 9 +FIFO : +1 1 1 1 1 4 +0 2 2 2 2 2 +0 0 3 3 3 3 +0 0 0 5 5 5 +0 0 0 0 6 6 + +Using 5frames ,the reference string yielding : +Scheme #faults +FIFO 6 + +//Explanation: +Here we created a Fifo scheme for page whenever a miss occur page fault count is increased and oldest present data is replaced. \ No newline at end of file diff --git a/Operating System Lab/Assignment-8/a b/Operating System Lab/Assignment-8/a new file mode 100755 index 0000000..3be0c58 Binary files /dev/null and b/Operating System Lab/Assignment-8/a differ diff --git a/Operating System Lab/Assignment-8/a.out b/Operating System Lab/Assignment-8/a.out new file mode 100755 index 0000000..b948259 Binary files /dev/null and b/Operating System Lab/Assignment-8/a.out differ diff --git a/Operating System Lab/Assignment-8/page.txt b/Operating System Lab/Assignment-8/page.txt new file mode 100644 index 0000000..4ffa513 --- /dev/null +++ b/Operating System Lab/Assignment-8/page.txt @@ -0,0 +1,6 @@ +3 +1 2 3 2 1 5 2 1 6 2 5 6 3 1 3 6 1 2 4 3 -1 +4 +1 2 3 2 1 5 2 1 6 2 5 6 3 1 3 6 1 2 4 3 -1 +5 +1 2 3 2 1 5 2 1 6 2 5 6 3 1 3 6 1 2 4 3 -1 \ No newline at end of file diff --git a/Operating System Lab/Assignment-9/Assignment 9.pdf b/Operating System Lab/Assignment-9/Assignment 9.pdf new file mode 100644 index 0000000..283e62a Binary files /dev/null and b/Operating System Lab/Assignment-9/Assignment 9.pdf differ diff --git a/Operating System Lab/Assignment-9/LFU.cpp b/Operating System Lab/Assignment-9/LFU.cpp new file mode 100644 index 0000000..78d2f60 --- /dev/null +++ b/Operating System Lab/Assignment-9/LFU.cpp @@ -0,0 +1,180 @@ +/************* + Author - am10 + ******************/ +#include + +#define sync ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(0); +#define endl '\n' +#define ll long long +#define pb push_back +#define PI acos(-1) +#define pii pair +#define FI first +#define SE second + +using namespace std; + +int freq[10005]; + +int check(vector&sequences,int val,int idx) +{ + for(int i=idx;i>=0;i--) + { + if(sequences[i]==val) + return i; + } +} + +bool checkAvailable(int i,vector& v) +{ + for(int j=0;j&sequences) +{ + int ans[frameSize][sequences.size()]; + memset(ans,0,sizeof(ans)); + int pageFault = 0; + memset(freq,0,sizeof(freq)); + vector pattern; + cout << "LFU :" << endl; + + for(int i=0;i=0;k--) + { + bool found1 = false; + for(int l=0;l=0;k--) + { + bool found1 = false; + for(int l=0;l sequences; + int x; + while(fscanf(fp,"%d",&x)!=EOF) + { + if(x == -1) + break; + sequences.push_back(x); + } + //calling FIFO scheme + LFU(frameSize,sequences); + } +} diff --git a/Operating System Lab/Assignment-9/LFU_Output.txt b/Operating System Lab/Assignment-9/LFU_Output.txt new file mode 100644 index 0000000..3a994c9 --- /dev/null +++ b/Operating System Lab/Assignment-9/LFU_Output.txt @@ -0,0 +1,32 @@ +darkmatter@hp-15:~/Dropbox/oslab/Assignment-9$ g++ -std=c++0x LFU.cpp +darkmatter@hp-15:~/Dropbox/oslab/Assignment-9$ ./a.out +LFU : +1 1 1 1 1 1 1 1 1 1 1 +0 2 2 2 2 2 2 2 2 2 2 +0 0 3 5 6 5 6 3 6 4 3 + +Using 3frames ,the reference string yielding : +Scheme #faults +LFU 11 +LFU : +1 1 1 1 1 1 1 1 +0 2 2 2 2 2 2 2 +0 0 3 3 6 6 6 6 +0 0 0 5 5 3 4 3 + +Using 4frames ,the reference string yielding : +Scheme #faults +LFU 8 +LFU : +1 1 1 1 1 1 +0 2 2 2 2 2 +0 0 3 3 3 3 +0 0 0 5 5 4 +0 0 0 0 6 6 + +Using 5frames ,the reference string yielding : +Scheme #faults +LFU 6 + +//Explanation: +In case of Page replacement Page with least frequency is removed and frequency of removed page is reset to zero.In case of tie oldest one is removed. \ No newline at end of file diff --git a/Operating System Lab/Assignment-9/LRU.cpp b/Operating System Lab/Assignment-9/LRU.cpp new file mode 100644 index 0000000..04f982f --- /dev/null +++ b/Operating System Lab/Assignment-9/LRU.cpp @@ -0,0 +1,138 @@ +/************* + Author - am10 + ******************/ +#include + +#define sync ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(0); +#define endl '\n' +#define ll long long +#define pb push_back +#define PI acos(-1) +#define pii pair +#define FI first +#define SE second + +using namespace std; + +//for getting idx of least recently used +int check(vector&sequences,int val,int idx) +{ + for(int i=idx;i>=0;i--) + { + if(sequences[i]==val) + return i; + } +} +//checking if it is present in pattern +bool checkAvailable(int i,vector& v) +{ + for(int j=0;j&sequences) +{ + int ans[frameSize][sequences.size()]; + memset(ans,0,sizeof(ans)); + int pageFault = 0; + + vector pattern; + cout << "LRU :" << endl; + + for(int i=0;i sequences; + int x; + while(fscanf(fp,"%d",&x)!=EOF) + { + if(x == -1) + break; + sequences.push_back(x); + } + //calling FIFO scheme + LRU(frameSize,sequences); + } +} diff --git a/Operating System Lab/Assignment-9/LRU_Output.txt b/Operating System Lab/Assignment-9/LRU_Output.txt new file mode 100644 index 0000000..030a405 --- /dev/null +++ b/Operating System Lab/Assignment-9/LRU_Output.txt @@ -0,0 +1,32 @@ +darkmatter@hp-15:~/Dropbox/oslab/Assignment-9$ g++ -std=c++0x LRU.cpp +darkmatter@hp-15:~/Dropbox/oslab/Assignment-9$ ./a.out +LRU : +1 1 1 1 1 5 5 1 1 1 3 +0 2 2 2 2 2 3 3 2 2 2 +0 0 3 5 6 6 6 6 6 4 4 + +Using 3frames ,the reference string yielding : +Scheme #faults +LRU 11 +LRU : +1 1 1 1 1 3 3 3 4 4 +0 2 2 2 2 2 1 1 1 1 +0 0 3 3 6 6 6 6 6 3 +0 0 0 5 5 5 5 2 2 2 + +Using 4frames ,the reference string yielding : +Scheme #faults +LRU 10 +LRU : +1 1 1 1 1 1 +0 2 2 2 2 2 +0 0 3 3 3 3 +0 0 0 5 5 4 +0 0 0 0 6 6 + +Using 5frames ,the reference string yielding : +Scheme #faults +LRU 6 + +//Explanation : +In case of Page replacement the page which was least recently used is remove. \ No newline at end of file diff --git a/Operating System Lab/Assignment-9/Optimal.cpp b/Operating System Lab/Assignment-9/Optimal.cpp new file mode 100644 index 0000000..9667cf8 --- /dev/null +++ b/Operating System Lab/Assignment-9/Optimal.cpp @@ -0,0 +1,184 @@ +/************* + Author - am10 + ******************/ +#include + +#define sync ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(0); +#define endl '\n' +#define ll long long +#define pb push_back +#define PI acos(-1) +#define pii pair +#define FI first +#define SE second + +using namespace std; + +//find next used term +int check(vector&sequences,int val,int idx) +{ + for(int i=idx;i& v) +{ + for(int j=0;j&sequences) +{ + int ans[frameSize][sequences.size()]; + memset(ans,0,sizeof(ans)); + int pageFault = 0; + + vector pattern; + cout << "Optimal :" << endl; + + for(int i=0;i=val) + { + bool use = true; + //in case of clash replace oldest one + if(val1==val) + { + int x = i-1; + for(int k=i-1;k>=0;k--) + { + bool found1 = false; + for(int l=0;l=0;k--) + { + bool found1 = false; + for(int l=0;l sequences; + int x; + while(fscanf(fp,"%d",&x)!=EOF) + { + if(x == -1) + break; + sequences.push_back(x); + } + //calling FIFO scheme + Optimal(frameSize,sequences); + } +} diff --git a/Operating System Lab/Assignment-9/Optimal_Output.txt b/Operating System Lab/Assignment-9/Optimal_Output.txt new file mode 100644 index 0000000..32134d8 --- /dev/null +++ b/Operating System Lab/Assignment-9/Optimal_Output.txt @@ -0,0 +1,32 @@ +darkmatter@hp-15:~/Dropbox/oslab/Assignment-9$ g++ -std=c++0x Optimal.cpp +darkmatter@hp-15:~/Dropbox/oslab/Assignment-9$ ./a.out +Optimal : +1 1 1 1 6 6 6 2 2 +0 2 2 2 2 2 1 1 4 +0 0 3 5 5 3 3 3 3 + +Using 3frames ,the reference string yielding : +Scheme #faults +Optimal 9 +Optimal : +1 1 1 1 6 6 6 +0 2 2 2 2 2 4 +0 0 3 3 3 3 3 +0 0 0 5 5 1 1 + +Using 4frames ,the reference string yielding : +Scheme #faults +Optimal 7 +Optimal : +1 1 1 1 1 4 +0 2 2 2 2 2 +0 0 3 3 3 3 +0 0 0 5 5 5 +0 0 0 0 6 6 + +Using 5frames ,the reference string yielding : +Scheme #faults +Optimal 6 + +//Explanation: +In case of Page replacement Page which are for long not accesed is replaced.In case of tie oldest one is removed. \ No newline at end of file diff --git a/Operating System Lab/Assignment-9/page.txt b/Operating System Lab/Assignment-9/page.txt new file mode 100644 index 0000000..4ffa513 --- /dev/null +++ b/Operating System Lab/Assignment-9/page.txt @@ -0,0 +1,6 @@ +3 +1 2 3 2 1 5 2 1 6 2 5 6 3 1 3 6 1 2 4 3 -1 +4 +1 2 3 2 1 5 2 1 6 2 5 6 3 1 3 6 1 2 4 3 -1 +5 +1 2 3 2 1 5 2 1 6 2 5 6 3 1 3 6 1 2 4 3 -1 \ No newline at end of file