2 Commits

Author SHA1 Message Date
b39c080d05 feat: part 2 complete 2025-09-25 23:32:22 +03:00
38752470c3 feat: part 1 complete 2025-09-25 23:12:44 +03:00
10 changed files with 166 additions and 107 deletions

2
DBs/.gitignore vendored
View File

@ -1,2 +0,0 @@
*.pdf

View File

@ -1,23 +0,0 @@
# Базы данных
Тут будут лежать лабы по PostgreSQL.
## Структура
В корне есть файл `main.typ`. Его можно скомпилировать с помощью
```
typst c main.typ
```
Внутри него есть директивы включения файлов с лабами.
В каждой директории `^lab\d+\/$` есть как минимум один файл `main.typ`,
которые и включаются в корневой `main.typ`. Это позволяет формировать структурированный отчет
для курсовика.
## Окружение
- Я использую `docker.io/library/postgres:16.10-alpine3.2` в контейнере `podman`
- Для работы с БД - `pgcli`

View File

@ -1,72 +0,0 @@
= Работа №1. Создание базы данных.
== Смена пароля
```sql
ALTER USER surimi WITH PASSWORD 'trio';
```
== Создание таблиц
```sql
-- create
CREATE TABLE auto_personnel (
id serial primary key,
first_name varchar(255),
last_name varchar(255),
father_name varchar(255)
);
COMMENT ON TABLE auto_personnel IS 'Сотрудники автопарка';
CREATE TABLE auto (
id serial primary key,
num varchar(255),
color varchar(255),
mark varchar(255)
);
COMMENT ON TABLE auto IS 'Автомобили автопарка';
CREATE TABLE routes (
id serial primary key,
name varchar(255)
);
COMMENT ON TABLE routes IS 'Маршруты';
CREATE TABLE journal (
id serial primary key,
time_out timestamp with time zone,
time_in timestamp with time zone
);
COMMENT ON TABLE journal IS 'Журнал оператора';
```
== Создание связей
```sql
ALTER TABLE journal ADD COLUMN route_id INTEGER NOT NULL CONSTRAINT fk_journal_routes REFERENCES routes(id);
ALTER TABLE journal ADD COLUMN auto_id INTEGER NOT NULL CONSTRAINT fk_journal_auto REFERENCES auto(id);
ALTER TABLE auto ADD COLUMN personnel_id INTEGER NOT NULL CONSTRAINT fk_auto_auto_personnel REFERENCES auto_personnel(id);
```
== Удаление таблиц
```sql
-- drop
DROP TABLE auto_personnel CASCADE;
DROP TABLE auto CASCADE;
DROP TABLE routes CASCADE;
DROP TABLE journal CASCADE;
```
== Бекап
```sh
podman exec postgresinstance-postgres-1 pg_dump postgres -U surimi > dump.sql
```
== Восстановление
```sh
podman exec -i postgresinstance-postgres-1 psql -U surimi -d postgres < dump.sql
```

View File

@ -1,10 +0,0 @@
#import "@preview/ilm:1.4.1": *
#set text(lang: "ru")
#show: ilm.with(
title: [Базы данных],
author: "Марк Железняков",
)
#include "lab1/main.typ"

12
OSs/lab4/part-1/Makefile Normal file
View 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

Binary file not shown.

99
OSs/lab4/part-1/main.c Normal file
View 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

Binary file not shown.

12
OSs/lab4/part-2/Makefile Normal file
View 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
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;
}