Last active
October 10, 2022 20:13
-
-
Save dulimarta/1eac89bdd1f660ebc63813a451fd986c to your computer and use it in GitHub Desktop.
CS452 Lab3 - Interrupted Calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <signal.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <stdbool.h> | |
int number = 0; | |
bool running = true; | |
void do_nothing(int s) { | |
if (number < 0) { | |
running = false; | |
} | |
printf ("Inside signal handler %d %d\n", running, number); | |
} | |
void skip_input() { | |
printf ("Skipping invalid input...\n"); | |
int c; | |
do { | |
c = getchar(); | |
} while (c != EOF && c != '\n'); | |
// c is EOF or newline | |
} | |
int main() { | |
// Using older API | |
// signal(SIGINT, do_nothing); | |
// Using new API | |
// struct sigaction z; | |
// sigemptyset(&z.sa_mask); | |
// z.sa_handler = do_nothing; | |
// sigaction(SIGINT, &z, NULL); | |
number = 47; | |
do { | |
printf ("Enter a number: "); | |
errno = 0; | |
int how_many_items = scanf("%d", &number); | |
if (errno == EINTR) { | |
perror("What?"); | |
printf ("I did not get your input. Last value was %d\n", number); | |
} else { | |
if (how_many_items == 0) { | |
skip_input(); | |
} | |
printf ("You entered %d\n", number); | |
} | |
} while (number != 0 && running); | |
printf ("Out of the loop\n"); | |
// pause(); | |
printf ("\nEnd of program\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment