diff --git a/PROJECT/func.c b/PROJECT/func.c index 8476242..afee160 100644 --- a/PROJECT/func.c +++ b/PROJECT/func.c @@ -1,7 +1,8 @@ #include #include -#include #include +#include +#include double f(double x) { @@ -20,8 +21,18 @@ void find_zero(double *pa, double *pb, double eps, double *px) void ctrlc_handler(int signum) { - printf("\nПолучен сигнал Ctrl+C\n"); - printf("Продолжить поиск корня? (C - продолжить, A - закончить работу программы, R - начать поиск на другом отрезке): "); + int sig; + sigset_t *set = (sigset_t *)signum; + + while (1) + { + sigwait(set, &sig); + if (sig == SIGINT) + { + printf("\nПолучен сигнал Ctrl+C\n"); + printf("Продолжить поиск корня? (C - продолжить, A - закончить работу программы, R - начать поиск на другом отрезке): "); + } + } } // void choose_path(char *choice); // void new_row(double *pa, double *pb); \ No newline at end of file diff --git a/PROJECT/main.c b/PROJECT/main.c index 8a03173..e1ac274 100644 --- a/PROJECT/main.c +++ b/PROJECT/main.c @@ -1,6 +1,7 @@ #include #include #include +#include double f(double x); // function x void find_zero(double *pa, double *pb, double eps, double *px); // @@ -12,25 +13,38 @@ int main() { double a, b, eps, x, *pa = &a, *pb = &b, *px = &x; char choice = ' ', *ch = &choice; + pthread_t thread; sigset_t set; - int sig; printf("Введите интервал [a, b] и точность eps: "); scanf("%lf %lf %lf", pa, pb, &eps); - // if (a > b || ) - - find_zero(pa, pb, eps, px); - // Создание набора сигналов и добавление в него SIGINT sigemptyset(&set); sigaddset(&set, SIGINT); - // Блокировка SIGINT - sigprocmask(SIG_BLOCK, &set, NULL); + // Блокировка сигналов в наборе для основного потока + if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) + { + perror("pthread_sigmask"); + return 1; + } + + // Создание потока для обработчика сигнала + if (pthread_create(&thread, NULL, ctrlc_handler, &set) != 0) + { + perror("pthread_create"); + return 1; + } + + printf("Ожидание сигнала Ctrl+C...\n"); + + find_zero(pa, pb, eps, px); + while (1) + { + sleep(1); + } - // Ожидание получения сигнала Ctrl+C с помощью sigwait - sigwait(&set, &sig); signal(SIGINT, ctrlc_handler); printf("Корень уравнения: %lf\n", *px); diff --git a/PROJECT/out_p/prog2 b/PROJECT/out_p/prog2 index af8efe0..0dc01f3 100755 Binary files a/PROJECT/out_p/prog2 and b/PROJECT/out_p/prog2 differ diff --git a/PROJECT/out_p/prog3 b/PROJECT/out_p/prog3 new file mode 100755 index 0000000..0dc01f3 Binary files /dev/null and b/PROJECT/out_p/prog3 differ