Thursday 5 May 2016

Program to implement address conversion functions

0

Some functions are used in network programming to convert the IP addresses to human readable form and back. 

inet_ntop() & inet_ntoa():These functions convert a network address in a struct in_addr to a dots-and-numbers format string.

inet_pton(), inet_aton() & inet_addr :This functions convert a dots-and-numbers string  to struct in_addr format.

inet_ntoa, inet_aton and inet_addr are deprecated because they are incompatible with IPv6 addresses. Functions like inet_pton and inet_ntop can be used with both IPv4 and IPv6 addresses. 

Code:

#include<stdio.h> 
#include<sys/socket.h> 
#include<stdlib.h> 
#include<arpa/inet.h> 
#define SERV_PORT 9002  
int main() 
{  
   char str[INET_ADDRSTRLEN],*str1,*str2; 
   struct sockaddr_in server; 
  
   printf("Testing inet_pton and inet inet_ntop\n"); 
   inet_pton(AF_INET, "192.0.2.33", &(server.sin_addr)); 
   inet_ntop(AF_INET, &(server.sin_addr), str, INET_ADDRSTRLEN); 
   printf("%s\n", str); // prints "192.0.2.33" 
    
   bzero(&str1,sizeof(str1)); 
   bzero(&server,sizeof(server)); 
  
   printf("Testing inet_ntoa and inet_aton\n"); 
   inet_aton("192.168.1.1",&(server.sin_addr)); 
   str1=inet_ntoa(server.sin_addr); 
   printf("%s\n",str1); 
  
   bzero(&str1,sizeof(str1)); 
   bzero(&server,sizeof(server)); 
  
   printf("Testing inet_addr\n"); 
   server.sin_addr.s_addr=inet_addr("192.2.2.2"); 
   str2=inet_ntoa(server.sin_addr); 
   printf("%s\n",str2); 
}   

Output: 

Tuesday 3 May 2016

Program to check whether data in your system is stored in little endian format or big endian format

0

Think of computer memory as cells which are 1 byte long. In order to store 2 byte hex-data (90AB), there are two possibilities: 

1. Big endianIn this, you store the most significant byte in the smallest address. Examples include Motorola 68000 series, Xilinx MicroblazeSuperH, IBM z/Architecture or Atmel AVR32
                                                    Address value      Data
                                                       1001                      90
                                                       1002                      AB

2. Little endianIn this, you store the least significant byte in the smallest address.
Example is Intel x86 and x86-64 series of processors, that's why it is also known as Intel Convention.
                                                   Address value      Data
                                                       1001                      AB
                                                       1002                      90


Here's the code to test your machine's endianness:
#include<stdio.h>
#include<stdlib.h>
#define CPU_VENDOR_OS "intel-gnu-linux"
int main()
{          union
            {
                   short s;
                   char c[sizeof(short)];
            }un1;
            un1.s=0x0102;
            printf("%s\n",CPU_VENDOR_OS);
            if(sizeof(short)==2)
           {
                  if(un.c[0]==1 && un.c[1]==2)
                  {
                         printf("Big endian\n");
                   }
                   else if(un.c[0]==2 && un.c[1]==1)
                   {
                        printf("Little endian\n");
                   }
                  else
                  {
                       printf("Unknown\n");
                  }
          }
         return 0;
}

Output: Intel processor, so output is little endian.



Monday 2 May 2016

Implement cloudlet scheduling strategies

3

Cloudsim employs namely two strategies which are used to schedule the cloudlets. One of them is time-shared scheduler and second one is space-shared scheduler.
Time-shared scheduler: All available resources are shared between cloudlets for stipulated amount of time only. It uses round- robin scheduling algorithm. All space is allocated to one particular thread which is scheduled at that time. As the time frame for that cloudlet is over, all space resources are allocated to one cloudlet.

Space-shared scheduler: All available space is divided among the cloudlets. One cloudlet is running at one time but space is equally distributed among the various cloudlets.

