Answer the following questions related to the functionality of the below C-progr
ID: 3820666 • Letter: A
Question
Answer the following questions related to the functionality of the below C-program that we discussed in class. Assume INPUTLEN to be 100.
main(int ac, char *av[])
{
void inthandler (int);
void quithandler (int);
char input[INPUTLEN];
int nchars;
signal(SIGINT, inthandler);
signal(SIGQUIT, quithandler);
do {
printf(" Type a message ");
nchars = read(0, input, (INPUTLEN - 1));
if ( nchars == -1)
perror("read returned an error");
else {
input[nchars] = '';
printf("You typed: %s", input);
}
}
while(strncmp(input, "quit" , 4) != 0);
}
void inthandler(int s)
{
printf(" Received Signal %d ....waiting ", s);
sleep(2);
printf(" Leaving inthandler ");
}
void quithandler(int s)
{
printf(" Received Signal %d ....waiting ", s);
sleep(3);
printf(" Leaving quithandler ");
}
What happens if SIGQUIT is generated while the process is in SIGINT handler? Explain WHY.
What happens if a second SIGINT is generated while the process is still in SIGINT handler? Or a third SIGINT? Explain WHY.
What happens if a signal is generated while the program is blocking on user input? Explain WHY.
Explanation / Answer
SIGQUIT generated while the process is in SIGINT handler
The SIGQUIT signal is similar to SIGINT. SIGQUIT produces a core dump when it terminates the process, just like a program error signal.
Hence it will create a Core dump and will terminate the process.
Second SIGINT is generated while the process is still in SIGINT handler
When second SIGINT is generated while the process is still in SIGINT handler it will be kept in queue. It will be sent to a different process from queue when SIGINT is in invoked.
Signal is generated while the program is blocking on user input
nchars = read(0, input, (INPUTLEN - 1));
if ( nchars == -1)
perror("read returned an error");
-->So as per the above code it will print the error "read returned an error".
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.