feat: part 2 complete

This commit is contained in:
2025-09-25 23:32:22 +03:00
parent 8a4173f3f7
commit 03159fa0f9
2 changed files with 55 additions and 0 deletions

43
OSs/lab4/part-2/main.c Normal file
View File

@ -0,0 +1,43 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#define SLEEP_TIME 1
volatile int x;
void sigint_handler(int signal)
{
puts("sigint handler");
sleep(SLEEP_TIME);
}
void sigusr1_handler(int signal)
{
puts("sigusr1 handler");
kill(getpid(), SIGINT);
}
#define DO_OR_FAIL(cond) if (cond) return EXIT_FAILURE
int main()
{
struct sigaction sa_int = {
.sa_handler = sigint_handler
};
struct sigaction sa_usr1 = {
.sa_handler = sigusr1_handler
};
struct sigaction old_sa = {0};
DO_OR_FAIL(sigemptyset(&sa_int.sa_mask));
DO_OR_FAIL(sigemptyset(&sa_usr1.sa_mask));
DO_OR_FAIL(sigaddset(&sa_int.sa_mask, SIGINT));
DO_OR_FAIL(sigaddset(&sa_usr1.sa_mask, SIGUSR1));
DO_OR_FAIL(sigaction(SIGINT, &sa_int, &old_sa));
DO_OR_FAIL(sigaction(SIGUSR1, &sa_usr1, NULL));
while (true) {
x++; // чтобы не соптимизировался
}
return EXIT_SUCCESS;
}