Вы находитесь на странице: 1из 4

Birla Institute of Technology & Science – Pilani

Work Integrated Learning Programmes


First Semester 2019-2020 (SS ZG-526)
Assignment-1 (Max marks:10)
Date given: 10th Oct, 2019 Date of submission: 21st Oct 2019
-------------------------------------------
Task 1 (6 Marks): You have to implement a calculator service using remote procedure call
(SUN RPC package) as discussed in the live sessions, which takes 2 integer arguments as inputs
and performs the following operations:

1. Addition of two numbers. 2. Multiplication of two numbers.

3. Subtraction of two numbers. 4. Division of two numbers.

5. Remainder of two numbers (First Number % Second Number).

6. Check if the first number (argument) is a prime number (For example, if the input is 23
and 10, then output should be 1 (or yes)).

Code for the above functionalities is given below (.x IDL file, client.c, and server.c files). Your
task is to add the following additional functionalities into the given program:

7. Check if both the input arguments are even or both are odd. Display “Yes” if both are
either even or both are odd, else display “No”.
8. Pl. ensure proper error checks like, if the input arguments are not integers then the
server should respond back with an error message as “arguments are not of valid type”.

Code:

cal.x fle:
struct variable
{
int x; int y; int ans; int choice;

};
program calc_prg
{
version calc_version
{
int calc(variable)=1;
}=1;
}=0x32345676;

cal_client.c file:
#include "cal.h"

void calc_prg_1(char *host)


{
CLIENT *clnt;
int *result_1;
variable calc_1_arg;

clnt = clnt_create (host, calc_prg, calc_version, "udp");


if (clnt == NULL) {
clnt_pcreateerror (host); exit (1);
}
printf("\n1 addition \n2.multiplication \n3.subtraction \n4.division
\n5.remainder \n6.is first number prime\nenter choice : ");
scanf("%d",&calc_1_arg.choice);
printf("enter values");
scanf("%d %d",&calc_1_arg.x,&calc_1_arg.y);

result_1 = calc_1(&calc_1_arg, clnt);


if (result_1 == (int *) NULL) {
clnt_perror (clnt, "call failed");
}
if(calc_1_arg.choice!=6)
printf("Your result is %d\n",*result_1);
else if(calc_1_arg.choice==6 && *result_1==1)
printf("\nYES\n");
else
printf("\nNO\n");
clnt_destroy (clnt);
}

int main (int argc, char *argv[])


{
char *host;

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
calc_prg_1 (host);
exit (0);
}

cal_server.c file:

#include "cal.h"
int isprime(int a)
{
int i;
for(i=2;i<=a/2;i++)
{if(a%i==0)
{break;}}
if(i==a/2+1)
{
printf("\nYES\n");
return 1;
}
else
{
printf("\nNO\n");

return 0;
}
}

int *calc_1_svc(variable *argp, struct svc_req *rqstp)


{
static int result;
switch(argp->choice)
{

case 1:
{
result=(argp->x)+(argp->y);
break;
}
case 2:
{
result=(argp->x)*(argp->y);
break;
}
case 3:
{
result=(argp->x)-(argp->y);
break;
}
case 4:
{
result=(argp->x)/(argp->y);
break;
}
case 5:
{
result=(argp->x)%(argp->y);
break;
}
case 6:
{
result=isprime(argp->x);
break;
}
}
return &result;
}

Task 2 (4 Marks): Lamport proposed a scheme to order the events in a distributed system by
using logical clocks. Due to the absence of synchronized clocks and hence global time in a
distributed system, the order in which events occur at two different machines is impossible to
be determined based on the local time at which they occurred.

The implementation rules are as below:

a) Clock Ci is implemented between any two events of the same process as Ci = Ci + d (d >0)
b) If event a is a send message by process Pi and the same is received by process Pj, then tm =
Ci (a), and Cj = max (Cj, tm+d), d>0.

Below code is an implementation of the above two rules (simulation). Output is also shown for
clarity. Your task is to modify the below program such that value of ‘d’ becomes 3 for all the
processes, and the starting value of the clock at P1, P2, and P3 is 4. Submit the code with a
snapshot of the run.

Code:
#include<stdio.h>
int main()
{
int p1[10],p2[10];

int e,i,m1,m2;
printf("\n enter the no of events in p1 and p2\n");
scanf("%d",&e);
printf("\n enter the event of p1 which will send msg\n");
scanf("%d",&m1);
printf("\n enter the event of p2 which will receive the msg\n");
scanf("%d",&m2);
p2[0]=0;
for(i=1;i<=e;i++) Output:
{
if(i==m2)
{
if(m1>p2[i-1])
p2[i]=m1+1;
else
{
p2[i]=p2[i-1]+1;
}
}
else
{
p2[i]=p2[i-1]+1;
}
}

printf("\nTime stamp for P1\n");


for(i=1;i<=e;i++)
{
printf("%d ",i);
}
printf("\nTime stamp for P2\n");
for(i=1;i<=e;i++)
{
printf("%d ",p2[i]);
}
printf("\n");
return 0;
}

Submission instruction: Pl submit your all source files and snapshot of code run through the
submission portal.
--------------------------------

Вам также может понравиться