Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include #include #include int main ( int argc, char * * argv ) { / * * * * * *

#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv){
/*************************************************************
write code for the main process that will establish
the communication channel between cleint and server using
pipes
*************************************************************/
pid_t pid;
int fd[2];
// Create pipe
if (pipe(fd)==-1){
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid <0){
perror("Fork failed
");
exit(EXIT_FAILURE);
}
if (pid ==0){// Child process (server)
close(fd[1]); // Close write end in child
server(fd[0], fd[1]);
exit(EXIT_SUCCESS);
} else {// Parent process (client)
close(fd[0]); // Close read end in parent
client(fd[1], fd[0]);
wait(NULL); // Wait for child process to finish
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
void client(int readfd, int writefd){
char buf[MAX_BUFF];
size_t len;
/*************************************************************
write client code to be used by the parent process
*************************************************************/
// implement client functionality
while (1){
// Read expression from stdin
printf("Enter expression: ");
fgets(buf, MAX_BUFF, stdin);
// Check for termination condition
if (strcmp(buf, "END
")==0){
// Send termination signal to server
write(writefd, "END", 4);
break;
}
// Send expression to server
write(writefd, buf, strlen(buf)+1);
// Read result from server
read(readfd, buf, MAX_BUFF);
printf("RESULT: %s", buf);
}
// Close file descriptors
close(readfd);
close(writefd);
}
#include
#include
#include
#include
#include
#include
#include
void server(int readfd, int writefd){
/*************************************************************
write server code to be used by the child process
*************************************************************/
char buf[MAX_BUFF];
size_t len;
// Implement server functionlity here
while (1){
// Read expression from client
read(readfd, buf, MAX_BUFF);
// Check for termination signal
if (strcmp(buf, "END")==0){
break;
}
// Evaluate expression
double result = calculate(buf);
// Convert result to string and send to client
sprintf(buf,"%f
", result);
write(writefd, buf, strlen(buf)+1);
}
// Close file descriptors
close(readfd);
close(writefd);
}
#include
#include
#include
#include
#include
#include
#include
double calculate(char *buf){
/*************************************************************
Implement the expression evaluation functionality which
will be invoked by the server whenever required.
*************************************************************/
double operands[20];
char op[20];
int oprnInd =0;
double result =0.0;
// implement expression evaluation functionality here
char *token = strtok(buf,"");
while (token != NULL){
if (sscanf(token,"%lf", &operands[oprnInd])==1){
oprnInd++;
} else {
strcpy(op, token); // Corrected variable name from 'ops' to 'op'
}
token = strtok(NULL,"");
}
// Perform calculation based on BODMAS rule
result = operands[0];
int i;
for (i =0; i < oprnInd; ++i){
if (strcmp(op + i,"+")==0)
result += operands[i +1];
else if (strcmp(op + i,"-")==0)
result -= operands[i +1];
else if (strcmp(op + i,"*")==0)
result *= operands[i +1];
else if (strcmp(op + i,"/")==0)
result /= operands[i +1];
else if (strcmp(op + i,"%")==0)
result =(int)result %(int)operands[i +1];
}
return result;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions