Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e57ba38b48 | |||
| 1ecce31da4 | |||
| 5dbe188b08 | |||
| 72d5756b79 | |||
| 949f1b8ac4 | |||
| 27b4b65dd7 | |||
| 03159fa0f9 | |||
| 8a4173f3f7 | |||
| 931fb4b146 |
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
|
||||||
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;
|
||||||
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
12
OSs/lab5/part-A/Makefile
Normal file
12
OSs/lab5/part-A/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
|
||||||
132
OSs/lab5/part-A/main.c
Normal file
132
OSs/lab5/part-A/main.c
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
15
OSs/lab5/part-B/Makefile
Normal file
15
OSs/lab5/part-B/Makefile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
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
|
||||||
80
OSs/lab5/part-B/client.c
Normal file
80
OSs/lab5/part-B/client.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
45
OSs/lab5/part-B/common.h
Normal file
45
OSs/lab5/part-B/common.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#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
|
||||||
66
OSs/lab5/part-B/server.c
Normal file
66
OSs/lab5/part-B/server.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
15
OSs/lab6/Makefile
Normal file
15
OSs/lab6/Makefile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
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
|
||||||
92
OSs/lab6/client_msg.c
Normal file
92
OSs/lab6/client_msg.c
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
#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>
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
void client(int readid, int writeid);
|
||||||
|
ssize_t mesg_send(int id, struct msg *mptr);
|
||||||
|
ssize_t mesg_recv(int id, struct msg *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 msg 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 msg *mptr)
|
||||||
|
{
|
||||||
|
return(msgsnd(id, &(mptr->mesg_type),mptr->mesg_len, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t mesg_recv(int id, struct msg *mptr)
|
||||||
|
{
|
||||||
|
ssize_t n;
|
||||||
|
|
||||||
|
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||||
|
mptr->mesg_len=n;
|
||||||
|
printf("Client: n=%ld\n",n);
|
||||||
|
|
||||||
|
return(n);
|
||||||
|
}
|
||||||
|
|
||||||
14
OSs/lab6/common.h
Normal file
14
OSs/lab6/common.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef COMMON_H_
|
||||||
|
#define COMMON_H_
|
||||||
|
|
||||||
|
#define MQ_KEY1 1234L
|
||||||
|
#define MQ_KEY2 2345L
|
||||||
|
#define MAXMESGDATA 4096
|
||||||
|
|
||||||
|
struct msg {
|
||||||
|
long mesg_len;
|
||||||
|
long mesg_type;
|
||||||
|
char mesg_data[MAXMESGDATA];
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
95
OSs/lab6/server_msg.c
Normal file
95
OSs/lab6/server_msg.c
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
#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>
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
void server(int readid, int writeid);
|
||||||
|
ssize_t mesg_send(int id, struct msg *mptr);
|
||||||
|
ssize_t mesg_recv(int id, struct msg *mptr);
|
||||||
|
|
||||||
|
int main(int argc,char **argv)
|
||||||
|
{
|
||||||
|
int readid, writeid;
|
||||||
|
key_t key1,key2;
|
||||||
|
|
||||||
|
printf("Server: Hello tvk!\n");
|
||||||
|
|
||||||
|
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((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 msg 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 msg *mptr)
|
||||||
|
{
|
||||||
|
return(msgsnd(id, &(mptr->mesg_type),mptr->mesg_len, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t mesg_recv(int id, struct msg *mptr)
|
||||||
|
{
|
||||||
|
ssize_t n;
|
||||||
|
n=msgrcv(id, &(mptr->mesg_type),MAXMESGDATA,mptr->mesg_type, 0);
|
||||||
|
mptr->mesg_len=n;
|
||||||
|
|
||||||
|
return(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
15
OSs/lab7/Makefile
Normal file
15
OSs/lab7/Makefile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
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
|
||||||
65
OSs/lab7/client.c
Normal file
65
OSs/lab7/client.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include "common.h"
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define FAIL_TO_CLEANUP(cond, jmp_to) \
|
||||||
|
if (cond) { \
|
||||||
|
perror(#cond); \
|
||||||
|
ret = EXIT_FAILURE; \
|
||||||
|
goto jmp_to; \
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int ret = 0;
|
||||||
|
int shm_fd = -1;
|
||||||
|
void *shm_ptr = MAP_FAILED;
|
||||||
|
sem_t *sem_c2s = SEM_FAILED;
|
||||||
|
sem_t *sem_s2c = SEM_FAILED;
|
||||||
|
|
||||||
|
FAIL_TO_CLEANUP((shm_fd = shm_open(SHM_NAME, O_RDWR, 0)) == -1, exit);
|
||||||
|
FAIL_TO_CLEANUP(
|
||||||
|
(shm_ptr = mmap(NULL, SHM_SIZE,
|
||||||
|
PROT_READ | PROT_WRITE,
|
||||||
|
MAP_SHARED, shm_fd, 0)) == MAP_FAILED,
|
||||||
|
unmap_shared_mem);
|
||||||
|
|
||||||
|
FAIL_TO_CLEANUP((sem_c2s = sem_open(SEM_C2S_NAME, 0)) == SEM_FAILED,
|
||||||
|
close_sem_c2s);
|
||||||
|
FAIL_TO_CLEANUP((sem_s2c = sem_open(SEM_S2C_NAME, 0)) == SEM_FAILED,
|
||||||
|
close_sem_s2c);
|
||||||
|
|
||||||
|
char filename[SHM_SIZE];
|
||||||
|
FAIL_TO_CLEANUP(fgets(filename, sizeof(filename), stdin) == NULL, close_sem_s2c);
|
||||||
|
|
||||||
|
size_t ln = strlen(filename);
|
||||||
|
if (ln > 0 && filename[ln - 1] == '\n')
|
||||||
|
filename[ln - 1] = '\0';
|
||||||
|
|
||||||
|
memset(shm_ptr, 0, SHM_SIZE);
|
||||||
|
strncpy((char *)shm_ptr, filename, SHM_SIZE - 1);
|
||||||
|
|
||||||
|
FAIL_TO_CLEANUP(sem_post(sem_c2s) == -1, close_sem_s2c);
|
||||||
|
|
||||||
|
FAIL_TO_CLEANUP(sem_wait(sem_s2c) == -1, close_sem_s2c);
|
||||||
|
|
||||||
|
size_t len = strnlen((char *)shm_ptr, SHM_SIZE);
|
||||||
|
FAIL_TO_CLEANUP(fwrite(shm_ptr, sizeof(char), len, stdout) != len, close_sem_s2c);
|
||||||
|
fflush(stdout); // don't really care if fails
|
||||||
|
|
||||||
|
close_sem_s2c:
|
||||||
|
sem_close(sem_s2c);
|
||||||
|
close_sem_c2s:
|
||||||
|
sem_close(sem_c2s);
|
||||||
|
unmap_shared_mem:
|
||||||
|
munmap(shm_ptr, SHM_SIZE);
|
||||||
|
close_shared_fd:
|
||||||
|
close(shm_fd);
|
||||||
|
exit:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
12
OSs/lab7/common.h
Normal file
12
OSs/lab7/common.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef COMMON_H
|
||||||
|
#define COMMON_H
|
||||||
|
|
||||||
|
#define SHM_NAME "/lab7_shm"
|
||||||
|
#define SEM_C2S_NAME "/sem_c2s_lab7"
|
||||||
|
#define SEM_S2C_NAME "/sem_s2c_lab7"
|
||||||
|
#define SHM_SIZE (64 * 1024)
|
||||||
|
|
||||||
|
#define FAIL_ON(cond) if (cond) return EXIT_FAILURE
|
||||||
|
|
||||||
|
#endif /* COMMON_H */
|
||||||
|
|
||||||
136
OSs/lab7/server.c
Normal file
136
OSs/lab7/server.c
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#define FAIL_TO_CLEANUP(cond, jmp_to) \
|
||||||
|
if (cond) { \
|
||||||
|
perror(#cond); \
|
||||||
|
ret = EXIT_FAILURE; \
|
||||||
|
goto jmp_to; \
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int ret;
|
||||||
|
int shm_fd = -1;
|
||||||
|
void *shm_ptr = MAP_FAILED;
|
||||||
|
sem_t *sem_c2s = SEM_FAILED;
|
||||||
|
sem_t *sem_s2c = SEM_FAILED;
|
||||||
|
|
||||||
|
/* Create or open shared memory */
|
||||||
|
FAIL_TO_CLEANUP((shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0600)), cleanup);
|
||||||
|
if (shm_fd == -1) {
|
||||||
|
perror("shm_open");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
if (ftruncate(shm_fd, SHM_SIZE) == -1) {
|
||||||
|
perror("ftruncate");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
shm_ptr = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE,
|
||||||
|
MAP_SHARED, shm_fd, 0);
|
||||||
|
if (shm_ptr == MAP_FAILED) {
|
||||||
|
perror("mmap");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create semaphores (initialize to 0) */
|
||||||
|
sem_c2s = sem_open(SEM_C2S_NAME, O_CREAT, 0600, 0);
|
||||||
|
if (sem_c2s == SEM_FAILED) {
|
||||||
|
perror("sem_open c2s");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
sem_s2c = sem_open(SEM_S2C_NAME, O_CREAT, 0600, 0);
|
||||||
|
if (sem_s2c == SEM_FAILED) {
|
||||||
|
perror("sem_open s2c");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Server: waiting for client...\n");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
/* Wait for client to write filename */
|
||||||
|
if (sem_wait(sem_c2s) == -1) {
|
||||||
|
perror("sem_wait c2s");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read filename from shared memory (ensure NUL) */
|
||||||
|
char filename[SHM_SIZE];
|
||||||
|
memset(filename, 0, sizeof(filename));
|
||||||
|
memcpy(filename, shm_ptr, sizeof(filename) - 1);
|
||||||
|
|
||||||
|
/* If client sent special "EXIT" filename, break loop and cleanup */
|
||||||
|
if (strcmp(filename, "EXIT") == 0) {
|
||||||
|
/* Notify client and exit */
|
||||||
|
const char *msg = "Server shutting down.";
|
||||||
|
size_t len = strlen(msg) + 1;
|
||||||
|
if (len > SHM_SIZE)
|
||||||
|
len = SHM_SIZE;
|
||||||
|
memcpy(shm_ptr, msg, len);
|
||||||
|
sem_post(sem_s2c);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Try open file */
|
||||||
|
FILE *f = fopen(filename, "rb");
|
||||||
|
if (!f) {
|
||||||
|
char errbuf[SHM_SIZE];
|
||||||
|
snprintf(errbuf, sizeof(errbuf), "Error opening file '%s'\n", filename);
|
||||||
|
size_t len = strlen(errbuf) + 1;
|
||||||
|
if (len > SHM_SIZE)
|
||||||
|
len = SHM_SIZE;
|
||||||
|
memcpy(shm_ptr, errbuf, len);
|
||||||
|
sem_post(sem_s2c);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read file content and write to shared memory (truncate if too large) */
|
||||||
|
size_t total = 0;
|
||||||
|
while (!feof(f) && total < SHM_SIZE - 1) {
|
||||||
|
size_t toread = SHM_SIZE - 1 - total;
|
||||||
|
size_t r = fread((char *)shm_ptr + total, 1, toread, f);
|
||||||
|
total += r;
|
||||||
|
if (r == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
((char *)shm_ptr)[total] = '\0'; /* ensure NUL */
|
||||||
|
|
||||||
|
if (!feof(f) && total == SHM_SIZE - 1) {
|
||||||
|
/* truncated */
|
||||||
|
const char *note =
|
||||||
|
"\n[Output truncated: file larger than shared memory]\n";
|
||||||
|
size_t note_len = strlen(note);
|
||||||
|
if (note_len + 1 < SHM_SIZE) {
|
||||||
|
size_t copy_pos = SHM_SIZE - 1 - note_len;
|
||||||
|
memcpy((char *)shm_ptr + copy_pos, note, note_len);
|
||||||
|
((char *)shm_ptr)[SHM_SIZE - 1] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
/* Signal client that data is ready */
|
||||||
|
if (sem_post(sem_s2c) == -1) {
|
||||||
|
perror("sem_post s2c");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (sem_c2s != SEM_FAILED)
|
||||||
|
sem_close(sem_c2s);
|
||||||
|
if (sem_s2c != SEM_FAILED)
|
||||||
|
sem_close(sem_s2c);
|
||||||
|
if (shm_ptr != MAP_FAILED) munmap(shm_ptr, SHM_SIZE); if (shm_fd != -1)
|
||||||
|
close(shm_fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user