Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b39c080d05 | |||
| 38752470c3 |
12
OSs/lab4/part-1/Makefile
Normal file
12
OSs/lab4/part-1/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
|
||||||
BIN
OSs/lab4/part-1/main
Executable file
BIN
OSs/lab4/part-1/main
Executable file
Binary file not shown.
99
OSs/lab4/part-1/main.c
Normal file
99
OSs/lab4/part-1/main.c
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
enum events{
|
||||||
|
EVENT_VICTIM_THREAD_READY,
|
||||||
|
EVENT_PRINT_BUFFER_UPDATED,
|
||||||
|
EVENTS_NR
|
||||||
|
};
|
||||||
|
|
||||||
|
enum threads_roles {
|
||||||
|
ROLE_VICTIM,
|
||||||
|
ROLE_KILLER,
|
||||||
|
ROLE_PRINTER,
|
||||||
|
ROLES_NR
|
||||||
|
};
|
||||||
|
|
||||||
|
#define ERR_PTR(ret) ((void*)(long)ret)
|
||||||
|
#define PRINTER_TIMEOUT_SECS 2
|
||||||
|
|
||||||
|
#define PRINT_BUFF_SIZE 256
|
||||||
|
static sem_t sems[EVENTS_NR];
|
||||||
|
static pthread_t threads[ROLES_NR];
|
||||||
|
static char print_buffer[PRINT_BUFF_SIZE];
|
||||||
|
static volatile bool run = true;
|
||||||
|
|
||||||
|
void sig_handler(int signal)
|
||||||
|
{
|
||||||
|
snprintf(print_buffer, PRINT_BUFF_SIZE, "signal %d recieved", signal);
|
||||||
|
pthread_exit(ERR_PTR(sem_post(&sems[EVENT_PRINT_BUFFER_UPDATED])));
|
||||||
|
}
|
||||||
|
|
||||||
|
void *killer_thread(void* _)
|
||||||
|
{
|
||||||
|
sigset_t ss;
|
||||||
|
sigemptyset(&ss);
|
||||||
|
sigaddset(&ss, SIGUSR1);
|
||||||
|
if (pthread_sigmask(SIG_BLOCK, &ss, NULL))
|
||||||
|
pthread_exit(ERR_PTR(-EFAULT));
|
||||||
|
int ret = sem_wait(&sems[EVENT_VICTIM_THREAD_READY]);
|
||||||
|
if (ret) pthread_exit(ERR_PTR(ret));
|
||||||
|
pthread_exit(ERR_PTR(pthread_kill(threads[ROLE_VICTIM], SIGUSR1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void *victim_thread(void* _)
|
||||||
|
{
|
||||||
|
int ret = sem_post(&sems[EVENT_VICTIM_THREAD_READY]);
|
||||||
|
if (ret) pthread_exit(ERR_PTR(ret));
|
||||||
|
volatile int x = 0; // for compiler not to optimize infinite loop
|
||||||
|
while (run) {
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *printer_thread(void* _)
|
||||||
|
{
|
||||||
|
sigset_t ss;
|
||||||
|
sigemptyset(&ss);
|
||||||
|
sigaddset(&ss, SIGUSR1);
|
||||||
|
if (pthread_sigmask(SIG_BLOCK, &ss, NULL)) pthread_exit(ERR_PTR(-EFAULT));
|
||||||
|
while (run) {
|
||||||
|
struct timespec ts = {0};
|
||||||
|
int ret = timespec_get(&ts, TIME_UTC);
|
||||||
|
if (ret != TIME_UTC) pthread_exit(ERR_PTR(-EFAULT));
|
||||||
|
ts.tv_sec += PRINTER_TIMEOUT_SECS;
|
||||||
|
if (sem_timedwait(&sems[EVENT_PRINT_BUFFER_UPDATED], &ts))
|
||||||
|
pthread_exit(ERR_PTR(-EFAULT));
|
||||||
|
puts(print_buffer);
|
||||||
|
memset(print_buffer, 0, PRINT_BUFF_SIZE);
|
||||||
|
}
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DO_OR_FAIL(cond) if (cond) return EXIT_FAILURE
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct sigaction sa = {
|
||||||
|
.sa_handler = sig_handler,
|
||||||
|
};
|
||||||
|
DO_OR_FAIL(sigaction(SIGUSR1, &sa, NULL));
|
||||||
|
DO_OR_FAIL(sem_init(&sems[EVENT_VICTIM_THREAD_READY], 0, 0));
|
||||||
|
DO_OR_FAIL(sem_init(&sems[EVENT_PRINT_BUFFER_UPDATED], 0, 0));
|
||||||
|
DO_OR_FAIL(pthread_create(&threads[ROLE_KILLER], NULL, killer_thread, NULL));
|
||||||
|
DO_OR_FAIL(pthread_create(&threads[ROLE_PRINTER], NULL, printer_thread, NULL));
|
||||||
|
DO_OR_FAIL(pthread_create(&threads[ROLE_VICTIM], NULL, victim_thread, NULL));
|
||||||
|
|
||||||
|
for (size_t i = 0; i < ROLES_NR; i++) {
|
||||||
|
DO_OR_FAIL(pthread_join(threads[i], NULL));
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
BIN
OSs/lab4/part-1/main.o
Normal file
BIN
OSs/lab4/part-1/main.o
Normal file
Binary file not shown.
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