Sunday, 23 October 2016

Program For Calculating Factorial Using Remote Procedure Call (RPC)

Note:  Make Sure you had done following before proceeding

sudo apt-get install rpcbind

Then Create Directory With Any Name As Follow

mkdir rpcprogram

then in the directory using following command

cd rpcprogram

Follow the step as given below

1) Creating  file with .x  extension

I am using gedit editor

so

gedit fact.x

2) Add following code to fact.x


struct intpair 
{
int a;
};

program FACT_PROG 
{
version FACT_VERS 
{
int FACT(intpair) = 1;
} = 1;

} = 0x23451111;

Save and Exit File

3) We Need RPC to generate C Code to implement RPC protocol
use the following command with your .x file name

rpcgen -a -C fact.x


4) Edit the Makefile

gedit Makefile.fact


5) Search for following Lines in File

CFLAGS += -g

Change it to:

CFLAGS += -g -DRPC_SVC_FG


6) Again search the following and replace

RPCGENFLAGS =

Change it to:

RPCGENFLAGS = -C

# save and exit the file


7) Client program

gedit fact_client.c

(Note the changes in the following Program )

#include "fact.h"

void fact_prog_1(char *host,int a)
{
CLIENT *clnt;
int *result_1;
intpair fact_1_arg;
#ifndefDEBUG
clnt = clnt_create (host, FACT_PROG, FACT_VERS, "udp");
if (clnt == NULL)
{
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */

fact_1_arg.a=a;
result_1 = fact_1(&fact_1_arg, clnt);
if (result_1 == (int *) NULL)
{
clnt_perror (clnt, "call failed");
}
else
{
printf("Factorial=%d",*result_1);
}

#ifndefDEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int main (int argc, char *argv[])
{
char *host;
int a,ch;
if (argc < 2)
{
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}

host = argv[1];
do
{
system("clear");
printf("\nEnter a no:: ");
scanf("%d",&a);
fact_prog_1 (host,a);
printf("\nTry again : (1/0) :: ");
scanf("%d",&ch);
} while(ch==1);

exit (0);

}



8) Server Program

gedit fact_server.c

(Note the changes in the following Program )


#include "fact.h"
int *fact_1_svc(intpair *argp, struct svc_req *rqstp)
{
static int result,n,fact;
int i;
n=argp->a;
// factorial logic
fact = 1;
printf("\n Received : n= %d \n",n);
for (i=n;i>0;i--)
{
fact=fact * i;
}
result=fact;
return &result;
}


9) Run Programs 



# In terminal 1, run:

sudo ./fact_server

# In terminal 2, run:

./fact_client localhost


Output:

humble@humble-desktop:~$ cd exp2
humble@humble-desktop:~/exp2$ make -f Makefile.fact

cc -g -DRPC_SVC_FG -c -o fact_clnt.o fact_clnt.c
cc -g -DRPC_SVC_FG -c -o fact_client.o fact_client.c
cc -g -DRPC_SVC_FG -c -o fact_xdr.o fact_xdr.c
cc -g -DRPC_SVC_FG -o fact_client fact_clnt.o fact_client.o fact_xdr.o -lnsl
cc -g -DRPC_SVC_FG -c -o fact_svc.o fact_svc.c
cc -g -DRPC_SVC_FG -c -o fact_server.o fact_server.c
cc -g -DRPC_SVC_FG -o fact_server fact_svc.o fact_server.o fact_xdr.o -lnsl

humble@humble-desktop:~/exp2$



At server:

humble@humble-desktop:~$ cd exp2
humble@humble-desktop:~/exp2$ sudo ./fact_server

Received : n= 5


At client:

humble@humble-desktop:~$ cd exp2
humble@humble-desktop:~/exp2$ ./fact_client localhost

Enter a no:: 5
Factorial=120

Try again : (1/0) ::

Program For Reversing String Using Message Passing Interface (MPI)

1) Program For Server Side 

server.c 

#include <stdlib.h> #include <stdio.h>
#include "mpi.h"
#include<string.h>
int main(int argc, char **argv)
{
MPI_Comm client;
MPI_Status status;
char port_name[MPI_MAX_PORT_NAME],str[50],ch,temp;
int size, again, i,j;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 1)
{
fprintf(stderr, "Server too big");
exit(EXIT_FAILURE);
}
MPI_Open_port(MPI_INFO_NULL, port_name);
printf("Server available at port: %s\n", port_name);
i=0;
while (1)
{
MPI_Comm_accept(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &client);
again = 1;
while (again)
{
MPI_Recv(&ch, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, client, &status);
switch (status.MPI_TAG)
{
case 0:
MPI_Comm_free(&client);
MPI_Close_port(port_name);
MPI_Finalize();
return 0;
case 1:
printf("\nReceived String: %s\n",str);
// reverse the string
i = 0;
j = strlen(str) - 1;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}

printf("\nReversed string is : %s\n",str);

// send the reversed string to client (character by character)
for (i = 0; i < strlen(str); i++)
{
ch=str[i];
MPI_Send(&ch, 1, MPI_CHAR, 0, 2, client);
}

//send tag=1 to indicate end of string
MPI_Send(&ch, 1, MPI_CHAR, 0, 1, client);
MPI_Comm_disconnect(&client);
again = 0;
strcpy(str,"");
i=0;
break;
case 2:
printf("Received character: %c\n", ch);
str[i]=ch;
i++;
// add null character at the end of string
str[i]='\0';
break;
default:
/* Unexpected message type */
MPI_Abort(MPI_COMM_WORLD, 1);
}
}
}

}


