Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include #include #include

static void die(const char *s) { perror(s); exit(1); }

int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: %s ", argv[0]); exit(1); }

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

How We Listen?

Answered: 1 week ago