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

Consider the following programs. - echo_server.c (https://ufile.io/lzz1k) - echo

ID: 3849143 • Letter: C

Question

Consider the following programs.
- echo_server.c (https://ufile.io/lzz1k)
- echo_client.c (https://ufile.io/53aek)
The programs implement an example of applications where the client sends messages to the server and returns the message. The limitation that exists is that the server can only serve one customer at a time.
1. Expand the server function so that it can create new customer connections and handle them in new processes using the fork command.
2. When you finish the implementation try to connect to the server from two different clients and after exchanging some messages terminate the customer connections.
Show your processes using the ps command. Observe whether there are zombie processes.
If you notice such processes, investigate what changes you should make in your program to eliminate this possibility.

Explanation / Answer

For this case, te server process:

#include <stdio.h>
#include <sys/types.h>
#include <nnetinet/in.h>
#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>

int main()
{
int server_soc, client_soc;
int serv_len,cl_len;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
server_soc = socket(AF_INST, SOCK_STREAM, 0);
serevr_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(9734);
server_len = sizeof(server_addr);
bind(server_soc,(struct sockaddr*)&server_addr,server_len);


/*create connection queue*/

listen(server_soc, 5);
signal(SIGCHLD, SIG_IGN);
while(1)
{
char ch;
printf("server is waiting... ");


/*Accept connection*/

client_len = sizeof(client_addr);
client_soc = accept(server_soc,(struct sockaddr*)&client_address,&client_len);

if(fork()==0)
{

read(client_sec,&ch, 1);
sleep(5);
ch++;
write(client_soc, &ch, 1);
close(client_soc);
exit(0);
}

else{

close(client_soc);
}
}
}