2) Program For Client Side

client.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mpi.h"

int main( int argc, char **argv )
{
MPI_Comm server;
MPI_Status status;
char port_name[MPI_MAX_PORT_NAME],str[50],ch;
int i, tag,again;
if (argc < 2)
{
fprintf(stderr, "server port name required.\n");
exit(EXIT_FAILURE);
}
MPI_Init(&argc, &argv);
strcpy(port_name, argv[1]);
MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &server);

// accept input string
printf("\nEnter the string :\n");
scanf("%s",str);

//send string to server (character by character)
for (i = 0; i < strlen(str); i++)
{
if(str[i]!='\0')
ch=str[i];
tag=2;
MPI_Send(&ch, 1, MPI_CHAR, 0, tag, server);
}

// done sending string to the server
MPI_Send(&i, 0, MPI_INT, 0, 1, server);

// Receive the reversed string from server and display it
i=0;
again=1;
while (again)
{
MPI_Recv(&ch, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, server, &status);
switch (status.MPI_TAG)
{
case 2:
str[i]=ch;
i++;
break;
case 1:
again=0;
break;
}
}

printf("\nReversed string is : %s\n\n",str);
MPI_Comm_disconnect(&server);
MPI_Finalize();
return 0;

}




/* OUTPUT
At Server Side

humble@humble-desktop:~$ cd exp4
humble@humble-desktop:~/exp4$ mpicc server.c -o server
humble@humble-desktop:~/exp4$ mpicc client.c -o client
humble@humble-desktop:~/exp4$ mpirun -np 1 ./server

Received character: H
Received character: u
Received character: m
Received character: b
Received character: l
Received character: e
Received String: Humble
Reversed string is: elbmuH




At Client Side

humble@humble-desktop:~$ cd exp4
humble@humble-desktop:~/exp4$ mpirun -np 1 ./client '2108030976.0;tcp://172.16.219.177:53146+2108030977.0;tcp://172.16.219.177:39924:300'
Enter the string :
Humble
Reversed string is : elbmuH

*/

Saturday, 22 October 2016

Program For KMeans


public class KMeans {

    public static void main(String[] args) {
        int data[] = {2, 5, 6, 8, 12, 15, 18, 28, 30};    // initial data
        int noofclusters = 3;
        int centroid[][] = new int[][]{
            {0, 0, 0},
            {2, 12, 30}
        };
        getCentroid(data, noofclusters, centroid);

    }

    public static int[][] getCentroid(int data[], int noofclusters, int centroid[][]) {

        int distance[][] = new int[noofclusters][data.length];
        int cluster[] = new int[data.length];
        int clusternodecount[] = new int[noofclusters];

        centroid[0] = centroid[1];
        centroid[1] = new int[]{0, 0, 0};
        System.out.println("========== Starting to get new centroid =========");

        for (int i = 0; i < noofclusters; i++) {
            for (int j = 0; j < data.length; j++) {
                distance[i][j] = Math.abs(data[j] - centroid[0][i]);
                System.out.print(distance[i][j] + " ,");
            }
            System.out.println();
        }

        for (int j = 0; j < data.length; j++) {
            int smallerDistance = 0;
            if (distance[0][j] < distance[1][j] && distance[0][j] < distance[2][j]) {
                smallerDistance = 0;
            }
            if (distance[1][j] < distance[0][j] && distance[1][j] < distance[2][j]) {
                smallerDistance = 1;
            }
            if (distance[2][j] < distance[0][j] && distance[2][j] < distance[1][j]) {
                smallerDistance = 2;
            }

            centroid[1][smallerDistance] = centroid[1][smallerDistance] + data[j];
            clusternodecount[smallerDistance] = clusternodecount[smallerDistance] + 1;
            cluster[j] = smallerDistance;
        }
        System.out.println("New centroid is ");

        for (int j = 0; j < noofclusters; j++) {
            centroid[1][j] = centroid[1][j] / clusternodecount[j];
            System.out.print(centroid[1][j] + ",");
        }
        System.out.println();

        boolean isAchived = true;
        for (int j = 0; j < noofclusters; j++) {
            if (isAchived && centroid[0][j] == centroid[1][j]) {
                isAchived = true;
                continue;
            }
            isAchived = false;
        }

        if (!isAchived) {
            getCentroid(data, noofclusters, centroid);
        }

        if (isAchived) {
            System.out.println("======================================== ");
            System.out.println(" Final Cluster is ");
            for (int i = 0; i < noofclusters; i++) {
                for (int j = 0; j < data.length; j++) {
                    if (cluster[j] == i) {
                        System.out.print(data[j] + " ,");
                    }

                }
                System.out.println();
            }
        }

        return centroid;

    }
}

