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); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.