Question

Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need...

Client / Server using named pipes with thread and fork() in C/C++

**Note** You will need to write a client program and a server program for this question to be correct. **
**Cannot use socket**

client

the client application that will get the command from the user and pass the command to be executed from the command line, parses the command and puts a binary representation of the parse into a shared memory segment. At this point, the client will listen on the read size of a named pipe to get the output from the command. Display this information on standard output.

server

the server application will listen for requests from clients in shared memory, process these requests when it receives them from the client, and return data back to the client over the connection. The server must be able to handle multiple connections at the same time.
The client and server need to communicate with each other using shared memory and named pipes. For each new request in shared memory, the server should start up a new thread. This thread will need to start a new thread for each command; this thread will need to use fork() and exec() to launch the new process for the command to be executed. You will need to keep track of which client has started the connection (the contents of the shared memory will need to include an indication of which client started the process).

Homework Answers

Answer #1

Pipe is one of the message passing IPC resource widely used on unix operating system platforms. Pipes provide
unidirectional interprocess communication channel. A pipe has a read end and a write end. Data written to the
write end of a pipe can be read from the read end of the pipe.pipes can be created using pipe api, which creates a new pipe and returns two file descriptors, one referring to the read end of the pipe, the other referring to the write end. Pipes can be used to create a communication channel between related processes.

Client-Server program.

#include <stdio. h>
#include <stdlib. h>
#include <unistd. h>
#include <string. h>
void client(int readfd, int writefd)
{
char msg[ 100] ;
int n;
char eof = EOF;
printf("%d enter msg to be sent to server: ", getpid() ) ;
fgets(msg, 100, stdin) ;
if (write(writefd, msg, strlen(msg) ‐ 1) == ‐1) {
perror("error writing. . . ") ;
exit(0) ;
perror("error reading. . . ") ;
exit(0) ;
}m
sg[ n] = ' ' ;
printf("received from server: %sn", msg) ;
}

void server(int readfd, int writefd)
{
char msg[ 100] ;
int n;
if ((n = read(readfd, msg, 100) ) < 0) {
perror("error reading. . . ") ;
}m
sg[ n] = ' ' ;
printf("%d server received from client: %sn", getpid() , msg) ;
printf("enter msg to be sent to client: ") ;
fgets(msg, 100, stdin) ;
write(writefd, msg, strlen(msg) ‐ 1) ;
}i
nt main()
{
int pipe1fd[ 2] , pipe2fd[ 2] ;
int pid;
/* create two pipes */
if (pipe(pipe1fd) == ‐1) {
perror("pipe: ") ;
return 0;
}i
f (pipe(pipe2fd) == ‐1) {
perror("pipe: ") ;
return 0;
}
/* start child process and run client code in it */
pid = fork() ;
if (pid == 0) {
close(pipe1fd[ 1] ) ; //close the write end of the first pipe
close(pipe2fd[ 0] ) ; //close the read end of the second pipe
client(pipe1fd[ 0] , pipe2fd[ 1] ) ;
exit(0) ;
}e
lse {
/* code that runs in parent ; runs as server */
close(pipe1fd[ 0] ) ; // close read end of the first pipe
close(pipe2fd[ 1] ) ; // close write end of the second pipe
server(pipe2fd[ 0] , pipe1fd[ 1] ) ;
wait(NULL) ; //wait for child process to finish
return 0;
}
}

The following program creates a pipe, and then forks to create a child process; the child inherits pipe file
descriptors that refer to the same pipe. After fork() each process closes the descriptors that it doesn’t need for
the pipe . The parent then waits on pipe1 for data and child process sends a string message , for which parent
sends response message through pipe2, and the child reads this string a byte at a time from the pipe and echoes it on standard output.

Shared memory:Shared memory is what the name says about it: a segment of memory shared between several processes.Shared memory in UNIX is based on the concept of memory mapping. The segment of memory shared is coupled with an actual file in the filesystem. The file content is a mirror of the memory segment at any time. The mapping between the shared segment content and the mapped file is persistent.

