#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
static volatile bool *termination_flag_ptr = NULL;
static void trap_ctrl_c(int sig)
{
if (termination_flag_ptr != NULL)
{
*termination_flag_ptr = true;
}
}
static void calculate_something(void)
{
volatile bool stop = false;
// Регистрируем.
termination_flag_ptr = &stop;
unsigned long long int n = 0;
printf("Calculating, press Ctrl+C to stop...\n"); while (!stop)
{
// Делаем какие-нибудь тяжелые вычисления.
++n;
}
printf("Calculation was terminated at %llu\n", n
); }
int main(void)
{
signal(SIGINT, trap_ctrl_c);
calculate_something();
}
I2luY2x1ZGUgPHNpZ25hbC5oPgojaW5jbHVkZSA8c3RkYm9vbC5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCnN0YXRpYyB2b2xhdGlsZSBib29sICp0ZXJtaW5hdGlvbl9mbGFnX3B0ciA9IE5VTEw7CgpzdGF0aWMgdm9pZCB0cmFwX2N0cmxfYyhpbnQgc2lnKQp7CiAgICBpZiAodGVybWluYXRpb25fZmxhZ19wdHIgIT0gTlVMTCkKICAgIHsKICAgICAgICAqdGVybWluYXRpb25fZmxhZ19wdHIgPSB0cnVlOwogICAgfQp9CgpzdGF0aWMgdm9pZCBjYWxjdWxhdGVfc29tZXRoaW5nKHZvaWQpCnsKICAgIHZvbGF0aWxlIGJvb2wgc3RvcCA9IGZhbHNlOwogICAgLy8g0KDQtdCz0LjRgdGC0YDQuNGA0YPQtdC8LgogICAgdGVybWluYXRpb25fZmxhZ19wdHIgPSAmc3RvcDsKICAgIAogICAgdW5zaWduZWQgbG9uZyBsb25nIGludCBuID0gMDsKICAgIHByaW50ZigiQ2FsY3VsYXRpbmcsIHByZXNzIEN0cmwrQyB0byBzdG9wLi4uXG4iKTsKICAgIHdoaWxlICghc3RvcCkKICAgIHsKICAgICAgICAvLyDQlNC10LvQsNC10Lwg0LrQsNC60LjQtS3QvdC40LHRg9C00Ywg0YLRj9C20LXQu9GL0LUg0LLRi9GH0LjRgdC70LXQvdC40Y8uCiAgICAgICAgKytuOwogICAgfQogICAgCiAgICBwcmludGYoIkNhbGN1bGF0aW9uIHdhcyB0ZXJtaW5hdGVkIGF0ICVsbHVcbiIsIG4pOwp9CgoKaW50IG1haW4odm9pZCkKewogICAgc2lnbmFsKFNJR0lOVCwgdHJhcF9jdHJsX2MpOwogICAgY2FsY3VsYXRlX3NvbWV0aGluZygpOwp9Cg==