added prog3

This commit is contained in:
ada-dmitry
2023-11-21 23:07:32 +03:00
parent 8111104a39
commit 53b1c06aa9
4 changed files with 37 additions and 12 deletions
+12 -1
View File
@@ -1,7 +1,8 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
double f(double x)
{
@@ -19,9 +20,19 @@ void find_zero(double *pa, double *pb, double eps, double *px)
}
void ctrlc_handler(int signum)
{
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);
+23 -9
View File
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
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);
Binary file not shown.
BIN
View File
Binary file not shown.