/* OUTPUT 


========== Starting to get new centroid =========
0 ,3 ,4 ,6 ,10 ,13 ,16 ,26 ,28 ,
10 ,7 ,6 ,4 ,0 ,3 ,6 ,16 ,18 ,
28 ,25 ,24 ,22 ,18 ,15 ,12 ,2 ,0 ,
New centroid is 
4,13,29,
========== Starting to get new centroid =========
2 ,1 ,2 ,4 ,8 ,11 ,14 ,24 ,26 ,
11 ,8 ,7 ,5 ,1 ,2 ,5 ,15 ,17 ,
27 ,24 ,23 ,21 ,17 ,14 ,11 ,1 ,1 ,
New centroid is 
5,15,29,
========== Starting to get new centroid =========
3 ,0 ,1 ,3 ,7 ,10 ,13 ,23 ,25 ,
13 ,10 ,9 ,7 ,3 ,0 ,3 ,13 ,15 ,
27 ,24 ,23 ,21 ,17 ,14 ,11 ,1 ,1 ,
New centroid is 
5,15,29,
======================================== 
 Final Cluster is 
2 ,5 ,6 ,8 ,
12 ,15 ,18 ,
28 ,30 ,
*/

Program For Web Page Ranking


import java.util.*;
import java.io.*;

public class PageRank {

    public int path[][] = new int[10][10];
    public double pagerank[] = new double[10];

    public void calc(double n) {
        Double init;
        double c = 0;
        double temp[] = new double[10];
        int i, j, u = 1, k = 1;
        init = 1 / n;
        System.out.printf(" n value:" + n + "\t init value :" + init + "\n");

        for (i = 1; i <= n; i++)
        {
            this.pagerank[i] = init;
            System.out.printf("\n Initial PageRank Values , 0th Step \n");
        }

        for (i = 1; i <= n; i++)
        {
            System.out.printf(" Page Rank of " + i + " is :\t" + this.pagerank[i] + "\n");

            while (u <= 2) {
                for (i = 1; i <= n; i++)
                {
                    temp[i] = this.pagerank[i];
                    this.pagerank[i] = 0;
                }
            }
            for (j = 1; j <= n; j++)
            {
                for (i = 1; i <= n; i++)
                {
                    if (this.path[i][j] == 1)
                    {
                        k = 1;
                        c = 0;
                        while (k <= n)
                        {
                            if (this.path[i][k] == 1)
                            {
                                c = c + 1;
                                k = k + 1;
                            }
                        }
                        this.pagerank[j] = this.pagerank[j] + temp[i] * (1 / c);
                    }

                    System.out.printf("\n After " + u + "th Step \n");
                    for (i = 1; i <= n; i++)
                    {
                        System.out.printf(" Page Rank of " + i + " is :\t" + this.pagerank[i] + "\n");
                        u = u + 1;
                    }
                }
            }
        }
    }

    public static void main(String args[])
    {
        int nodes, i, j, cost;

        Scanner in = new Scanner(System.in);
        System.out.println("Enter the Number of WebPages \n");

        nodes = in.nextInt();
        PageRank p = new PageRank();

        System.out.println("Enter the Adjacency Matrix with 1->PATH & 0->NO PATHBetween two WebPages: \n");

        for (i = 1; i <= nodes; i++)
        {
            for (j = 1; j <= nodes; j++)
            {
                p.path[i][j] = in.nextInt();
                if (j == i)
                {
                    p.path[i][j] = 0;
                }
            }
        }
        p.calc(nodes);
    }
}


/*
OUTPUT
Enter the Number of WebPages
5
Enter the Adjacency Matrix with 1->PATH & 0->NO PATH Between two WebPages:
0 1 0 1 0
0 0 1 0 1
0 0 0 1 0
0 0 0 0 1
1 1 1 0 0
n value:5.0
init value :0.2
Initial PageRank Values , 0th Step
Page Rank of 1 is : 0.2
Page Rank of 2 is : 0.2
Page Rank of 3 is : 0.2
Page Rank of 4 is : 0.2
Page Rank of 5 is : 0.2
After 1th Step
Page Rank of 1 is :
Page Rank of 2 is :
Page Rank of 3 is :
Page Rank of 4 is :
Page Rank of 5 is : 0.06666666666666667
0.16666666666666669
0.16666666666666669
0.30000000000000004
0.30000000000000004
After 2th Step
Page Rank of 1 is :
Page Rank of 2 is :
Page Rank of 3 is : 0.1
0.13333333333333333
0.18333333333333335Page Rank of 4 is :
Page Rank of 5 is :
0.2
0.3833333333333334 */