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.






Wednesday 9 March 2016

Identify whether identifier is valid or not

1


An identifier is used for any variable, function, data definition, etc. In the programming language C, an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline.

This program is to identify whether the entered input is a valid identifier or not. Here goes the code:

#include<stdio.h> 
#include<conio.h> 
#include<ctype.h> // to use isdigit() and isalpha() functions
void main() {    
char a[10];  
int flag, i=1; 
for(int j=0; j<5; j++) {
    printf("\n Enter an identifier:");  
    gets(a);    
    if(isalpha(a[0]) || a[0]=='_')      
        flag=1;  
    else      
        printf("\n Not a valid identifier");    
    while(a[i]!='\0')    {   
        if(!isdigit(a[i])&&!isalpha(a[i])&&a[i]!='_') {         
            flag=0;         
            break;       
        }     
        i++;  
    }  
    if(flag==1)     
    printf("\n Valid identifier");    
    }
 


Output:



Thursday 11 February 2016

Fuzzy box implementation in the Matlab

0



Note:This article is for the audience who have prior knowledge of fuzzy sets. This is just implementation of fuzzy sets in Matlab.

Software used: Matlab R2010a

Problem Statement: Food Tipping
Tip is decided on the basis of various attributes. Selected member function for attributes is triangular here. Range is selected so that membership functions don’t overlap with each other.
Input Variable:
S.No.
Attributes
Range
1.
Taste
[0 3]

Delicious
[0 1]

Normal
[1 2]

Rancid
[2 3]
2.
Ambience
[0 3]

Worst
[0 1]

Good
[1 2]

Excellent
[2 3]
3.
Service
[0 3]

Slow
[0 1]

On time
[1 2]

Fast
[2 3]
4.
Price
[0 2]

Reasonable
[0 1]

Costly
[1 2]
5.
Hygiene
[0 2]

Clean
[0 1]

Stinky
[1 2]

Output Variable:
S.No.
Attributes
Range
1.
Tip
[0 1]

100
[0 0.2]

200
[0.2 0.4]

300
[0.4 0.6]

400
[0.6 0.8]

500
[0.8 1]


Steps:
1.Start matlab and type fuzzy in command window.
      2. A new wizard named FIS Editor will open (Fuzzy Inference System).  
      3.Go on Edit->Add variable->Input. Do this for 4 times as we have five input variables. Change name of the input variable by editing the content of Name.

      4.Similarly edit the name of all five variables and output variable. Now save the work in file first. Go to File->Export-> File. Now save the food_tip.fis file. FIS editor will look like as follows:
      5. Double click on the taste input variable to edit the attributes of taste. Taste has three attributes namely: delicious, normal, rancid. New box will pop up named Membership Function Editor. Go to Edit->Remove All MFs. Then Edit-> Add Mfs. Small box asking for type and no of mf will pop up. Select trimf and 3 as we want to use the triangular member function and no of member function is 3. Click Ok.      
                                  

       6.Set range of taste variable as [0 3] (your choice). Edit mf1 name as delicious and its parameters. Parameters in triangular membership function is vertices of triangle. Select [0 0.5 1]. Similarly set all three membership function as required.
    7. Do it for all the variables. Close the membership function editor.




      8. In FIS Editor, go to Edit-> Rules. A new rule Editor will open. Now add rules one by one. Close when you are done.

      9. Now go to View->Rules. See output by varying inputs. First input is [0.5 2.5 2.5 0.5 0.5] = [taste=delicious ambience=excellent service=fast price=reasonable hygiene=clean] (as range of delicious in taste is [0 1] so 0.5 means delicious, similarly others are done) so the tip value is 0.9 so tip would be 500 (as range of 500 is [0.8 1]).

      10.Similarly, you can try for another inputs.
   Fuzzy toolbox implemented in the Matlab successfully.Thanks for reading!!

Tuesday 9 February 2016

Cloudsim with netbeans

5


To install and study examples of cloud sim in netbeans

CloudSim: A Framework for Modeling and Simulation of Cloud Computing Infrastructures and Services
Recently, cloud computing emerged as the leading technology for delivering reliable, secure, fault-tolerant, sustainable, and scalable computational services, which are presented as Software, Infrastructure, or Platform as services (SaaS, IaaS, PaaS). Moreover, these services may be offered in private data centers (private clouds), may be commercially offered for clients (public clouds), or yet it is possible that both public and private clouds are combined in hybrid clouds.
The primary objective of this project is to provide a generalized, and extensible simulation framework that enables seamless modeling, simulation, and experimentation of emerging Cloud computing infrastructures and application services. By using CloudSim, researchers and industry-based developers can focus on specific system design issues that they want to investigate, without getting concerned about the low level details related to Cloud-based infrastructures and services.
CloudSim is powered by jProfiler.
Overview of CloudSim functionalities:

  • support for modeling and simulation of large scale Cloud computing data centers
  • support for modeling and simulation of virtualized server hosts, with customizable policies for provisioning host resources to virtual machines
  • support for modeling and simulation of energy-aware computational resources
  • support for modeling and simulation of data center network topologies and message-passing applications
  • support for modeling and simulation of federated clouds
  • support for dynamic insertion of simulation elements, stop and resume of simulation
  • support for user-defined policies for allocation of hosts to virtual machines and policies for allocation of host resources to virtual machines
Software Required:
Netbeans IDE (version greater than 5) 
CloudSim

Steps:

1.   1. Netbeans Installation:Simply click on the downloaded file and follow it with default settings. Open the netbeans. Click file then new project.
2 Select java and java application.
       3. It will take few seconds to create the project.
4. Give name to your project and uncheck to create main file option.
5. Netbeans will take time to make project.
Once it’s done double click on the new project in solution explorer. Right click on libraries and choose the Add jar files.

Locate the cloud sim folder and you will find cloudsim-3.0.3.jar file. Click on ok.
Now locate the org folder which is present in the examples folder in main cloud sim folder. Copy the org folder.

 6. Right click on the source packages in the solution explorer panel and paste it. To run the example program, open org.cloudbus.cloudsim.examples in source packages. Right click on example you want to run and click run file.

7. Done! See the output below.

Thanks for reading!!