Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIS 340 System Programming Assignment5 Due: 8:00pm, Tue. April 19,2018 100 point

ID: 3906254 • Letter: C

Question

CIS 340 System Programming Assignment5 Due: 8:00pm, Tue. April 19,2018 100 points In this assignment, you will modify the client and server programs that use pipe to communicate 1- 2. The server generates a random key between 0 and 99. It then waits and reads from the pipe. It 3. A sample run of the programs are as follows. The client keeps prompting the user for a number (positive integer) and send the number to the 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: turing?:-/Documents/Assign5% ./client Please give a number (-1 to exit):33 Please give a number (-1 to exit):72 Please give a number (-1 to exit):96 Please give a number (-1 to exit):84 Please give a number (-1 to exit):83 Please give a number (-1 to exit):-1 turing 3:-/Documents/Assigns% On the server side the key i/Documents/Assign5./server The key is bigger The key is smal1er The key is smaller You got it, The key is 83 turing 3 :-/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 is -1. 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: . client.e (Handles user input and write to the pipe) server.c (Reads from the pipe and prints) myfifo.h (Declares global variables and constants). o o o 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 . .

Explanation / Answer

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>

Client

#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

#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;
}