Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b39c080d05 | |||
| 38752470c3 |
55
.gitignore
vendored
55
.gitignore
vendored
@ -1,55 +0,0 @@
|
||||
# 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,22 +0,0 @@
|
||||
all: dynamic static
|
||||
|
||||
dynamic: main.o lib.so
|
||||
gcc $^ -o $@ $(CFLAGS) -lm
|
||||
|
||||
static: main.o lib.a
|
||||
gcc $^ -o $@ $(CFLAGS) -lm
|
||||
|
||||
lib.so: lib.o
|
||||
gcc -shared $^ -o $@ $(CFLAGS)
|
||||
|
||||
lib.a: lib.o
|
||||
ar r $@ $^
|
||||
|
||||
main.o: main.c
|
||||
gcc -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
lib.o: lib.c
|
||||
gcc -c -o $@ -fPIC $^ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f dynamic static *.so *.o
|
||||
@ -1,45 +0,0 @@
|
||||
#include "lib.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#define ZEROIN_MAX_ITERATIONS 1000
|
||||
|
||||
size_t fibo(size_t n)
|
||||
{
|
||||
size_t prev = 0, cur = 1, tmp = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
tmp = cur;
|
||||
cur = cur + prev;
|
||||
prev = tmp;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
double zeroin(double (*f)(double), double err, double a, double b)
|
||||
{
|
||||
double left = f(a);
|
||||
if (fabs(left) < err) return left;
|
||||
double right = f(b);
|
||||
if (fabs(right) < err) return right;
|
||||
if (left * right > 0) {
|
||||
return NAN;
|
||||
}
|
||||
|
||||
double x = NAN;
|
||||
double cur = NAN;
|
||||
size_t n = 0;
|
||||
do {
|
||||
x = (a + b) / 2;
|
||||
cur = f(x);
|
||||
if (cur * left > 0) {
|
||||
left = cur;
|
||||
a = x;
|
||||
}
|
||||
else {
|
||||
right = cur;
|
||||
b = x;
|
||||
}
|
||||
n++;
|
||||
} while (fabs(cur) > err || n < ZEROIN_MAX_ITERATIONS);
|
||||
return x;
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
#ifndef LIB_H_
|
||||
#define LIB_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* Это будет просто какая-то не очень умная библиотечка с двумя функциями,
|
||||
* просто чтобы их типа собрать
|
||||
*/
|
||||
|
||||
/**
|
||||
* Считает n-ное число фибоначи
|
||||
*/
|
||||
size_t fibo(size_t n);
|
||||
|
||||
/**
|
||||
* Считает ноль функции на интервале с указанной погрешностью
|
||||
*/
|
||||
double zeroin(double (*f)(double), double err, double a, double b);
|
||||
|
||||
#endif
|
||||
@ -1,17 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "lib.h"
|
||||
|
||||
#define FIBO_NUM 15
|
||||
|
||||
#define SIN_A 1.6
|
||||
#define SIN_B 3.7
|
||||
#define ZEROIN_ERR 0.001
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("fibonachi number under index %u: %zu\n", FIBO_NUM, fibo(FIBO_NUM));
|
||||
printf("zero of sin(x) in interval (%lf %lf) is %lf\n",
|
||||
SIN_A, SIN_B, zeroin(sin, ZEROIN_ERR, SIN_A, SIN_B));
|
||||
return 0;
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
Здесь я не буду оформлять первый уровень, потому что он фактически выглядит как копипаста. Возможно я потом докину его
|
||||
@ -1,12 +0,0 @@
|
||||
targets = main
|
||||
all: $(targets)
|
||||
|
||||
main: main.o buff.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(targets)
|
||||
rm -f *.o
|
||||
@ -1,5 +0,0 @@
|
||||
Во продвинутом уровне задания необходимо было реализовать паттерн producer-consumer. Заводится буффер определенного размера, а также потоки, которые в него что-то кладут и потоки, которые из него что-то берут.
|
||||
|
||||
Внутри buff.c сокрыта вся магия по синхронизации буффера, постановке на него предметов и прочего. В main же только создаются потоки. Я решил, что producer будет создавать "ключи", а consumer будет их печатать
|
||||
|
||||
Чтобы не решать проблемы с зачисткой буффера в конце, семафоры поставлены на таймаут, по истечении которого поток вылетит
|
||||
@ -1,142 +0,0 @@
|
||||
#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);
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
#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
|
||||
@ -1,105 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
BIN
OSs/lab4/part-1/main
Executable file
BIN
OSs/lab4/part-1/main
Executable file
Binary file not shown.
BIN
OSs/lab4/part-1/main.o
Normal file
BIN
OSs/lab4/part-1/main.o
Normal file
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,132 +0,0 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define PIPE_READ_IDX 0
|
||||
#define PIPE_WRITE_IDX 1
|
||||
|
||||
int client(int fd_read, int fd_write);
|
||||
int server(int fd_read, int fd_write);
|
||||
|
||||
#define DO_OR_FAIL(cond) if (cond) return EXIT_FAILURE
|
||||
int main()
|
||||
{
|
||||
int pipe_to_server[2], pipe_to_client[2];
|
||||
DO_OR_FAIL(pipe(pipe_to_server) < 0);
|
||||
DO_OR_FAIL(pipe(pipe_to_client) < 0);
|
||||
|
||||
pid_t client_pid;
|
||||
if ((client_pid = fork()) == 0) { // server
|
||||
DO_OR_FAIL(close(pipe_to_server[PIPE_WRITE_IDX]));
|
||||
DO_OR_FAIL(close(pipe_to_client[PIPE_READ_IDX]));
|
||||
|
||||
DO_OR_FAIL(
|
||||
server(
|
||||
pipe_to_server[PIPE_READ_IDX],
|
||||
pipe_to_client[PIPE_WRITE_IDX]
|
||||
)
|
||||
);
|
||||
}
|
||||
// client
|
||||
DO_OR_FAIL(close(pipe_to_server[PIPE_READ_IDX]));
|
||||
DO_OR_FAIL(close(pipe_to_client[PIPE_WRITE_IDX]));
|
||||
|
||||
DO_OR_FAIL(
|
||||
client(
|
||||
pipe_to_client[PIPE_READ_IDX],
|
||||
pipe_to_server[PIPE_WRITE_IDX]
|
||||
)
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *ltrim(char *s)
|
||||
{
|
||||
while(isspace(*s)) s++;
|
||||
return s;
|
||||
}
|
||||
|
||||
char *rtrim(char *s)
|
||||
{
|
||||
char* back = s + strlen(s);
|
||||
while(isspace(*--back));
|
||||
*(back+1) = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
char *trim(char *s)
|
||||
{
|
||||
return rtrim(ltrim(s));
|
||||
}
|
||||
|
||||
#define BUF_SIZE 256
|
||||
|
||||
#define DO_OR_PROPAGATE(call, expected, tmp_var) \
|
||||
if ((tmp_var = (call)) != expected)) return tmp_var
|
||||
|
||||
/**
|
||||
* Performs client actions
|
||||
*
|
||||
* @fd_read - pipe to read from
|
||||
* @fd_write - pipe to write t\no
|
||||
*
|
||||
* returns 0 on success or -errno on fail
|
||||
*/
|
||||
int client(int fd_read, int fd_write)
|
||||
{
|
||||
char buf[BUF_SIZE] = {0};
|
||||
if (!fgets(buf, BUF_SIZE, stdin)) return -EFAULT;
|
||||
const char *s = trim(buf);
|
||||
size_t len = strlen(s);
|
||||
ssize_t n_written = write(fd_write, s, len);
|
||||
if (n_written < 0 || n_written < len) {
|
||||
return -EFAULT;
|
||||
}
|
||||
memset(buf, 0, BUF_SIZE);
|
||||
size_t n;
|
||||
while ((n = read(fd_read, buf, BUF_SIZE)) > 0) {
|
||||
len = strlen(buf);
|
||||
n_written = fwrite(buf, sizeof(buf[0]), strlen(buf), stdout);
|
||||
if (n_written < 0 || n_written < len) {
|
||||
return -EFAULT;
|
||||
}
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs server actions
|
||||
*
|
||||
* @fd_read - pipe to read from
|
||||
* @fd_write - pipe to write to
|
||||
*
|
||||
* returns 0 on success or -errno on fail
|
||||
*/
|
||||
int server(int fd_read, int fd_write)
|
||||
{
|
||||
char buf[BUF_SIZE] = {0};
|
||||
size_t n = read(fd_read, buf, BUF_SIZE);
|
||||
if (n == 0) return -EFAULT;
|
||||
FILE *f = fopen(buf, "r");
|
||||
// не 100% правильно, но наиболее вероятно
|
||||
if (f == NULL) {
|
||||
const char error_msg[] = "Can't open requested file\n";
|
||||
write(fd_write, error_msg, sizeof(error_msg));
|
||||
goto epilog;
|
||||
}
|
||||
|
||||
while ((n = fread(buf, sizeof(buf[0]), BUF_SIZE, f)) > 0) {
|
||||
write(fd_write, buf, n);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
|
||||
epilog:
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
targets = server client
|
||||
all: $(targets)
|
||||
|
||||
server: server.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
client: client.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(targets)
|
||||
rm -f *.o
|
||||
@ -1,80 +0,0 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
int client(int fd_read, int fd_write);
|
||||
|
||||
int main()
|
||||
{
|
||||
int read_fd, write_fd;
|
||||
FAIL_ON((write_fd=open(FIFO_SERVER_PATH, O_WRONLY)) < 0);
|
||||
FAIL_ON((read_fd=open(FIFO_CLIENT_PATH, O_RDONLY)) < 0);
|
||||
|
||||
FAIL_ON(client(read_fd, write_fd));
|
||||
|
||||
FAIL_ON(unlink(FIFO_SERVER_PATH) < 0);
|
||||
printf("unlinked %s\n", FIFO_SERVER_PATH);
|
||||
FAIL_ON(unlink(FIFO_CLIENT_PATH) < 0);
|
||||
printf("unlinked %s\n", FIFO_CLIENT_PATH);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
#define BUF_SIZE 256
|
||||
|
||||
char *ltrim(char *s)
|
||||
{
|
||||
while(isspace(*s)) s++;
|
||||
return s;
|
||||
}
|
||||
|
||||
char *rtrim(char *s)
|
||||
{
|
||||
char* back = s + strlen(s);
|
||||
while(isspace(*--back));
|
||||
*(back+1) = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
char *trim(char *s)
|
||||
{
|
||||
return rtrim(ltrim(s));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs client actions
|
||||
*
|
||||
* @fd_read - pipe to read from
|
||||
* @fd_write - pipe to write t\no
|
||||
*
|
||||
* returns 0 on success or -errno on fail
|
||||
*/
|
||||
int client(int fd_read, int fd_write)
|
||||
{
|
||||
char buf[BUF_SIZE] = {0};
|
||||
if (!fgets(buf, BUF_SIZE, stdin)) return -EFAULT;
|
||||
const char *s = trim(buf);
|
||||
size_t len = strlen(s);
|
||||
ssize_t n_written = write(fd_write, s, len);
|
||||
if (n_written < 0 || n_written < len) {
|
||||
return -EFAULT;
|
||||
}
|
||||
memset(buf, 0, BUF_SIZE);
|
||||
size_t n;
|
||||
struct custom_ds ds;
|
||||
while ((n = recv_msg(fd_read, &ds)) > 0) {
|
||||
len = ds.hdr.len;
|
||||
n_written = fwrite(ds.msg, sizeof(ds.msg[0]), len, stdout);
|
||||
if (n_written < 0 || n_written < len) {
|
||||
return -EFAULT;
|
||||
}
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
#ifndef COMMON_H_
|
||||
#define COMMON_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define FIFO_SERVER_PATH "/tmp/fifo.to_server" // server reads this file
|
||||
#define FIFO_CLIENT_PATH "/tmp/fifo.to_client" // client reads this file
|
||||
|
||||
#define __DATA_STRUCT_MSG_LEN 4096
|
||||
|
||||
enum ds_types {
|
||||
DS_MESSAGE,
|
||||
DS_FAIL
|
||||
};
|
||||
|
||||
struct custom_ds {
|
||||
struct {
|
||||
long len;
|
||||
long type;
|
||||
} hdr;
|
||||
char msg[__DATA_STRUCT_MSG_LEN];
|
||||
};
|
||||
|
||||
static inline ssize_t send_msg(int fd, const struct custom_ds *ds)
|
||||
{
|
||||
return write(fd, ds, sizeof(ds->hdr) + ds->hdr.len);
|
||||
}
|
||||
|
||||
static inline ssize_t recv_msg(int fd, struct custom_ds *reciever)
|
||||
{
|
||||
size_t n_hdr = read(fd, reciever, sizeof(reciever->hdr));
|
||||
if (n_hdr == 0) return 0; // eof reached
|
||||
if (n_hdr != sizeof(reciever->hdr)) return -ENOMSG;
|
||||
if (reciever->hdr.len == 0) return 0;
|
||||
|
||||
size_t n_payload = read(fd, &reciever->msg, reciever->hdr.len);
|
||||
if (n_payload != reciever->hdr.len) return -EFAULT;
|
||||
return n_hdr + n_payload;
|
||||
}
|
||||
|
||||
#define FAIL_ON(cond) if (cond) return EXIT_FAILURE
|
||||
|
||||
#endif
|
||||
@ -1,66 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
int server(int fd_read, int fd_write);
|
||||
|
||||
int main()
|
||||
{
|
||||
FAIL_ON(mknod(FIFO_SERVER_PATH, S_IFIFO | 0666, 0) < 0);
|
||||
printf("created %s\n", FIFO_SERVER_PATH);
|
||||
FAIL_ON(mknod(FIFO_CLIENT_PATH, S_IFIFO | 0666, 0) < 0);
|
||||
printf("created %s\n", FIFO_CLIENT_PATH);
|
||||
|
||||
int read_fd, write_fd;
|
||||
FAIL_ON((read_fd=open(FIFO_SERVER_PATH, O_RDONLY)) < 0);
|
||||
FAIL_ON((write_fd=open(FIFO_CLIENT_PATH, O_WRONLY)) < 0);
|
||||
|
||||
return server(read_fd, write_fd);
|
||||
}
|
||||
|
||||
#define BUF_SIZE 256
|
||||
|
||||
/**
|
||||
* Performs server actions
|
||||
*
|
||||
* @fd_read - pipe to read from
|
||||
* @fd_write - pipe to write to
|
||||
*
|
||||
* returns 0 on success or -errno on fail
|
||||
*/
|
||||
int server(int fd_read, int fd_write)
|
||||
{
|
||||
char buf[BUF_SIZE] = {0};
|
||||
size_t n = read(fd_read, buf, BUF_SIZE);
|
||||
if (n == 0) return -EFAULT;
|
||||
FILE *f = fopen(buf, "r");
|
||||
// не 100% правильно, но наиболее вероятно
|
||||
if (f == NULL) {
|
||||
const char error_msg[] = "Can't open requested file\n";
|
||||
struct custom_ds payload = {
|
||||
.hdr.len = sizeof(error_msg),
|
||||
.hdr.type = DS_FAIL,
|
||||
};
|
||||
strncpy(payload.msg, error_msg, sizeof(error_msg));
|
||||
send_msg(fd_write, &payload);
|
||||
goto epilog;
|
||||
}
|
||||
|
||||
while ((n = fread(buf, sizeof(buf[0]), BUF_SIZE, f)) > 0) {
|
||||
struct custom_ds payload = {
|
||||
.hdr.len = n,
|
||||
.hdr.type = DS_MESSAGE,
|
||||
};
|
||||
strncpy(payload.msg, buf, n);
|
||||
send_msg(fd_write, &payload);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
|
||||
epilog:
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
targets = server_msg client_msg
|
||||
all: $(targets)
|
||||
|
||||
server_msg: server_msg.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
client_msg: client_msg.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(targets)
|
||||
rm -f *.o
|
||||
@ -1,99 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/msg.h>
|
||||
#define MQ_KEY1 1234L
|
||||
#define MQ_KEY2 2345L
|
||||
#define MAXMESGDATA 4096
|
||||
|
||||
struct mymesg {
|
||||
long mesg_len;
|
||||
long mesg_type;
|
||||
char mesg_data[MAXMESGDATA];
|
||||
};
|
||||
|
||||
void client(int readid, int writeid);
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr);
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr);
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
int readid, writeid;
|
||||
|
||||
if((writeid=msgget(MQ_KEY1,0666)) < 0)
|
||||
{
|
||||
printf("Client: can not get writeid!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Client:writeid=%d\n",writeid);
|
||||
|
||||
if((readid=msgget(MQ_KEY2,0666)) < 0)
|
||||
{
|
||||
printf("Client: can not get readid!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Client: readid=%d\n",readid);
|
||||
|
||||
client(readid,writeid);
|
||||
|
||||
if((msgctl(readid,IPC_RMID, NULL)) < 0)
|
||||
{
|
||||
printf("Client: can not delete massage queue2!\n"); exit(1);
|
||||
}
|
||||
|
||||
if((msgctl(writeid,IPC_RMID, NULL)) < 0)
|
||||
{
|
||||
printf("Client: can not delete massage queue1!\n"); exit(1);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void client(int readid, int writeid)
|
||||
{
|
||||
size_t len;
|
||||
ssize_t n;
|
||||
struct mymesg ourmesg;
|
||||
|
||||
printf("Client:readid=%d writeid=%d\n",readid,writeid);
|
||||
printf("Input file name, please\n");
|
||||
|
||||
fgets(ourmesg.mesg_data,MAXMESGDATA, stdin);
|
||||
len=strlen(ourmesg.mesg_data);
|
||||
|
||||
if(ourmesg.mesg_data[len-1]=='\n') len--;
|
||||
ourmesg.mesg_len=len;
|
||||
|
||||
ourmesg.mesg_type=1;
|
||||
|
||||
printf("Client: %s\n",ourmesg.mesg_data);
|
||||
|
||||
mesg_send(writeid,&ourmesg);
|
||||
|
||||
printf("Client: tvk before recv!\n");
|
||||
|
||||
while((n= mesg_recv(readid, &ourmesg))>0)
|
||||
//n= mesg_recv(readid, &ourmesg);
|
||||
write(1,ourmesg.mesg_data, n);
|
||||
}
|
||||
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr)
|
||||
{
|
||||
return(msgsnd(id, &(mptr->mesg_type),mptr->mesg_len, 0));
|
||||
}
|
||||
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||
mptr->mesg_len=n;
|
||||
printf("Client: n=%d\n",n);
|
||||
|
||||
return(n);
|
||||
}
|
||||
|
||||
@ -1,116 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/msg.h>
|
||||
#define MQ_KEY1 1234L
|
||||
#define MQ_KEY2 2345L
|
||||
#define MAXMESGDATA 4096
|
||||
|
||||
struct mymesg {
|
||||
long mesg_len;
|
||||
long mesg_type;
|
||||
char mesg_data[MAXMESGDATA];
|
||||
};
|
||||
|
||||
void server(int readid, int writeid);
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr);
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr);
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
int readid, writeid;
|
||||
key_t key1,key2;
|
||||
|
||||
printf("Server: Hello tvk!\n");
|
||||
|
||||
/*
|
||||
if((key1=ftok("/home/tvk/IPC/input.txt",'A'))<0)
|
||||
{
|
||||
printf("Server: can not get key!\n"); exit(1);
|
||||
}
|
||||
printf("key1=%x\n",key1);
|
||||
*/
|
||||
if((readid=msgget(MQ_KEY1, 0666|IPC_CREAT)) < 0)
|
||||
{
|
||||
printf("Server: can not get readid!\n"); exit(1);
|
||||
}
|
||||
printf("Server: readid=%d\n",readid);
|
||||
/*
|
||||
if((key2=ftok("/home/tvk/IPC/server_msg.c",'B'))<0)
|
||||
{
|
||||
printf("Server: can not get key!\n"); exit(1);
|
||||
}
|
||||
printf("key2=%x\n",key2);
|
||||
*/
|
||||
if((writeid=msgget(MQ_KEY2, 0666|IPC_CREAT)) < 0)
|
||||
{
|
||||
printf("Server: can not get readid!\n"); exit(1);
|
||||
}
|
||||
printf("Server: writeid=%d\n",writeid);
|
||||
|
||||
server(readid,writeid);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void server(int readid, int writeid)
|
||||
{
|
||||
FILE *fp;
|
||||
ssize_t n;
|
||||
struct mymesg ourmesg;
|
||||
|
||||
printf("Server:readid=%d writeid=%d\n",readid,writeid);
|
||||
|
||||
ourmesg.mesg_type=1;
|
||||
|
||||
if( (n=mesg_recv(readid, &ourmesg)) == 0)
|
||||
{
|
||||
printf("Server: can not read file name\n");
|
||||
exit(1);
|
||||
}
|
||||
ourmesg.mesg_data[n]='\0';
|
||||
|
||||
printf("Server: file name %s\n",ourmesg.mesg_data);
|
||||
|
||||
if( (fp=fopen(ourmesg.mesg_data,"r"))==NULL)
|
||||
{
|
||||
printf("Server: can not open file name\n");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Server: %s is opened\n",ourmesg.mesg_data);
|
||||
|
||||
while(fgets(ourmesg.mesg_data, MAXMESGDATA,fp) != NULL)
|
||||
{
|
||||
ourmesg.mesg_len=strlen(ourmesg.mesg_data);
|
||||
printf("Server: %s\n",ourmesg.mesg_data);
|
||||
mesg_send(writeid,&ourmesg);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
ourmesg.mesg_len=0;
|
||||
mesg_send(writeid,&ourmesg);
|
||||
}
|
||||
|
||||
ssize_t mesg_send(int id, struct mymesg *mptr)
|
||||
{
|
||||
return(msgsnd(id, &(mptr->mesg_type),mptr->mesg_len, 0));
|
||||
}
|
||||
|
||||
ssize_t mesg_recv(int id, struct mymesg *mptr)
|
||||
{
|
||||
ssize_t n;
|
||||
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||
mptr->mesg_len=n;
|
||||
|
||||
return(n);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user