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

Modify HW05 to use signals to pause the output of the timer instead of control c

ID: 3795029 • Letter: M

Question

Modify HW05 to use signals to pause the output of the timer instead of control characters. So, the only terminal setting you will need to modify programatically is echoing. If the user hits ctrl-C, the program pauses. If they hit it again, it unpauses. Ctrl- terminates and restores the settings gracefully.

Hints :

Spinlocks :

while( pause == true)
{}

If pause is set to true, then this while loop will iterate. When pause gets set to false, this loop gets skipped.

See the attached code for a spinlock example, spinlock.cpp

Spinlock.cpp

HW5

Explanation / Answer

#include #include #include int main(int ac, char* args[]) { struct termios old_settings, new_settings; // get the terminal settings tcgetattr(0, &old_settings); tcgetattr(0, &new_settings); // update them // turn off echoing new_settings.c_lflag &= ~ECHO; // change VSTART to 'u' new_settings.c_cc[VSTART] = 'u'; // change VSTOP to 'p' new_settings.c_cc[VSTOP] = 'p'; // send them back tcsetattr(0, TCSANOW, &new_settings); // do the countdown thing int seconds = atoi(args[1]); while(seconds >= 0) { printf("%i ", seconds); sleep(1); seconds--; } // restore original settings tcsetattr(0, TCSANOW, &old_settings); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote