From 03159fa0f9442515a0ba1ef91d12403c213338b9 Mon Sep 17 00:00:00 2001 From: ElectronixTM Date: Thu, 25 Sep 2025 23:32:22 +0300 Subject: [PATCH] feat: part 2 complete --- OSs/lab4/part-2/Makefile | 12 +++++++++++ OSs/lab4/part-2/main.c | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 OSs/lab4/part-2/Makefile create mode 100644 OSs/lab4/part-2/main.c diff --git a/OSs/lab4/part-2/Makefile b/OSs/lab4/part-2/Makefile new file mode 100644 index 0000000..da8fa15 --- /dev/null +++ b/OSs/lab4/part-2/Makefile @@ -0,0 +1,12 @@ +targets = main +all: $(targets) + +main: main.o + $(CC) -o $@ $^ $(CFLAGS) + +%.o: %.c + $(CC) -c $^ -o $@ $(CFLAGS) + +clean: + rm -f $(targets) + rm -f *.o diff --git a/OSs/lab4/part-2/main.c b/OSs/lab4/part-2/main.c new file mode 100644 index 0000000..e4fd3ab --- /dev/null +++ b/OSs/lab4/part-2/main.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include + +#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; +}