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

*/