Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Выполнил: Багаев Даниил Алексеевич, 0374
- * Задание: (Т1) Приложение Linux: "Обеспечение таймаута"
- * Дата: 12-02-2024
- * Версия: 0.1
- *
- * Скрипт для компиляции и запуска:
- * gcc T1_Bagaev_0374.c && ./a.out
- * -----------------------------------------------------
- *
- * В программном коде было реализовано циклическая система управления, с периодом
- * time=3c. Она измеряет состояние объекта, выполняет действия по принятию
- * решения и формируют управляющие воздействия. Все эти действия реализуются
- * некоторой функцией doControl(). Функция doControl() вызывается родительском
- * процессе, а поимка сигнала реализована в дочернем процессе. Для «мягкого»
- * deadline взято время – 0.52 секунд (расчетное время работы функции
- * doControl() + 4%), а для «жесткого» deadline взято время – 1 секунда
- *
- */
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <time.h>
- #include <unistd.h>
- pid_t parent_pid, child_pid;
- const unsigned int work_time_control = 500 * 1000;
- void doControl()
- {
- const int x = random() % 10;
- if (!x) {
- while (1) {}
- }
- srandom(time(NULL));
- const long int dt = random() % 40000;
- const long int t = work_time_control + dt;
- printf("######## %ld \n", t);
- usleep(t);
- }
- void deadlineHandler(int signum, siginfo_t *info, void *context)
- {
- printf("######### deadline exceeded #########\n");
- }
- void alarmHandler()
- {
- printf("##### Restart required! #####\n");
- kill(child_pid, SIGUSR1);
- exit(EXIT_FAILURE);
- }
- int main(int argc, char** argv)
- {
- signal(SIGALRM, alarmHandler);
- const int deadline_signal = SIGRTMIN + 1;
- const unsigned int hard_dt = 1;
- const unsigned int soft_dt = work_time_control * 1.04;
- const unsigned frequency = 3 * 1000 * 1000;
- struct timeval stop, start;
- parent_pid = getpid();
- child_pid = fork();
- while(1) {
- sigset_t set;
- sigemptyset(&set);
- sigaddset(&set, deadline_signal);
- sigprocmask(SIG_BLOCK, &set, NULL);
- if (getpid() == parent_pid) {
- alarm(hard_dt);
- gettimeofday(&start, NULL);
- doControl();
- gettimeofday(&stop, NULL);
- long cDelta_us = (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec;
- if (cDelta_us > soft_dt) {
- siginfo_t info;
- info.si_value.sival_int = 100;
- sigqueue(child_pid, deadline_signal, info.si_value);
- }
- alarm(0);
- usleep(frequency - cDelta_us);
- } else {
- struct sigaction deadline_act;
- deadline_act.sa_sigaction = &deadlineHandler;
- deadline_act.sa_flags = SA_SIGINFO;
- deadline_act.sa_mask = set;
- sigaction(deadline_signal, &deadline_act, NULL);
- sigprocmask(SIG_UNBLOCK, &set, NULL);
- usleep(2900000);
- }
- }
- return 0;
- }
- /*
- *
- *
- * это мне повезло, он быстро закончился но бывает гораздо дольше
- ######## 896652
- ######### deadline exceeded #########
- ######## 875372
- ######### deadline exceeded #########
- ######## 504542
- ######## 665978
- ######### deadline exceeded #########
- ######## 1017797
- ##### Restart required! #####
- *
- *
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement