In this assignment, you will modify the client and server programs that use pipe
ID: 3904657 • Letter: I
Question
In this assignment, you will modify the client and server programs that use pipe to communicate. 1. The client keeps prompting the user for a number (positive integer) and send the number to the 2. The server gencrates a random key between 0 and 99. It then waits and reads from the pipe. I 3. A sample run of the programs are as follows. server via pipe. The client exits when the user inputs -1 will compare the number sent and the key and give proper response On the client side: turi ng3:-/Documents/AssignS./client Please give a nunber (-1 to exit):33 Please give a nunber (-1 to exit):72 Please give a nunber(-1 to exit):96 Please give a nunber (-1 to exit):84 Please give a nunber (-1 to exit):83 Please give a nunber (-1 to exit):-1 turing?:-/Documents/Assigns% On the server side: turing?:-/Documents/AssignS% ./server The key is bigger The key is bigger The key is smaller The key is smaller You got it, The key is 83 turing? :-/Documents/Assign5% I General Requirements Your programs' implementation must include the following features: The client will keep prompting the user for a number (positive integer), and then read the number send the number to the server via pipe. If the number is-1, the client exits. . . The server generates random key between 0 and 99, It then waits to read from the pipe .If there is nothing in the pipe, the server waits (blocked). When the server reads a number, it exits if the number. Otherwise, it will compare the number with the key. It will print a message depending on the result of the comparison. .Your programs must be compiled from 3 source files: o elient.e (Handles user input and write to the pipe) o server.c (Reads from the pipe and prints) o myfifo.h (Declares global variables and constants) . Your myfifo h file must contain the proper preprocessor directives to prevent multiple inclusion . You must use a makefile to create two executables: client and server .You may use any systems or tools to create and run your program. However, your program must run correctly on a departmental Linux computer Please follow the guidelines in the programming guideline document.Explanation / Answer
client.c
#include"myfifo.h"
int main()
{
char file[20],data[20];
int flag=0;
char *a_b_fifo="./a_to_b_fifo_file";
int ab;
char *b_a_fifo="./b_to_a_fifo_file";
int ba;
mkfifo(a_b_fifo,0666);
ab=open(a_b_fifo,O_WRONLY);
mkfifo(b_a_fifo,0666);
ba=open(b_a_fifo,O_RDONLY);
while(strcmp("DONE",data)!=0)
{
printf(" Please give a number (-1 to exit):");
scanf("%s",file);
write(ab,file,sizeof(file));
read(ba,data,sizeof(data));
}
printf(" Out Of DATA LOOP ");
close(ba);
close(ab);
unlink(a_b_fifo);
unlink(b_a_fifo);
return 0;
}
server.c
#include"myfifo.h"
int main()
{
int data_=100,key;
char file[20],data[20];
srand(time(0));
key=(rand() + 0) % (99 + 1);
//printf("key=%d ",key);
char *a_b_fifo="./a_to_b_fifo_file";
int ab;
char *b_a_fifo="./b_to_a_fifo_file";
int ba;
mkfifo(a_b_fifo,0666);
mkfifo(b_a_fifo,0666);
ab=open(a_b_fifo,O_RDONLY);
ba=open(b_a_fifo,O_WRONLY);
while(data_>=0)
{
read(ab,data,sizeof(data));
data_=atoi(data);
if(data_==key)
{
printf(" You got it, key is %d",data_);
write(ba,"DONE",100);
}
else if(data_<key)
{
printf(" Key is smaller");
write(ba,"SMALLER",100);
}
else
{
printf(" Key is greater");
write(ba,"GREATER",100);
}
}
close(ba);
close(ab);
unlink(a_b_fifo);
unlink(b_a_fifo);
return 0;
}
myfifo.h
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>