feat: part 2 complete
This commit is contained in:
12
OSs/lab4/part-2/Makefile
Normal file
12
OSs/lab4/part-2/Makefile
Normal file
@ -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
|
||||||
43
OSs/lab4/part-2/main.c
Normal file
43
OSs/lab4/part-2/main.c
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user