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

I would greatly appreciate if you could come up with a solution for this. Each p

ID: 3809058 • Letter: I

Question

I would greatly appreciate if you could come up with a solution for this. Each picture is a continuation.

(1) Compile and run these two examples (from APUE book) provided below. arning. It may not compile and you need to fix it or make it compile. [Hint. check apue.h or for err sys for the similar activities last few weeks.]). (2) Explain the logic of the program in Example 2, especially on (a) when SIGTSTP is received, which process (parent or child) is handling the signal, and (b) what happens (or is done) by the signal handler (signhand). void sighand int signum supposedly need to reset this here per man signal and it's discussion of signals under linux), but doesn't actually seem to matter. Documentation observed program behavior kill (pid child, SIGKILL); printf (stderr nyah, nyah Mr. i sd.Nn", signum) (3) Modify the program in Example 2 so that (a) the signal is handled by the child (not the parent, and (b) output message (fprintfstatement in sighand for SIGTSTP will also print the PID of the current process handling the signal. void sighand (int signum supposed y need to reset this here per man signal and it's discussion of signals under linux), but doesn't actually seem to matter. Documentation observe program behavior. d kill pid child, SIGKILL fprintf (stderr nyah nyah Mr. d.Nn", signum)

Explanation / Answer

1) To compile Example 1, make sure you download the source code that comes with the APUE (Advanced Programming in the UNIX enivronment) book.

a) Compile the downloded source code as mentioned in README.

e.g.

$ cd apue.3d

$ make

Use following command to compile Example 1.

e.g. (Assumption: Downloaded source code 'apue.3d' is extracted under the same folder where Example 1 code is present.)

$ gcc example1.c -I ./apue.3d/include -L ./apue.3d/lib -lapue

Use following command to compile Example 2.

$ gcc example2.c

2) Example 2 involves following logic:

(a) The parent process handles the signal whereas the child process ignores it.

(b) The signal handler registred by parent process terminates the child process and prints following text. In below text 20 is the signal number for SIGTSTP (20).

nyah, nyah Mr. 20.

(3)

(a) Modify following code snippet so that the parent ignores the SIGTSTP signal and the child process registers a handler for it.

if(pid == 0) { // code to be run by the child
               signal(SIGSTP, sighand);
                   status = execvp(command, args); // execute
                   exit(0); // exit child
               }
               else { // code to be run by the parent
               pid_child = pid;
               signal(SIGSTP, SIG_IGN);
                   waitpid(-1); // wait for children to finish
}

(b) getpid() method can be used to print a process's PID as shown below.

void sighand(int signum)
{
kill(pid_child, SIGKILL);
   fprintf(stderr, "My PID is %d. nyah, nyah Mr. #%d. ", getpid(), signum);
}