I would greatly appreciate if you could solve this. I believe there is an error
ID: 3808488 • Letter: I
Question
I would greatly appreciate if you could solve this. I believe there is an error in Example 1 and I am not sure about Example 2. Please solve all 3 parts. Each picture is a continuation.
(1) Compile and run these two examples (from APUE book) provided below. [Warning. 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) fprintf (stderr nyah nyah Mr. #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 (fprintf statement in sighand for SIGTSTP will also print the PID of the current process handling the signal. void sig hand (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); fprintf (stderr nyah nyah Mr. tsd .An", signumExplanation / Answer
Example-1: You are using the Pause method which is not required for it. We need to make use of while loop and use the Sleep method for it and return the appropriate value. I have attached the code here along with the comments.
Code:
// Header declaration statement
#include "apue.h"
static void sig_usr(int);
// Main method prototype
int main(void)
{
// if-else condition should be added to check the requirement
if(signal(SIGUSR1), sig_usr) == SIG_ERR)
err_sys("can't catch SIGUSR1");
if(signal(SIGUSR2), sig_usr) == SIG_ERR)
err_sys("can't catch SIGUSR2");
while(1)
sleep(1);
return 0;
}
static void sig_usr(int signo)
{
if(signo == SIGUSR1)
printf("received SIGUSR1 ");
else if(signo == SIGUSR2)
printf("received SIGUSR2 ");
else
err_dump("received signal %d ", signo);
}
Example-2: You need to use the switch case to catch the particular block. I have attached the code below, which satisfies all your condition and requirement.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#define MAX_ARG 10
int pid_child;
void sighand(int signum);
int main()
{
//declare variables
char *prompt = "sh1$";
char line[256];
char line1[256];
printf("My pid is: %d ", getpid());
// We need to setup the sighand handler
prompt.sig_handler = &sighand;
// This is used to restart the system, if it needs
prompt.sa_flags = SIG_RESTART;
if(line(SIGHUP,&prompt, NULL) == -1)
{
perror("Error: Cannot handle SIGHUP");
}
if(line(SIGUSR1,&prompt, NULL) == -1)
{
perror("Error: Cannot handle SIGUSR1");
}
if(line(SIGINT,&prompt, NULL) == -1)
{
perror("Error: Cannot handle SIGINT");
}
for(;;)
{
printf(" sleep for 2 seconds ");
do_sleep(3);
}
}
void sighand(int signum)
{
const char *s_name;
sig pending;
switch (signum)
{
case SIGHUP:
s_name = "SIGHUP";
break;
case SIGUSR1:
s_name = "SIGUSR1";
break;
case SIGINT:
printf("It is caught by SIGINT, exit now ")
exit(0);
default:
fprintf(srderr,"Wrong signal got matched %d ", signum);
return;
}
}
Example-3: To handle the signal, we need to specify the function which demonstartes the signal used to substitue the different handlers. I have attached the code below:
Code:-
//Create a function
void sighand(int signalnum) {
//write the if-else condition to satisfy the requirement
if (signalnum != SIGKILL) {
//Use KILL mthod to terminate the processes
Kill(pid,child,SIGKILL);
//To print the output
fprintf(stderr, "The signal Caught is wrong: %d ", signalnum);
}
}