Question
Write an improved version: mdb-lookup-server-nc-2.c mdb-lookup-server-nc-1.c from (b) forked once, exec'ed the script, waited until it's done, and then exited. mdb-lookup-server-nc-2.c will do the following:
Write an improved version: mdb-lookup-server-nc-2.c
mdb-lookup-server-nc-1.c from (b) forked once, exec'ed the script, waited until it's done, and then exited.
mdb-lookup-server-nc-2.c will do the following:
- It has a loop in which it displays a prompt, "port number: ". It reads the port number typed in by the user and then fork/exec the script from (a) using that port number. It also prints out a message stating that an instance of mdb-lookup-server has started. The message should include the child's process ID and the port number on which it's listening.
- Hitting ENTER on the prompt should simply display another prompt.
- On every iteration, before it displays a prompt, it should check if any of the child processes have terminated. It should display the process IDs of all mdb-lookup-server processes that have terminated since the last prompt was displayed, along with messages saying that they have terminated. For this, you need to use the non-blocking version of waitpid() system call. Here is how you use it:
pid = waitpid( (pid_t) -1, NULL, WNOHANG);
- Use Ctrl-C to quit.
Here is a program that runs the shell script from (a) as a child process (mdb-lookup-server-nc-1.c):
#include
static void die(const char *s) { perror(s); exit(1); }
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: %s
pid_t pid = fork(); if (pid < 0) { die("fork failed"); } else if (pid == 0) { // child process fprintf(stderr, "[pid=%d] ", (int)getpid()); fprintf(stderr, "mdb-lookup-server started on port %s ", argv[1]); execl("./mdb-lookup-server-nc.sh", "mdb-lookup-server-nc.sh", argv[1], (char *)0); die("execl failed"); } else { // parent process if (waitpid(pid, NULL, // no status 0) // no options != pid) die("waitpid failed"); fprintf(stderr, "[pid=%d] ", (int)pid); fprintf(stderr, "mdb-lookup-server terminated "); }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started