/*
* shm_msgclient.c
*/

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>

#define MAX_MSG_LENGTH
#define SHMOBJ_PATH /*shared memory object path*/

#define TYPES 8


struct msg_s {
int type;
char content[MAX_MSG_LENGTH];
};

int main(int argc, char *argv[]) {
int shmfd;
int shared_seg_size = (1 * sizeof(struct msg_s));
struct msg_s *shared_msg;   
shmfd = shm_open(SHMOBJ_PATH, O_RDWR, S_IRWXU | S_IRWXG);

if (shmfd < 0) {
perror("In shm_open()");
exit(1);
}
printf("Created shared memory object %s\n", SHMOBJ_PATH);
  
shared_msg = (struct msg_s *)mmap(NULL, shared_seg_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
if (shared_msg == NULL) {
perror("In mmap()");
exit(1);
}
printf("Shared memory segment allocated correctly (%d bytes).\n", shared_seg_size);

printf("Message type is %d, content is: %s\n", shared_msg->type, shared_msg->content);
   return 0;
}

/*
* shm_msgserver.c */

#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>

#define MAX_MSG_LENGTH   
#define TYPES

struct msg_s {

int type;
char content[MAX_MSG_LENGTH];
};

int main(int argc, char *argv[]) {
int shmfd;
int shared_seg_size = (1 * sizeof(struct msg_s));   
struct msg_s *shared_msg;   
  shmfd = shm_open(SHMOBJ_PATH, O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG);
if (shmfd < 0) {
perror("In shm_open()");
exit(1);
}
fprintf(stderr, "Created shared memory object %s\n", SHMOBJ_PATH);
    ftruncate(shmfd, shared_seg_size);
shared_msg = (struct msg_s *)mmap(NULL, shared_seg_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
if (shared_msg == NULL) {
perror("In mmap()");
exit(1);
}
fprintf(stderr, "Shared memory segment allocated correctly (%d bytes).\n", shared_seg_size);

srandom(time(NULL));

   shared_msg->type = random() % TYPES;
snprintf(shared_msg->content, MAX_MSG_LENGTH, "My message, type %d, num %ld", shared_msg->type, random());

  if (shm_unlink(SHMOBJ_PATH) != 0) {
perror("In shm_unlink()");
exit(1);
}

return 0;

}

compile using : gcc -o shm_msgserver shm_msgserver.c and
gcc -o shm_msgclient shm_msgclient.c

Run:

./shm_msgserver
./shm_msgclient

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The goal of this assignment is to implement a simple client-server system. You have to use...
The goal of this assignment is to implement a simple client-server system. You have to use Python. The basic functionality of the system is a remote calculator. You first will be sending expressions through the client to the server. The server will then calculate the result of the expression and send it back to the client where it is displayed. The server should also send back to the client the string "Socket Programming" as many times as the absolute value...
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas,...
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas, the following les for each of your programs: (1) the client and the server source les each (2) the client and the serve executable les each (3) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identiers suitable, to enable enhanced readability of the code. Problems 1. Write an ftp client that...
The code I have written runs but I need it us UDP not TCP. Also I...
The code I have written runs but I need it us UDP not TCP. Also I just need someone to check my work and make sure that it works properly. The python code needs to be run with command line so you can add more than one client. The directions: 1. The chat is performed between 2 clients and not the server. 2. The server will first start up and choose a port number. Then the server prints out its...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
      MK Restaurant: Branding of Thai-Style Hotpot The restaurant industry is one of the most...
      MK Restaurant: Branding of Thai-Style Hotpot The restaurant industry is one of the most competitive in Thailand. With a large number of players ranging from restaurants in five-star hotels, global fast-food chains to small stalls along the streets and everything in between, the Thais are spoiled for choice. In addition, as the world becomes globalized, consumers are familiar with international dishes and would not hesitate to try new offerings from the other side of the globe. As a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT