Compute this code in C, please use \'setjmp\' and \'longjmp\' Write a C code as
ID: 3888012 • Letter: C
Question
Compute this code in C, please use 'setjmp' and 'longjmp'
Write a C code as described below. I am trying to print out a statement Good Morning from function 1 (funci) and Good Bye! from function 2 (func2) in the program. However, due to the infinite loop that I made by mistake in the program func1, 1 can not see the second statement "Good Bye". The internupt handler that I added to deal with the signal control+to kill the foreground process stops the infinite loop but couldn't see Good Bye!". The program and the output after executing the program are as follows. Program finclude csignal.h void inthandler printfl This is an interrupt handlerin exit EXIT SUCCESS) void funcIO while( 1){ /* infinite loop made by mistake'/ sleep(1) void func20 printfl Good Byen) exit EXIT SUCCESS int main(int argc, char "argv[]) { signal calls interrupt handler (intHandler) when control+ 'c's typed signal(SIGINT, intHandler) exit EXIT SUCCESS) Good Morning Looping CThis is an interrupt handler Modify the above program so that it can print out "Good Bye!" as spedified at func2 even though the program is badly written with the infinite loop statement The Output should like Good Morning Looping Looping. CThis is an interrupt handler Good Bye!
Explanation / Answer
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "tlpi_hdr.h"
#include <setjmp.h>
jmp_buf bufferA;
void intHandler() {
printf("This is an interrupt handler ");
longjmp(bufferA, 1);
exit(EXIT_SUCCESS);
}
void func1() {
printf("Good Morning ");
while (1) {
printf("Looping... ");
sleep(1);
}
}
void func2() {
printf("Good Bye ");
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
signal(SIGINT, intHandler);
if (!setjmp(bufferA)) {
func1();
func2();
}
else {
func2();
}
exit(EXIT_SUCCESS);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.