Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bbbf39720b | |||
| 2bfad7f836 |
55
.gitignore
vendored
Normal file
55
.gitignore
vendored
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Object files
|
||||||
|
*.o
|
||||||
|
*.ko
|
||||||
|
*.obj
|
||||||
|
*.elf
|
||||||
|
|
||||||
|
# Linker output
|
||||||
|
*.ilk
|
||||||
|
*.map
|
||||||
|
*.exp
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Libraries
|
||||||
|
*.lib
|
||||||
|
*.a
|
||||||
|
*.la
|
||||||
|
*.lo
|
||||||
|
|
||||||
|
# Shared objects (inc. Windows DLLs)
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.so.*
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
*.i*86
|
||||||
|
*.x86_64
|
||||||
|
*.hex
|
||||||
|
|
||||||
|
# Debug files
|
||||||
|
*.dSYM/
|
||||||
|
*.su
|
||||||
|
*.idb
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# Kernel Module Compile Results
|
||||||
|
*.mod*
|
||||||
|
*.cmd
|
||||||
|
.tmp_versions/
|
||||||
|
modules.order
|
||||||
|
Module.symvers
|
||||||
|
Mkfile.old
|
||||||
|
dkms.conf
|
||||||
|
|
||||||
|
# debug information files
|
||||||
|
*.dwo
|
||||||
1
OSs/lab3/README.txt
Normal file
1
OSs/lab3/README.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Здесь я не буду оформлять первый уровень, потому что он фактически выглядит как копипаста. Возможно я потом докину его
|
||||||
@ -1,7 +1,7 @@
|
|||||||
targets = main
|
targets = main
|
||||||
all: $(targets)
|
all: $(targets)
|
||||||
|
|
||||||
main: main.o
|
main: main.o buff.o
|
||||||
$(CC) -o $@ $^ $(CFLAGS)
|
$(CC) -o $@ $^ $(CFLAGS)
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
5
OSs/lab3/level-2/README.txt
Normal file
5
OSs/lab3/level-2/README.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Во продвинутом уровне задания необходимо было реализовать паттерн producer-consumer. Заводится буффер определенного размера, а также потоки, которые в него что-то кладут и потоки, которые из него что-то берут.
|
||||||
|
|
||||||
|
Внутри buff.c сокрыта вся магия по синхронизации буффера, постановке на него предметов и прочего. В main же только создаются потоки. Я решил, что producer будет создавать "ключи", а consumer будет их печатать
|
||||||
|
|
||||||
|
Чтобы не решать проблемы с зачисткой буффера в конце, семафоры поставлены на таймаут, по истечении которого поток вылетит
|
||||||
142
OSs/lab3/level-2/buff.c
Normal file
142
OSs/lab3/level-2/buff.c
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "buff.h"
|
||||||
|
|
||||||
|
#define NBUFF 32
|
||||||
|
#define TIMEOUT_SECS 5
|
||||||
|
#define TIMEOUT_NSECS 0
|
||||||
|
|
||||||
|
#define ARR_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||||
|
|
||||||
|
static void __zero_msg(struct message *msg)
|
||||||
|
{
|
||||||
|
memset(msg->msg, 0, sizeof(msg->msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void __copy_msg(struct message *dest, const struct message *src)
|
||||||
|
{
|
||||||
|
memcpy(dest->msg, src->msg, sizeof(src->msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct {
|
||||||
|
pthread_mutex_t msgs_lock;
|
||||||
|
sem_t nstored;
|
||||||
|
sem_t nempty;
|
||||||
|
struct message msgs[NBUFF];
|
||||||
|
size_t cidx; //< consumer offset
|
||||||
|
size_t pidx; //< producer offset
|
||||||
|
} buf = {0};
|
||||||
|
|
||||||
|
int buf_init(void)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
ret = sem_init(&buf.nstored, 0, 0);
|
||||||
|
if (ret) {
|
||||||
|
ret = -EFAULT;
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
ret = sem_init(&buf.nempty, 0, ARR_SIZE(buf.msgs));
|
||||||
|
if (ret) {
|
||||||
|
ret = -EFAULT;
|
||||||
|
goto destroy_nstored;
|
||||||
|
}
|
||||||
|
ret = pthread_mutex_init(&buf.msgs_lock, NULL);
|
||||||
|
if (ret) {
|
||||||
|
goto destroy_nempty;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
destroy_nempty:
|
||||||
|
sem_destroy(&buf.nempty);
|
||||||
|
destroy_nstored:
|
||||||
|
sem_destroy(&buf.nstored);
|
||||||
|
error:
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
|
||||||
|
int buf_deinit(void)
|
||||||
|
{
|
||||||
|
int ret = 0, tmp_ret;
|
||||||
|
if ((tmp_ret = pthread_mutex_destroy(&buf.msgs_lock))) ret = tmp_ret;
|
||||||
|
if ((tmp_ret = sem_destroy(&buf.nempty))) ret = tmp_ret;
|
||||||
|
if ((tmp_ret = sem_destroy(&buf.nstored))) ret = tmp_ret;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define __buf_wrap_idx(idx) (idx % ARR_SIZE(buf.msgs))
|
||||||
|
#define __buf_consume_ptr (&buf.msgs[__buf_wrap_idx(buf.cidx)])
|
||||||
|
#define __buf_produce_ptr (&buf.msgs[__buf_wrap_idx(buf.pidx)])
|
||||||
|
|
||||||
|
static int __timeout_to_abs(struct timespec *ts, time_t secs, ssize_t nsec)
|
||||||
|
{
|
||||||
|
int ret = timespec_get(ts, TIME_UTC);
|
||||||
|
if (ret != TIME_UTC) return -EFAULT;
|
||||||
|
ts->tv_sec += secs;
|
||||||
|
ts->tv_nsec += nsec;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int __sem_timedwait_rel(sem_t *sem, time_t secs, ssize_t nsecs)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
struct timespec sem_timeout;
|
||||||
|
ret = __timeout_to_abs(&sem_timeout, secs, nsecs);
|
||||||
|
if (ret) return ret;
|
||||||
|
return sem_timedwait(sem, &sem_timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int __buf_consume(struct message *dest)
|
||||||
|
{
|
||||||
|
int ret = pthread_mutex_lock(&buf.msgs_lock);
|
||||||
|
if (ret) {
|
||||||
|
return -EFAULT;
|
||||||
|
}
|
||||||
|
__copy_msg(dest, __buf_consume_ptr);
|
||||||
|
__zero_msg(__buf_consume_ptr);
|
||||||
|
buf.cidx++;
|
||||||
|
// Осозннано игнорируем ошибки
|
||||||
|
ret = pthread_mutex_unlock(&buf.msgs_lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int buf_consume(struct message *dest)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
int semval = 0;
|
||||||
|
ret = __sem_timedwait_rel(&buf.nstored, TIMEOUT_SECS, TIMEOUT_NSECS);
|
||||||
|
if (ret) return ret;
|
||||||
|
|
||||||
|
ret = __buf_consume(dest);
|
||||||
|
if (ret) return ret;
|
||||||
|
|
||||||
|
ret = sem_post(&buf.nempty);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int __buf_push(const struct message *src)
|
||||||
|
{
|
||||||
|
int ret = pthread_mutex_lock(&buf.msgs_lock);
|
||||||
|
if (ret) {
|
||||||
|
return -EFAULT;
|
||||||
|
}
|
||||||
|
__copy_msg(__buf_produce_ptr, src);
|
||||||
|
buf.pidx++;
|
||||||
|
ret = pthread_mutex_unlock(&buf.msgs_lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int buf_push(const struct message *src)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
ret = __sem_timedwait_rel(&buf.nempty, TIMEOUT_SECS, TIMEOUT_NSECS);
|
||||||
|
if (ret) return ret;
|
||||||
|
|
||||||
|
ret = __buf_push(src);
|
||||||
|
if (ret) return ret;
|
||||||
|
|
||||||
|
return sem_post(&buf.nstored);
|
||||||
|
}
|
||||||
16
OSs/lab3/level-2/buff.h
Normal file
16
OSs/lab3/level-2/buff.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef BUFF_H_
|
||||||
|
#define BUFF_H_
|
||||||
|
|
||||||
|
#define MSG_MAX_LEN 256
|
||||||
|
|
||||||
|
struct message {
|
||||||
|
char msg[MSG_MAX_LEN];
|
||||||
|
};
|
||||||
|
|
||||||
|
int buf_init(void);
|
||||||
|
int buf_deinit(void);
|
||||||
|
|
||||||
|
int buf_consume(struct message *dest);
|
||||||
|
int buf_push(const struct message *src);
|
||||||
|
|
||||||
|
#endif
|
||||||
105
OSs/lab3/level-2/main.c
Normal file
105
OSs/lab3/level-2/main.c
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "buff.h"
|
||||||
|
|
||||||
|
#define ERR_PTR ((void *)1)
|
||||||
|
#define VOID_CAST(num) ((void *) num)
|
||||||
|
|
||||||
|
#define N_PRODUCERS 16
|
||||||
|
#define N_CONSUMERS 1
|
||||||
|
#define KEY_LEN 32 // len in chars
|
||||||
|
|
||||||
|
const char key_syms[] = "abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"
|
||||||
|
"1234567890";
|
||||||
|
|
||||||
|
#define ALPHABET_SIZE (sizeof(key_syms) - 1)
|
||||||
|
|
||||||
|
pthread_t consumers[N_CONSUMERS] = {0};
|
||||||
|
pthread_t producers[N_PRODUCERS] = {0};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Создает "ключи" - наборы "случайных" символов из
|
||||||
|
* списка допустимых
|
||||||
|
*/
|
||||||
|
void get_key(char key_dest[KEY_LEN])
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < KEY_LEN; i++) {
|
||||||
|
size_t sym_idx = rand() % ALPHABET_SIZE;
|
||||||
|
key_dest[i] = key_syms[sym_idx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
volatile bool run = true;
|
||||||
|
|
||||||
|
void *producer_routine(void *arg)
|
||||||
|
{
|
||||||
|
size_t pidx = (size_t) arg;
|
||||||
|
struct message msg = {0};
|
||||||
|
while (run) {
|
||||||
|
get_key(msg.msg);
|
||||||
|
if (buf_push(&msg)) pthread_exit(ERR_PTR);
|
||||||
|
printf("[p%lu] pushed new key\n", pidx);
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *consumer_routine(void *arg)
|
||||||
|
{
|
||||||
|
size_t cidx = (size_t) arg;
|
||||||
|
struct message msg = {0};
|
||||||
|
while (run) {
|
||||||
|
if (buf_consume(&msg)) pthread_exit(ERR_PTR);
|
||||||
|
printf("[c%lu] obtained key %s\n", cidx, msg.msg);
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
if (buf_init()) {
|
||||||
|
printf("failed to init work buffer\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < N_PRODUCERS; i++) {
|
||||||
|
if (pthread_create(&producers[i], NULL,
|
||||||
|
producer_routine, VOID_CAST(i))) {
|
||||||
|
printf("Failed to create enough consumer threads\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < N_CONSUMERS; i++) {
|
||||||
|
if (pthread_create(&consumers[i], NULL,
|
||||||
|
consumer_routine, VOID_CAST(i))) {
|
||||||
|
printf("Failed to create enough consumer threads\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep(10);
|
||||||
|
run = false;
|
||||||
|
|
||||||
|
void *ret = NULL;
|
||||||
|
for (size_t i = 0; i < N_PRODUCERS; i++) {
|
||||||
|
if (pthread_join(producers[i], &ret)) {
|
||||||
|
printf("failed to join producer");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < N_CONSUMERS; i++) {
|
||||||
|
if (pthread_join(consumers[i], &ret)) {
|
||||||
|
printf("failed to join consumer");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// не используем разделяемые ресурсы, так что код возврата
|
||||||
|
// можно игнорировать, linux справится
|
||||||
|
buf_deinit();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
Binary file not shown.
@ -1,99 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
Binary file not shown.
@ -1,12 +0,0 @@
|
|||||||
targets = main
|
|
||||||
all: $(targets)
|
|
||||||
|
|
||||||
main: main.o
|
|
||||||
$(CC) -o $@ $^ $(CFLAGS)
|
|
||||||
|
|
||||||
%.o: %.c
|
|
||||||
$(CC) -c $^ -o $@ $(CFLAGS)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f $(targets)
|
|
||||||
rm -f *.o
|
|
||||||
@ -1,43 +0,0 @@
|
|||||||
#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