We will use both of them:

1. Time-shared Scheduling:
    Change the code as written below (rest code is same from previous post). Change the constructor       of each virtual machine as the shown below.

    Vm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new           
    CloudletSchedulerTimeShared());

   Then output will be:

2. Space- shared scheduling:
    Change the constructor of each virtual machine as the shown below for space shared scheduling         algorithm.

    Vm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new 
    CloudletSchedulerSpaceShared());

    Then output will be:

Comparison of the output:

Starting time and finish time of all the cloudlets is same in the case of time-shared scheduling as all cloudlets are given a impression that all are running at one point but time is divided among each providing resources are used by only one at one time. 

Whereas the starting and finish time of all 4 cloudlets is not same. Resources are divided among the cloudlets i.e. space, but not time. They are starting and finishing execution at different times.

Hope you like it!




Create 2 virtual machines and 4 cloudlets in cloudsim

3

Hope now you know how to install the cloudim in Netbeans (if you don't know how to do this refer this blog: http://geekahead.blogspot.in/2016/02/cloud-computing.html). Now lets create two virtual machines and four cloudlets in it.

First of all, we should start with basic terminologies in terms of the cloudsim: 

1. Virtual Machines: In computing, a virtual machine (VM) is an emulation of a particular computer system.Virtual machines operate based on the computer architecture and functions of a real or hypothetical computer, and their implementations may involve specialized hardware, software, or a combination of both. As cloudsim is a simulation software, it has class for virtual machine named, vm. It runs inside a Host, sharing hostList with other VMs. It processes cloudlets. This processing happens according to a policy, defined by the CloudletScheduler. Each VM has a owner, which can submit cloudlets to the VM to be executed.

2. Cloudlets: A cloudlet is a mobility-enhanced small-scale cloud datacenter that is located at the edge of the Internet. The main purpose of the cloudlet is supporting resource-intensive and interactive mobile applications by providing powerful computing resources to mobile devices with lower latency. In cloudsim, the tasks/processes are the cloudlets. So you need to create a cloudlet that mimics the task that you want and assign the cloudlet(task) to a VM.

Now we will be editing the code from example 1 of the cloudsim (cloudsim comes with sample codes).

Search for comment "VM description", then change the code as follows till you find a comment: "submit cloudlet list to the broker" :

                       // VM description
int vmid1 = 1,vmid2 = 2;
int mips = 500;
long size = 10000; // image size (MB)
int ram = 512; // vm memory (MB)
long bw = 1000;
int pesNumber = 1; // number of cpus
String vmm = "Xen"; // VMM name

// create VM
Vm vm1 = new Vm(vmid1, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
                        
                        Vm vm2 = new Vm(vmid2, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());

// add the VM to the vmList
vmlist.add(vm1);
                        vmlist.add(vm2);

// submit vm list to the broker
broker.submitVmList(vmlist);

// Fifth step: Create one Cloudlet
cloudletList = new ArrayList<Cloudlet>();

// Cloudlet properties
int id = 0;
long length = 400000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();

Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel,           utilizationModel);
                        cloudlet1.setUserId(brokerId);
                        cloudlet1.setVmId(vmid1);
                        id++;
                           Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel,          utilizationModel);
                           cloudlet2.setUserId(brokerId);
                           cloudlet2.setVmId(vmid1);
                           id++;
                           Cloudlet cloudlet3 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel,          utilizationModel);
                           cloudlet3.setUserId(brokerId);
                           cloudlet3.setVmId(vmid2);
                           id++;
                           Cloudlet cloudlet4 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel,          utilizationModel);
                            cloudlet4.setUserId(brokerId);
                            cloudlet4.setVmId(vmid2);
                        // add the cloudlet to the list
                        cloudletList.add(cloudlet1);
                        cloudletList.add(cloudlet2);
                        cloudletList.add(cloudlet3);
                        cloudletList.add(cloudlet4); 

And rest of the code is same. 

And run this file. Below is the output on the console.