(1) Create a C server program and a C client program. The client program reads a
ID: 3755527 • Letter: #
Question
(1) Create a C server program and a C client program. The client program reads a line from a text file, converts everything to upper case letters, and send it to the server, using a named pipe. It pauses for 1 second, and repeats the above process, until the end of a file.
The client sends a "Stop" string at the end.
The server displays any thing it receives. The server quits when it sees "Stop". The server needs to cleanup by deleting the named pipe.
The programs (client and server) need to create the name pipe first, if it does not exist.
(2) Re-implement Part 1, using SysV shared memory.
(3) Re-implement Part 2, using mmap() to create a shared memory area between the client and the server.
Explanation / Answer
ANSWER:
The client program reads a line from a text file, converts everything upper case letters,and send it to the server,using a named pipe.
Client File:
#include <stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include <stdlib.h>
int main(void) {
char str[1000];
FILE *fptr;
if ((fptr = fopen("myPIPE.txt", "r")) == NULL)
{
printf("file does not exist");
}
// Name PIPE (FIFO) file path
char * myPIPE = "/dikshita/desktop/myfifo";
// Creating the named PIPE file(FIFO) if it does not exist
// mkfifo(<pathname>, <permission>)
mkfifo(myPIPE, 0444);
fptr = open(myPIPE, O_WRONLY);
While(fgets(str, 1000, fptr) != NULL)
{
// Converting to uppercase
printf("The string in upper case: %s ", strupr(str));
// pause for 1 second
delay(1000);
}
Printf("Stop");
fclose(fptr);
}
Server File:
#include <stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include <stdlib.h>
int main(void) {
FILE *fread;
char * myPIPE = "/dikshita/desktop/myfifo";
char str2[1000];
fread = open(myPIPE,O_RDONLY);
read(fread, str2, 1000);
// Display the text
printf("%s ", str2);
if(fscanf(fread,"%[^ ]", "STOP"))
{
exit(1);
}
remove(myPIPE);
remove("myPIPE.bin");
}
Output:
File myPIPE with port permission 0444
Hi
Hello
(1) Run client.c
The string in upper case: HI
The string in upper case:HELLO
(2) Run server.c
HI
HELLO
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.