From f3a5c1f658b1bcbd901201cfcfabd15be142b17e Mon Sep 17 00:00:00 2001 From: pajjilykk Date: Wed, 10 Dec 2025 16:50:28 +0700 Subject: [PATCH] kirill-refactor --- {lab_1/kirill`s => kirill/lab_1}/in.txt | 0 {lab_1/kirill`s => kirill/lab_1}/task18.c | 0 {lab_2/kirill => kirill/lab_2}/Makefile | 0 {lab_2/kirill => kirill/lab_2}/lib_d.c | 0 {lab_2/kirill => kirill/lab_2}/lib_s.c | 0 {lab_2/kirill => kirill/lab_2}/main_d.c | 0 {lab_2/kirill => kirill/lab_2}/main_s.c | 0 {lab_3/kirill => kirill/lab_3}/Makefile | 0 {lab_3/kirill => kirill/lab_3}/parent.c | 0 {lab_3/kirill => kirill/lab_3}/task18.c | 0 {lab_4/kirill's => kirill/lab_4}/Makefile | 0 .../kirill's => kirill/lab_4}/fifo_client.c | 0 .../kirill's => kirill/lab_4}/fifo_server.c | 0 {lab_6/kirill => kirill/lab_6}/Makefile | 0 {lab_6/kirill => kirill/lab_6}/client.c | 0 {lab_6 => kirill/lab_6}/input.txt | 0 {lab_6/kirill => kirill/lab_6}/server.c | 0 {lab_7/kirill => kirill/lab_7}/Makefile | 0 {lab_7/kirill => kirill/lab_7}/main.c | 0 {lab_8/kirill => kirill/lab_8}/Makefile | 0 {lab_8/kirill => kirill/lab_8}/client.c | 0 {lab_8/kirill => kirill/lab_8}/server.c | 0 .../.old/12_first_symbols_to_spaces.cpp | 0 {lab_1 => mine/lab_1}/.old/compile.sh | 0 {lab_1 => mine/lab_1}/.old/email | 0 {lab_1 => mine/lab_1}/.old/file.in | 0 {lab_1 => mine/lab_1}/.old/run.sh | 0 {lab_1 => mine/lab_1}/Makefile | 0 {lab_1 => mine/lab_1}/examples/copy.c | 0 {lab_1 => mine/lab_1}/examples/file.c | 0 {lab_1 => mine/lab_1}/ilya`s/file.in | 0 {lab_1 => mine/lab_1}/ilya`s/file.out | 0 {lab_1 => mine/lab_1}/ilya`s/signs | Bin {lab_1 => mine/lab_1}/ilya`s/signs.cpp | 0 {lab_1 => mine/lab_1}/in.txt | 0 mine/lab_1/kirill`s/in.txt | 4 + mine/lab_1/kirill`s/task18.c | 129 ++++++++ {lab_1 => mine/lab_1}/task12.c | 0 {lab_1 => mine/lab_1}/vlad`s/in.txt | 0 {lab_1 => mine/lab_1}/vlad`s/task11.c | 0 {lab_2 => mine/lab_2}/Makefile | 0 {lab_2 => mine/lab_2}/README.txt | 0 {lab_2 => mine/lab_2}/in.txt | 0 mine/lab_2/kirill/Makefile | 52 ++++ mine/lab_2/kirill/lib_d.c | 18 ++ mine/lab_2/kirill/lib_s.c | 19 ++ mine/lab_2/kirill/main_d.c | 62 ++++ mine/lab_2/kirill/main_s.c | 44 +++ {lab_2 => mine/lab_2}/main_dynamic.c | 0 {lab_2 => mine/lab_2}/main_static.c | 0 {lab_2 => mine/lab_2}/old/Makefile | 0 {lab_2 => mine/lab_2}/old/task12.c | 0 {lab_2 => mine/lab_2}/old/textlib.c | 0 {lab_2 => mine/lab_2}/textlib_dynamic.c | 0 {lab_2 => mine/lab_2}/textlib_static.c | 0 {lab_2 => mine/lab_2}/textlib_static.h | 0 {lab_2 => mine/lab_2}/vlad/Makefile | 0 {lab_2 => mine/lab_2}/vlad/lib_d.c | 0 {lab_2 => mine/lab_2}/vlad/lib_s.c | 0 {lab_2 => mine/lab_2}/vlad/main_d.c | 0 {lab_2 => mine/lab_2}/vlad/main_s.c | 0 {lab_3 => mine/lab_3}/Makefile | 0 mine/lab_3/kirill/Makefile | 55 ++++ mine/lab_3/kirill/parent.c | 117 ++++++++ mine/lab_3/kirill/task18.c | 129 ++++++++ {lab_3 => mine/lab_3}/lab1_var12.c | 0 {lab_3 => mine/lab_3}/parent.c | 0 {lab_3 => mine/lab_3}/vlad/Makefile | 0 {lab_3 => mine/lab_3}/vlad/parent.c | 0 {lab_3 => mine/lab_3}/vlad/task11.c | 0 {lab_4 => mine/lab_4}/Makefile | 0 {lab_4 => mine/lab_4}/fifo_client.c | 0 {lab_4 => mine/lab_4}/fifo_server.c | 0 .../vlad's => mine/lab_4/kirill's}/Makefile | 0 .../lab_4/kirill's}/fifo_client.c | 0 mine/lab_4/kirill's/fifo_server.c | 183 ++++++++++++ mine/lab_4/vlad's/Makefile | 99 +++++++ mine/lab_4/vlad's/fifo_client.c | 144 +++++++++ {lab_4 => mine/lab_4}/vlad's/fifo_server.c | 0 {lab_5 => mine/lab_5}/Makefile | 0 {lab_5 => mine/lab_5}/common.h | 0 {lab_5 => mine/lab_5}/server.c | 0 {lab_5 => mine/lab_5}/worker.c | 0 {lab_6 => mine/lab_6}/Makefile | 0 {lab_6 => mine/lab_6}/client.c | 0 {lab_6/kirill => mine/lab_6}/input.txt | 0 {lab_6/vlad => mine/lab_6/kirill}/Makefile | 0 mine/lab_6/kirill/client.c | 141 +++++++++ {lab_6/vlad => mine/lab_6/kirill}/input.txt | 0 mine/lab_6/kirill/server.c | 185 ++++++++++++ {lab_6 => mine/lab_6}/server.c | 0 mine/lab_6/vlad/Makefile | 51 ++++ {lab_6 => mine/lab_6}/vlad/client.c | 0 mine/lab_6/vlad/input.txt | 6 + {lab_6 => mine/lab_6}/vlad/server.c | 0 {lab_7 => mine/lab_7}/Makefile | 0 mine/lab_7/kirill/Makefile | 43 +++ mine/lab_7/kirill/main.c | 276 ++++++++++++++++++ {lab_7 => mine/lab_7}/threads_var12.c | 0 {lab_7 => mine/lab_7}/vlad/Makefile | 0 {lab_7 => mine/lab_7}/vlad/main.c | 0 {lab_8 => mine/lab_8}/Makefile | 0 {lab_8 => mine/lab_8}/client.c | 0 mine/lab_8/kirill/Makefile | 50 ++++ mine/lab_8/kirill/client.c | 117 ++++++++ mine/lab_8/kirill/server.c | 142 +++++++++ {lab_8 => mine/lab_8}/server.c | 0 {lab_8 => mine/lab_8}/vlad/Makefile | 0 {lab_8 => mine/lab_8}/vlad/client.c | 0 {lab_8 => mine/lab_8}/vlad/server.c | 0 {rgz => mine/rgz}/Makefile | 0 {rgz => mine/rgz}/fifo_client.c | 0 {rgz => mine/rgz}/fifo_server_daemon.c | 0 {rgz => mine/rgz}/input.txt | 0 114 files changed, 2066 insertions(+) rename {lab_1/kirill`s => kirill/lab_1}/in.txt (100%) rename {lab_1/kirill`s => kirill/lab_1}/task18.c (100%) rename {lab_2/kirill => kirill/lab_2}/Makefile (100%) rename {lab_2/kirill => kirill/lab_2}/lib_d.c (100%) rename {lab_2/kirill => kirill/lab_2}/lib_s.c (100%) rename {lab_2/kirill => kirill/lab_2}/main_d.c (100%) rename {lab_2/kirill => kirill/lab_2}/main_s.c (100%) rename {lab_3/kirill => kirill/lab_3}/Makefile (100%) rename {lab_3/kirill => kirill/lab_3}/parent.c (100%) rename {lab_3/kirill => kirill/lab_3}/task18.c (100%) rename {lab_4/kirill's => kirill/lab_4}/Makefile (100%) rename {lab_4/kirill's => kirill/lab_4}/fifo_client.c (100%) rename {lab_4/kirill's => kirill/lab_4}/fifo_server.c (100%) rename {lab_6/kirill => kirill/lab_6}/Makefile (100%) rename {lab_6/kirill => kirill/lab_6}/client.c (100%) rename {lab_6 => kirill/lab_6}/input.txt (100%) rename {lab_6/kirill => kirill/lab_6}/server.c (100%) rename {lab_7/kirill => kirill/lab_7}/Makefile (100%) rename {lab_7/kirill => kirill/lab_7}/main.c (100%) rename {lab_8/kirill => kirill/lab_8}/Makefile (100%) rename {lab_8/kirill => kirill/lab_8}/client.c (100%) rename {lab_8/kirill => kirill/lab_8}/server.c (100%) rename {lab_1 => mine/lab_1}/.old/12_first_symbols_to_spaces.cpp (100%) rename {lab_1 => mine/lab_1}/.old/compile.sh (100%) rename {lab_1 => mine/lab_1}/.old/email (100%) rename {lab_1 => mine/lab_1}/.old/file.in (100%) rename {lab_1 => mine/lab_1}/.old/run.sh (100%) rename {lab_1 => mine/lab_1}/Makefile (100%) rename {lab_1 => mine/lab_1}/examples/copy.c (100%) rename {lab_1 => mine/lab_1}/examples/file.c (100%) rename {lab_1 => mine/lab_1}/ilya`s/file.in (100%) rename {lab_1 => mine/lab_1}/ilya`s/file.out (100%) rename {lab_1 => mine/lab_1}/ilya`s/signs (100%) rename {lab_1 => mine/lab_1}/ilya`s/signs.cpp (100%) rename {lab_1 => mine/lab_1}/in.txt (100%) create mode 100644 mine/lab_1/kirill`s/in.txt create mode 100644 mine/lab_1/kirill`s/task18.c rename {lab_1 => mine/lab_1}/task12.c (100%) rename {lab_1 => mine/lab_1}/vlad`s/in.txt (100%) rename {lab_1 => mine/lab_1}/vlad`s/task11.c (100%) rename {lab_2 => mine/lab_2}/Makefile (100%) rename {lab_2 => mine/lab_2}/README.txt (100%) rename {lab_2 => mine/lab_2}/in.txt (100%) create mode 100644 mine/lab_2/kirill/Makefile create mode 100644 mine/lab_2/kirill/lib_d.c create mode 100644 mine/lab_2/kirill/lib_s.c create mode 100644 mine/lab_2/kirill/main_d.c create mode 100644 mine/lab_2/kirill/main_s.c rename {lab_2 => mine/lab_2}/main_dynamic.c (100%) rename {lab_2 => mine/lab_2}/main_static.c (100%) rename {lab_2 => mine/lab_2}/old/Makefile (100%) rename {lab_2 => mine/lab_2}/old/task12.c (100%) rename {lab_2 => mine/lab_2}/old/textlib.c (100%) rename {lab_2 => mine/lab_2}/textlib_dynamic.c (100%) rename {lab_2 => mine/lab_2}/textlib_static.c (100%) rename {lab_2 => mine/lab_2}/textlib_static.h (100%) rename {lab_2 => mine/lab_2}/vlad/Makefile (100%) rename {lab_2 => mine/lab_2}/vlad/lib_d.c (100%) rename {lab_2 => mine/lab_2}/vlad/lib_s.c (100%) rename {lab_2 => mine/lab_2}/vlad/main_d.c (100%) rename {lab_2 => mine/lab_2}/vlad/main_s.c (100%) rename {lab_3 => mine/lab_3}/Makefile (100%) create mode 100644 mine/lab_3/kirill/Makefile create mode 100644 mine/lab_3/kirill/parent.c create mode 100644 mine/lab_3/kirill/task18.c rename {lab_3 => mine/lab_3}/lab1_var12.c (100%) rename {lab_3 => mine/lab_3}/parent.c (100%) rename {lab_3 => mine/lab_3}/vlad/Makefile (100%) rename {lab_3 => mine/lab_3}/vlad/parent.c (100%) rename {lab_3 => mine/lab_3}/vlad/task11.c (100%) rename {lab_4 => mine/lab_4}/Makefile (100%) rename {lab_4 => mine/lab_4}/fifo_client.c (100%) rename {lab_4 => mine/lab_4}/fifo_server.c (100%) rename {lab_4/vlad's => mine/lab_4/kirill's}/Makefile (100%) rename {lab_4/vlad's => mine/lab_4/kirill's}/fifo_client.c (100%) create mode 100644 mine/lab_4/kirill's/fifo_server.c create mode 100644 mine/lab_4/vlad's/Makefile create mode 100644 mine/lab_4/vlad's/fifo_client.c rename {lab_4 => mine/lab_4}/vlad's/fifo_server.c (100%) rename {lab_5 => mine/lab_5}/Makefile (100%) rename {lab_5 => mine/lab_5}/common.h (100%) rename {lab_5 => mine/lab_5}/server.c (100%) rename {lab_5 => mine/lab_5}/worker.c (100%) rename {lab_6 => mine/lab_6}/Makefile (100%) rename {lab_6 => mine/lab_6}/client.c (100%) rename {lab_6/kirill => mine/lab_6}/input.txt (100%) rename {lab_6/vlad => mine/lab_6/kirill}/Makefile (100%) create mode 100644 mine/lab_6/kirill/client.c rename {lab_6/vlad => mine/lab_6/kirill}/input.txt (100%) create mode 100644 mine/lab_6/kirill/server.c rename {lab_6 => mine/lab_6}/server.c (100%) create mode 100644 mine/lab_6/vlad/Makefile rename {lab_6 => mine/lab_6}/vlad/client.c (100%) create mode 100644 mine/lab_6/vlad/input.txt rename {lab_6 => mine/lab_6}/vlad/server.c (100%) rename {lab_7 => mine/lab_7}/Makefile (100%) create mode 100644 mine/lab_7/kirill/Makefile create mode 100644 mine/lab_7/kirill/main.c rename {lab_7 => mine/lab_7}/threads_var12.c (100%) rename {lab_7 => mine/lab_7}/vlad/Makefile (100%) rename {lab_7 => mine/lab_7}/vlad/main.c (100%) rename {lab_8 => mine/lab_8}/Makefile (100%) rename {lab_8 => mine/lab_8}/client.c (100%) create mode 100644 mine/lab_8/kirill/Makefile create mode 100644 mine/lab_8/kirill/client.c create mode 100644 mine/lab_8/kirill/server.c rename {lab_8 => mine/lab_8}/server.c (100%) rename {lab_8 => mine/lab_8}/vlad/Makefile (100%) rename {lab_8 => mine/lab_8}/vlad/client.c (100%) rename {lab_8 => mine/lab_8}/vlad/server.c (100%) rename {rgz => mine/rgz}/Makefile (100%) rename {rgz => mine/rgz}/fifo_client.c (100%) rename {rgz => mine/rgz}/fifo_server_daemon.c (100%) rename {rgz => mine/rgz}/input.txt (100%) diff --git a/lab_1/kirill`s/in.txt b/kirill/lab_1/in.txt similarity index 100% rename from lab_1/kirill`s/in.txt rename to kirill/lab_1/in.txt diff --git a/lab_1/kirill`s/task18.c b/kirill/lab_1/task18.c similarity index 100% rename from lab_1/kirill`s/task18.c rename to kirill/lab_1/task18.c diff --git a/lab_2/kirill/Makefile b/kirill/lab_2/Makefile similarity index 100% rename from lab_2/kirill/Makefile rename to kirill/lab_2/Makefile diff --git a/lab_2/kirill/lib_d.c b/kirill/lab_2/lib_d.c similarity index 100% rename from lab_2/kirill/lib_d.c rename to kirill/lab_2/lib_d.c diff --git a/lab_2/kirill/lib_s.c b/kirill/lab_2/lib_s.c similarity index 100% rename from lab_2/kirill/lib_s.c rename to kirill/lab_2/lib_s.c diff --git a/lab_2/kirill/main_d.c b/kirill/lab_2/main_d.c similarity index 100% rename from lab_2/kirill/main_d.c rename to kirill/lab_2/main_d.c diff --git a/lab_2/kirill/main_s.c b/kirill/lab_2/main_s.c similarity index 100% rename from lab_2/kirill/main_s.c rename to kirill/lab_2/main_s.c diff --git a/lab_3/kirill/Makefile b/kirill/lab_3/Makefile similarity index 100% rename from lab_3/kirill/Makefile rename to kirill/lab_3/Makefile diff --git a/lab_3/kirill/parent.c b/kirill/lab_3/parent.c similarity index 100% rename from lab_3/kirill/parent.c rename to kirill/lab_3/parent.c diff --git a/lab_3/kirill/task18.c b/kirill/lab_3/task18.c similarity index 100% rename from lab_3/kirill/task18.c rename to kirill/lab_3/task18.c diff --git a/lab_4/kirill's/Makefile b/kirill/lab_4/Makefile similarity index 100% rename from lab_4/kirill's/Makefile rename to kirill/lab_4/Makefile diff --git a/lab_4/kirill's/fifo_client.c b/kirill/lab_4/fifo_client.c similarity index 100% rename from lab_4/kirill's/fifo_client.c rename to kirill/lab_4/fifo_client.c diff --git a/lab_4/kirill's/fifo_server.c b/kirill/lab_4/fifo_server.c similarity index 100% rename from lab_4/kirill's/fifo_server.c rename to kirill/lab_4/fifo_server.c diff --git a/lab_6/kirill/Makefile b/kirill/lab_6/Makefile similarity index 100% rename from lab_6/kirill/Makefile rename to kirill/lab_6/Makefile diff --git a/lab_6/kirill/client.c b/kirill/lab_6/client.c similarity index 100% rename from lab_6/kirill/client.c rename to kirill/lab_6/client.c diff --git a/lab_6/input.txt b/kirill/lab_6/input.txt similarity index 100% rename from lab_6/input.txt rename to kirill/lab_6/input.txt diff --git a/lab_6/kirill/server.c b/kirill/lab_6/server.c similarity index 100% rename from lab_6/kirill/server.c rename to kirill/lab_6/server.c diff --git a/lab_7/kirill/Makefile b/kirill/lab_7/Makefile similarity index 100% rename from lab_7/kirill/Makefile rename to kirill/lab_7/Makefile diff --git a/lab_7/kirill/main.c b/kirill/lab_7/main.c similarity index 100% rename from lab_7/kirill/main.c rename to kirill/lab_7/main.c diff --git a/lab_8/kirill/Makefile b/kirill/lab_8/Makefile similarity index 100% rename from lab_8/kirill/Makefile rename to kirill/lab_8/Makefile diff --git a/lab_8/kirill/client.c b/kirill/lab_8/client.c similarity index 100% rename from lab_8/kirill/client.c rename to kirill/lab_8/client.c diff --git a/lab_8/kirill/server.c b/kirill/lab_8/server.c similarity index 100% rename from lab_8/kirill/server.c rename to kirill/lab_8/server.c diff --git a/lab_1/.old/12_first_symbols_to_spaces.cpp b/mine/lab_1/.old/12_first_symbols_to_spaces.cpp similarity index 100% rename from lab_1/.old/12_first_symbols_to_spaces.cpp rename to mine/lab_1/.old/12_first_symbols_to_spaces.cpp diff --git a/lab_1/.old/compile.sh b/mine/lab_1/.old/compile.sh similarity index 100% rename from lab_1/.old/compile.sh rename to mine/lab_1/.old/compile.sh diff --git a/lab_1/.old/email b/mine/lab_1/.old/email similarity index 100% rename from lab_1/.old/email rename to mine/lab_1/.old/email diff --git a/lab_1/.old/file.in b/mine/lab_1/.old/file.in similarity index 100% rename from lab_1/.old/file.in rename to mine/lab_1/.old/file.in diff --git a/lab_1/.old/run.sh b/mine/lab_1/.old/run.sh similarity index 100% rename from lab_1/.old/run.sh rename to mine/lab_1/.old/run.sh diff --git a/lab_1/Makefile b/mine/lab_1/Makefile similarity index 100% rename from lab_1/Makefile rename to mine/lab_1/Makefile diff --git a/lab_1/examples/copy.c b/mine/lab_1/examples/copy.c similarity index 100% rename from lab_1/examples/copy.c rename to mine/lab_1/examples/copy.c diff --git a/lab_1/examples/file.c b/mine/lab_1/examples/file.c similarity index 100% rename from lab_1/examples/file.c rename to mine/lab_1/examples/file.c diff --git a/lab_1/ilya`s/file.in b/mine/lab_1/ilya`s/file.in similarity index 100% rename from lab_1/ilya`s/file.in rename to mine/lab_1/ilya`s/file.in diff --git a/lab_1/ilya`s/file.out b/mine/lab_1/ilya`s/file.out similarity index 100% rename from lab_1/ilya`s/file.out rename to mine/lab_1/ilya`s/file.out diff --git a/lab_1/ilya`s/signs b/mine/lab_1/ilya`s/signs similarity index 100% rename from lab_1/ilya`s/signs rename to mine/lab_1/ilya`s/signs diff --git a/lab_1/ilya`s/signs.cpp b/mine/lab_1/ilya`s/signs.cpp similarity index 100% rename from lab_1/ilya`s/signs.cpp rename to mine/lab_1/ilya`s/signs.cpp diff --git a/lab_1/in.txt b/mine/lab_1/in.txt similarity index 100% rename from lab_1/in.txt rename to mine/lab_1/in.txt diff --git a/mine/lab_1/kirill`s/in.txt b/mine/lab_1/kirill`s/in.txt new file mode 100644 index 0000000..ec56556 --- /dev/null +++ b/mine/lab_1/kirill`s/in.txt @@ -0,0 +1,4 @@ +123456123456123456123456123456123456123456123456 +123456123456 +123456123456 +123456123456 diff --git a/mine/lab_1/kirill`s/task18.c b/mine/lab_1/kirill`s/task18.c new file mode 100644 index 0000000..76976b6 --- /dev/null +++ b/mine/lab_1/kirill`s/task18.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include + +#define RBUFSZ 4096 +#define WBUFSZ 4096 + +static void die_perror(const char *what, const char *path, int exit_code) { + int saved = errno; + char msg[512]; + int n; + if (path) { + n = snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved)); + } else { + n = snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved)); + } + if (n > 0) (void) write(STDERR_FILENO, msg, (size_t) n); + _exit(exit_code); +} + +static void xwrite_all(int fd, const char *buf, size_t len, const char *path) { + size_t off = 0; + while (off < len) { + ssize_t n = write(fd, buf + off, len - off); + if (n < 0) { + if (errno == EINTR) continue; + die_perror("write failed", path, -1); + } + off += (size_t) n; + } +} + +static long long parse_ll(const char *s) { + char *end = NULL; + errno = 0; + long long v = strtoll(s, &end, 10); + if (errno != 0 || end == s || *end != '\0' || v < 0) { + errno = EINVAL; + return -1; + } + return v; +} + +int main(int argc, char *argv[]) { + if (argc != 4) { + const char *usage = + "Usage: lab1_var_thirds_line \n" + "Replace every third non-newline byte in each line with a space; counter resets after LF.\n"; + (void) write(STDERR_FILENO, usage, strlen(usage)); + return -1; + } + + const char *in_path = argv[1]; + const char *out_path = argv[2]; + long long cap = parse_ll(argv[3]); + if (cap < 0) die_perror("invalid max_replacements", argv[3], -1); + + int in_fd = open(in_path, O_RDONLY); + if (in_fd < 0) die_perror("open input failed", in_path, -1); + + mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, perms); + if (out_fd < 0) die_perror("open output failed", out_path, -1); + + char rbuf[RBUFSZ]; + char wbuf[WBUFSZ]; + size_t wlen = 0; + + long long total = 0; + long long col = 0; + int replacing_enabled = 1; + + for (;;) { + ssize_t n = read(in_fd, rbuf, sizeof(rbuf)); + if (n > 0) { + for (ssize_t i = 0; i < n; i++) { + unsigned char c = (unsigned char) rbuf[i]; + + if (c == '\n') { + if (wlen == sizeof(wbuf)) { + xwrite_all(out_fd, wbuf, wlen, out_path); + wlen = 0; + } + wbuf[wlen++] = '\n'; + col = 0; + continue; + } + + unsigned char outc = c; + + col++; + + if (replacing_enabled && (col % 3 == 0) && total < cap) { + outc = ' '; + total++; + if (total == cap) replacing_enabled = 0; + } + + if (wlen == sizeof(wbuf)) { + xwrite_all(out_fd, wbuf, wlen, out_path); + wlen = 0; + } + wbuf[wlen++] = (char) outc; + } + } else if (n == 0) { + if (wlen > 0) { + xwrite_all(out_fd, wbuf, wlen, out_path); + wlen = 0; + } + break; + } else { + if (errno == EINTR) continue; + die_perror("read failed", in_path, -1); + } + } + + if (close(in_fd) < 0) die_perror("close input failed", in_path, -1); + if (close(out_fd) < 0) die_perror("close output failed", out_path, -1); + + char res[64]; + int m = snprintf(res, sizeof(res), "%lld\n", total); + if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m); + + return 0; +} diff --git a/lab_1/task12.c b/mine/lab_1/task12.c similarity index 100% rename from lab_1/task12.c rename to mine/lab_1/task12.c diff --git a/lab_1/vlad`s/in.txt b/mine/lab_1/vlad`s/in.txt similarity index 100% rename from lab_1/vlad`s/in.txt rename to mine/lab_1/vlad`s/in.txt diff --git a/lab_1/vlad`s/task11.c b/mine/lab_1/vlad`s/task11.c similarity index 100% rename from lab_1/vlad`s/task11.c rename to mine/lab_1/vlad`s/task11.c diff --git a/lab_2/Makefile b/mine/lab_2/Makefile similarity index 100% rename from lab_2/Makefile rename to mine/lab_2/Makefile diff --git a/lab_2/README.txt b/mine/lab_2/README.txt similarity index 100% rename from lab_2/README.txt rename to mine/lab_2/README.txt diff --git a/lab_2/in.txt b/mine/lab_2/in.txt similarity index 100% rename from lab_2/in.txt rename to mine/lab_2/in.txt diff --git a/mine/lab_2/kirill/Makefile b/mine/lab_2/kirill/Makefile new file mode 100644 index 0000000..94088c9 --- /dev/null +++ b/mine/lab_2/kirill/Makefile @@ -0,0 +1,52 @@ +# Makefile + +CC = gcc +CFLAGS = -Wall -Wextra -O2 +PICFLAGS = -fPIC + +.PHONY: all dynamic static test-dynamic test-static clean + +all: dynamic static + +# --- Dynamic (shared) build --- +dynamic: libtext.so main_d + +libtext.so: lib_d.o + $(CC) -shared -o $@ $^ + +main_d: main_d.o + $(CC) -o $@ $^ -ldl + +lib_d.o: lib_d.c + $(CC) $(CFLAGS) $(PICFLAGS) -c $< -o $@ + +# --- Static build --- +static: libtext.a main_s + +libtext.a: lib_s.o + ar rcs $@ $^ + +main_s: main_s.o libtext.a + $(CC) -o $@ main_s.o libtext.a + +# Generic rule for other .o files +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +# --- Test targets --- +# Creates a small `test_input.txt`, runs the program, and shows `out.txt` +test-dynamic: dynamic + printf "Hello123456789\nLine2abc\n" > test_input.txt + ./main_d test_input.txt out.txt 100 ./libtext.so + @echo "---- out.txt ----" + cat out.txt + +test-static: static + printf "Hello123456789\nLine2abc\n" > test_input.txt + ./main_s test_input.txt out.txt 100 + @echo "---- out.txt ----" + cat out.txt + +# --- Cleanup --- +clean: + rm -f *.o *.so *.a main_d main_s test_input.txt out.txt diff --git a/mine/lab_2/kirill/lib_d.c b/mine/lab_2/kirill/lib_d.c new file mode 100644 index 0000000..f1b2833 --- /dev/null +++ b/mine/lab_2/kirill/lib_d.c @@ -0,0 +1,18 @@ +#include + +int replace_char_line(char *buf, int key, int *replacements_left) { + (void)key; + int replaced = 0; + int count = 0; + + for (size_t i = 0; buf[i] != '\0'; i++) { + if (buf[i] == '\n') continue; + count++; + if (count % 3 == 0 && *replacements_left > 0) { + buf[i] = ' '; + replaced++; + (*replacements_left)--; + } + } + return replaced; +} diff --git a/mine/lab_2/kirill/lib_s.c b/mine/lab_2/kirill/lib_s.c new file mode 100644 index 0000000..5fd77c0 --- /dev/null +++ b/mine/lab_2/kirill/lib_s.c @@ -0,0 +1,19 @@ +#include + +int replace_char_line(char *buf, int key, int *replacements_left) { + (void)key; + int replaced = 0; + int count = 0; + + for (size_t i = 0; buf[i] != '\0'; i++) { + if (buf[i] == '\n') continue; + count++; + if (count % 3 == 0 && *replacements_left > 0) { + buf[i] = ' '; + replaced++; + (*replacements_left)--; + if (*replacements_left == 0) break; + } + } + return replaced; +} \ No newline at end of file diff --git a/mine/lab_2/kirill/main_d.c b/mine/lab_2/kirill/main_d.c new file mode 100644 index 0000000..69a2fc2 --- /dev/null +++ b/mine/lab_2/kirill/main_d.c @@ -0,0 +1,62 @@ +#include +#include +#include + +#define MAX_LINE 4096 + +typedef int (*replace_func_t)(char*, int, int*); + +int main(int argc, char *argv[]) { + if (argc != 5) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + FILE *fin = fopen(argv[1], "r"); + if (!fin) { perror("fopen input"); return 1; } + FILE *fout = fopen(argv[2], "w"); + if (!fout) { perror("fopen output"); fclose(fin); return 1; } + + int cap = atoi(argv[3]); + if (cap < 0) { + fprintf(stderr, "invalid cap\n"); + fclose(fin); + fclose(fout); + return 1; + } + + void *lib = dlopen(argv[4], RTLD_LAZY); + if (!lib) { + fprintf(stderr, "dlopen error: %s\n", dlerror()); + fclose(fin); + fclose(fout); + return 1; + } + + replace_func_t replace = (replace_func_t)dlsym(lib, "replace_char_line"); + if (!replace) { + fprintf(stderr, "dlsym error: %s\n", dlerror()); + dlclose(lib); + fclose(fin); + fclose(fout); + return 1; + } + + int total = 0; + char line[MAX_LINE]; + + while (fgets(line, sizeof(line), fin)) { + if (cap > 0) { + int key = (unsigned char)line[0]; + int repl_line = replace(line, key, &cap); + total += repl_line; + } + fputs(line, fout); + } + + dlclose(lib); + fclose(fin); + fclose(fout); + printf("total_replacements: %d\n", total); + return 0; +} diff --git a/mine/lab_2/kirill/main_s.c b/mine/lab_2/kirill/main_s.c new file mode 100644 index 0000000..4c69b9f --- /dev/null +++ b/mine/lab_2/kirill/main_s.c @@ -0,0 +1,44 @@ +#include +#include + +#define MAX_LINE 4096 + +int replace_char_line(char *buf, int key, int *replacements_left); + +int main(int argc, char *argv[]) { + if (argc != 4) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + FILE *fin = fopen(argv[1], "r"); + if (!fin) { perror("fopen input"); return 1; } + FILE *fout = fopen(argv[2], "w"); + if (!fout) { perror("fopen output"); fclose(fin); return 1; } + + int cap = atoi(argv[3]); + if (cap < 0) { + fprintf(stderr, "invalid cap\n"); + fclose(fin); + fclose(fout); + return 1; + } + + int total = 0; + char line[MAX_LINE]; + + while (fgets(line, sizeof(line), fin)) { + if (cap > 0) { + // key is unused, but pass the first byte for symmetry + int key = (unsigned char)line[0]; + int repl_line = replace_char_line(line, key, &cap); + total += repl_line; + } + fputs(line, fout); + } + + fclose(fin); + fclose(fout); + printf("total_replacements: %d\n", total); + return 0; +} diff --git a/lab_2/main_dynamic.c b/mine/lab_2/main_dynamic.c similarity index 100% rename from lab_2/main_dynamic.c rename to mine/lab_2/main_dynamic.c diff --git a/lab_2/main_static.c b/mine/lab_2/main_static.c similarity index 100% rename from lab_2/main_static.c rename to mine/lab_2/main_static.c diff --git a/lab_2/old/Makefile b/mine/lab_2/old/Makefile similarity index 100% rename from lab_2/old/Makefile rename to mine/lab_2/old/Makefile diff --git a/lab_2/old/task12.c b/mine/lab_2/old/task12.c similarity index 100% rename from lab_2/old/task12.c rename to mine/lab_2/old/task12.c diff --git a/lab_2/old/textlib.c b/mine/lab_2/old/textlib.c similarity index 100% rename from lab_2/old/textlib.c rename to mine/lab_2/old/textlib.c diff --git a/lab_2/textlib_dynamic.c b/mine/lab_2/textlib_dynamic.c similarity index 100% rename from lab_2/textlib_dynamic.c rename to mine/lab_2/textlib_dynamic.c diff --git a/lab_2/textlib_static.c b/mine/lab_2/textlib_static.c similarity index 100% rename from lab_2/textlib_static.c rename to mine/lab_2/textlib_static.c diff --git a/lab_2/textlib_static.h b/mine/lab_2/textlib_static.h similarity index 100% rename from lab_2/textlib_static.h rename to mine/lab_2/textlib_static.h diff --git a/lab_2/vlad/Makefile b/mine/lab_2/vlad/Makefile similarity index 100% rename from lab_2/vlad/Makefile rename to mine/lab_2/vlad/Makefile diff --git a/lab_2/vlad/lib_d.c b/mine/lab_2/vlad/lib_d.c similarity index 100% rename from lab_2/vlad/lib_d.c rename to mine/lab_2/vlad/lib_d.c diff --git a/lab_2/vlad/lib_s.c b/mine/lab_2/vlad/lib_s.c similarity index 100% rename from lab_2/vlad/lib_s.c rename to mine/lab_2/vlad/lib_s.c diff --git a/lab_2/vlad/main_d.c b/mine/lab_2/vlad/main_d.c similarity index 100% rename from lab_2/vlad/main_d.c rename to mine/lab_2/vlad/main_d.c diff --git a/lab_2/vlad/main_s.c b/mine/lab_2/vlad/main_s.c similarity index 100% rename from lab_2/vlad/main_s.c rename to mine/lab_2/vlad/main_s.c diff --git a/lab_3/Makefile b/mine/lab_3/Makefile similarity index 100% rename from lab_3/Makefile rename to mine/lab_3/Makefile diff --git a/mine/lab_3/kirill/Makefile b/mine/lab_3/kirill/Makefile new file mode 100644 index 0000000..864ba6c --- /dev/null +++ b/mine/lab_3/kirill/Makefile @@ -0,0 +1,55 @@ +# Компилятор и флаги +CC = gcc +CFLAGS = -Wall -Wextra -std=c99 -pedantic + +# Целевые файлы +TARGET_PARENT = parent +TARGET_LAB1 = task18 + +# Исходные файлы +SRC_PARENT = parent.c +SRC_LAB1 = task18.c + +# Объектные файлы +OBJ_PARENT = $(SRC_PARENT:.c=.o) +OBJ_LAB1 = $(SRC_LAB1:.c=.o) + +.PHONY: all clean help test + +all: $(TARGET_PARENT) $(TARGET_LAB1) + +$(TARGET_PARENT): $(OBJ_PARENT) + $(CC) $(CFLAGS) -o $@ $^ + @echo "Родительская программа собрана: $@" + +$(TARGET_LAB1): $(OBJ_LAB1) + $(CC) $(CFLAGS) -o $@ $^ + @echo "Программа лаб. работы №1 собрана: $@" + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(TARGET_PARENT) $(TARGET_LAB1) $(OBJ_PARENT) $(OBJ_LAB1) *.txt + @echo "Очистка завершена" + +help: + @echo "Доступные цели:" + @echo " all - собрать все" + @echo " clean - удалить все скомпилированные файлы" + @echo " test - запустить тестирование программы" + +test: all + @echo "Создаем тестовые файлы..." + @echo "22222222222222222222" > input1.txt + @echo "2222222222222222222" >> input1.txt + @echo "22222222222222222222" >> input1.txt + @echo "Test line one in second file" > input2.txt + @echo "Second line in second file" >> input2.txt + @echo "Third line in second file" >> input2.txt + @echo "Запуск parent..." + @./$(TARGET_PARENT) 5 input1.txt output1.txt input2.txt output2.txt + @echo "Содержимое output1.txt:" + @cat output1.txt || echo "Файл output1.txt не найден" + @echo "Содержимое output2.txt:" + @cat output2.txt || echo "Файл output2.txt не найден" diff --git a/mine/lab_3/kirill/parent.c b/mine/lab_3/kirill/parent.c new file mode 100644 index 0000000..5083535 --- /dev/null +++ b/mine/lab_3/kirill/parent.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + // Проверка аргументов командной строки + if (argc < 4) { + const char *usage = + "Usage: parent " + "[ ...]\n" + " max_replacements: максимальное количество замен\n" + " input/output: пары входных и выходных файлов\n"; + fprintf(stderr, "%s", usage); + return -1; + } + + // Проверка четности количества файлов (пары input/output) + if ((argc - 2) % 2 != 0) { + fprintf(stderr, "Error: количество файлов должно быть четным (пары input/output)\n"); + return -1; + } + + const char *max_replacements_arg = argv[1]; + int num_files = (argc - 2) / 2; // количество пар файлов + + printf("Родительский процесс: PID=%d\n", getpid()); + printf("Будет запущено %d дочерних процессов\n", num_files); + printf("Максимальное количество замен: %s\n\n", max_replacements_arg); + + // Массив для хранения PID дочерних процессов + pid_t *child_pids = malloc(num_files * sizeof(pid_t)); + if (child_pids == NULL) { + perror("malloc failed"); + return -1; + } + + // Запуск дочерних процессов + for (int i = 0; i < num_files; i++) { + const char *input_file = argv[2 + i * 2]; + const char *output_file = argv[2 + i * 2 + 1]; + + pid_t pid = fork(); + + if (pid < 0) { + // Ошибка при создании процесса + perror("fork failed"); + + // Ждем завершения уже запущенных процессов + for (int j = 0; j < i; j++) { + int status; + waitpid(child_pids[j], &status, 0); + } + + free(child_pids); + return -1; + + } else if (pid == 0) { + // Код дочернего процесса + printf("Дочерний процесс %d: PID=%d, обработка %s -> %s\n", + i + 1, getpid(), input_file, output_file); + + // Запуск программы из лабораторной работы №1 + execl("./task18", "task18", input_file, output_file, max_replacements_arg, (char *)NULL); + + // Если execl вернул управление, произошла ошибка + fprintf(stderr, "Дочерний процесс %d: execl failed для %s: %s\n", + i + 1, input_file, strerror(errno)); + _exit(-1); + + } else { + // Код родительского процесса + child_pids[i] = pid; + } + } + + // Ожидание завершения всех дочерних процессов + printf("\nРодительский процесс ожидает завершения дочерних процессов...\n\n"); + + for (int i = 0; i < num_files; i++) { + int status; + pid_t terminated_pid = waitpid(child_pids[i], &status, 0); + + if (terminated_pid < 0) { + perror("waitpid failed"); + continue; + } + + // Проверка статуса завершения + if (WIFEXITED(status)) { + int exit_code = WEXITSTATUS(status); + printf("Процесс %d (PID=%d) завершился нормально\n", + i + 1, terminated_pid); + + if (exit_code == 0) { + printf(" Код завершения: %d (успех)\n", exit_code); + } else { + printf(" Код завершения: %d (ошибка)\n", exit_code); + } + } else if (WIFSIGNALED(status)) { + int signal = WTERMSIG(status); + printf("Процесс %d (PID=%d) был прерван сигналом %d\n", + i + 1, terminated_pid, signal); + } else { + printf("Процесс %d (PID=%d) завершился с неизвестным статусом\n", + i + 1, terminated_pid); + } + } + + printf("\nВсе дочерние процессы завершены\n"); + + free(child_pids); + return 0; +} diff --git a/mine/lab_3/kirill/task18.c b/mine/lab_3/kirill/task18.c new file mode 100644 index 0000000..76976b6 --- /dev/null +++ b/mine/lab_3/kirill/task18.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include + +#define RBUFSZ 4096 +#define WBUFSZ 4096 + +static void die_perror(const char *what, const char *path, int exit_code) { + int saved = errno; + char msg[512]; + int n; + if (path) { + n = snprintf(msg, sizeof(msg), "%s: %s: %s\n", what, path, strerror(saved)); + } else { + n = snprintf(msg, sizeof(msg), "%s: %s\n", what, strerror(saved)); + } + if (n > 0) (void) write(STDERR_FILENO, msg, (size_t) n); + _exit(exit_code); +} + +static void xwrite_all(int fd, const char *buf, size_t len, const char *path) { + size_t off = 0; + while (off < len) { + ssize_t n = write(fd, buf + off, len - off); + if (n < 0) { + if (errno == EINTR) continue; + die_perror("write failed", path, -1); + } + off += (size_t) n; + } +} + +static long long parse_ll(const char *s) { + char *end = NULL; + errno = 0; + long long v = strtoll(s, &end, 10); + if (errno != 0 || end == s || *end != '\0' || v < 0) { + errno = EINVAL; + return -1; + } + return v; +} + +int main(int argc, char *argv[]) { + if (argc != 4) { + const char *usage = + "Usage: lab1_var_thirds_line \n" + "Replace every third non-newline byte in each line with a space; counter resets after LF.\n"; + (void) write(STDERR_FILENO, usage, strlen(usage)); + return -1; + } + + const char *in_path = argv[1]; + const char *out_path = argv[2]; + long long cap = parse_ll(argv[3]); + if (cap < 0) die_perror("invalid max_replacements", argv[3], -1); + + int in_fd = open(in_path, O_RDONLY); + if (in_fd < 0) die_perror("open input failed", in_path, -1); + + mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + int out_fd = open(out_path, O_CREAT | O_WRONLY | O_TRUNC, perms); + if (out_fd < 0) die_perror("open output failed", out_path, -1); + + char rbuf[RBUFSZ]; + char wbuf[WBUFSZ]; + size_t wlen = 0; + + long long total = 0; + long long col = 0; + int replacing_enabled = 1; + + for (;;) { + ssize_t n = read(in_fd, rbuf, sizeof(rbuf)); + if (n > 0) { + for (ssize_t i = 0; i < n; i++) { + unsigned char c = (unsigned char) rbuf[i]; + + if (c == '\n') { + if (wlen == sizeof(wbuf)) { + xwrite_all(out_fd, wbuf, wlen, out_path); + wlen = 0; + } + wbuf[wlen++] = '\n'; + col = 0; + continue; + } + + unsigned char outc = c; + + col++; + + if (replacing_enabled && (col % 3 == 0) && total < cap) { + outc = ' '; + total++; + if (total == cap) replacing_enabled = 0; + } + + if (wlen == sizeof(wbuf)) { + xwrite_all(out_fd, wbuf, wlen, out_path); + wlen = 0; + } + wbuf[wlen++] = (char) outc; + } + } else if (n == 0) { + if (wlen > 0) { + xwrite_all(out_fd, wbuf, wlen, out_path); + wlen = 0; + } + break; + } else { + if (errno == EINTR) continue; + die_perror("read failed", in_path, -1); + } + } + + if (close(in_fd) < 0) die_perror("close input failed", in_path, -1); + if (close(out_fd) < 0) die_perror("close output failed", out_path, -1); + + char res[64]; + int m = snprintf(res, sizeof(res), "%lld\n", total); + if (m > 0) (void) write(STDOUT_FILENO, res, (size_t) m); + + return 0; +} diff --git a/lab_3/lab1_var12.c b/mine/lab_3/lab1_var12.c similarity index 100% rename from lab_3/lab1_var12.c rename to mine/lab_3/lab1_var12.c diff --git a/lab_3/parent.c b/mine/lab_3/parent.c similarity index 100% rename from lab_3/parent.c rename to mine/lab_3/parent.c diff --git a/lab_3/vlad/Makefile b/mine/lab_3/vlad/Makefile similarity index 100% rename from lab_3/vlad/Makefile rename to mine/lab_3/vlad/Makefile diff --git a/lab_3/vlad/parent.c b/mine/lab_3/vlad/parent.c similarity index 100% rename from lab_3/vlad/parent.c rename to mine/lab_3/vlad/parent.c diff --git a/lab_3/vlad/task11.c b/mine/lab_3/vlad/task11.c similarity index 100% rename from lab_3/vlad/task11.c rename to mine/lab_3/vlad/task11.c diff --git a/lab_4/Makefile b/mine/lab_4/Makefile similarity index 100% rename from lab_4/Makefile rename to mine/lab_4/Makefile diff --git a/lab_4/fifo_client.c b/mine/lab_4/fifo_client.c similarity index 100% rename from lab_4/fifo_client.c rename to mine/lab_4/fifo_client.c diff --git a/lab_4/fifo_server.c b/mine/lab_4/fifo_server.c similarity index 100% rename from lab_4/fifo_server.c rename to mine/lab_4/fifo_server.c diff --git a/lab_4/vlad's/Makefile b/mine/lab_4/kirill's/Makefile similarity index 100% rename from lab_4/vlad's/Makefile rename to mine/lab_4/kirill's/Makefile diff --git a/lab_4/vlad's/fifo_client.c b/mine/lab_4/kirill's/fifo_client.c similarity index 100% rename from lab_4/vlad's/fifo_client.c rename to mine/lab_4/kirill's/fifo_client.c diff --git a/mine/lab_4/kirill's/fifo_server.c b/mine/lab_4/kirill's/fifo_server.c new file mode 100644 index 0000000..41b454d --- /dev/null +++ b/mine/lab_4/kirill's/fifo_server.c @@ -0,0 +1,183 @@ +// fifo_server.c - Серверная программа с использованием именованных каналов (FIFO) +// Вариант повышенной сложности для Lab 4 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FIFO_REQUEST "/tmp/fifo_request" +#define FIFO_RESPONSE "/tmp/fifo_response" +#define BUFFER_SIZE 4096 + +volatile sig_atomic_t running = 1; + +void signal_handler(int sig) { + (void)sig; + running = 0; +} + +static long long parse_ll(const char *s) { + char *end = NULL; + errno = 0; + long long v = strtoll(s, &end, 10); + if (errno != 0 || end == s || *end != '\0' || v < 0) { + return -1; + } + return v; +} + +long long process_data(const char *input, size_t input_len, char *output, + size_t output_size, long long max_replacements) { + long long total_replacements = 0; + long long col = 0; /* position in current line (counts non-newline bytes) */ + size_t out_pos = 0; + + for (size_t i = 0; i < input_len && out_pos < output_size - 1; i++) { + unsigned char c = (unsigned char)input[i]; + + if (c == '\n') { + /* write newline, reset column counter */ + output[out_pos++] = '\n'; + col = 0; + continue; + } + + col++; + unsigned char outc = c; + + if ((col % 3) == 0 && total_replacements < max_replacements) { + outc = ' '; + total_replacements++; + } + + output[out_pos++] = (char)outc; + } + + /* If there is space, terminate string */ + if (out_pos < output_size) + output[out_pos] = '\0'; + else if (output_size > 0) + output[output_size - 1] = '\0'; + + return total_replacements; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + long long max_replacements = parse_ll(argv[1]); + if (max_replacements < 0) { + fprintf(stderr, "ERROR: Invalid max_replacements\n"); + return 1; + } + + printf("=== FIFO Server запущен ===\n"); + printf("Server PID: %d\n", getpid()); + printf("Максимум замен: %lld\n", max_replacements); + + // Устанавливаем обработчик сигналов + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + + // Удаляем старые FIFO, если существуют + unlink(FIFO_REQUEST); + unlink(FIFO_RESPONSE); + + // Создаем именованные каналы + if (mkfifo(FIFO_REQUEST, 0666) == -1) { + perror("mkfifo request"); + return 1; + } + if (mkfifo(FIFO_RESPONSE, 0666) == -1) { + perror("mkfifo response"); + unlink(FIFO_REQUEST); + return 1; + } + + printf("FIFO каналы созданы:\n"); + printf(" Request: %s\n", FIFO_REQUEST); + printf(" Response: %s\n", FIFO_RESPONSE); + printf("Ожидание запросов от клиентов...\n\n"); + + while (running) { + // Открываем FIFO для чтения запроса + int fd_req = open(FIFO_REQUEST, O_RDONLY); + if (fd_req == -1) { + if (errno == EINTR) continue; + perror("open request FIFO"); + break; + } + + // Читаем данные от клиента + char *input_buffer = malloc(BUFFER_SIZE); + char *output_buffer = malloc(BUFFER_SIZE); + + if (!input_buffer || !output_buffer) { + fprintf(stderr, "ERROR: Memory allocation failed\n"); + close(fd_req); + free(input_buffer); + free(output_buffer); + continue; + } + + ssize_t bytes_read = read(fd_req, input_buffer, BUFFER_SIZE - 1); + close(fd_req); + + if (bytes_read <= 0) { + free(input_buffer); + free(output_buffer); + continue; + } + + input_buffer[bytes_read] = '\0'; + printf("Получен запрос: %zd байт\n", bytes_read); + + // Обрабатываем данные + long long replacements = process_data(input_buffer, bytes_read, + output_buffer, BUFFER_SIZE, + max_replacements); + + printf("Выполнено замен: %lld\n", replacements); + + // Открываем FIFO для отправки ответа + int fd_resp = open(FIFO_RESPONSE, O_WRONLY); + if (fd_resp == -1) { + perror("open response FIFO"); + free(input_buffer); + free(output_buffer); + continue; + } + + // Отправляем обработанные данные + size_t output_len = strlen(output_buffer); + ssize_t bytes_written = write(fd_resp, output_buffer, output_len); + + // Отправляем количество замен (в отдельной строке) + char result[64]; + snprintf(result, sizeof(result), "\nREPLACEMENTS:%lld\n", replacements); + write(fd_resp, result, strlen(result)); + + close(fd_resp); + + printf("Отправлен ответ: %zd байт\n\n", bytes_written); + + free(input_buffer); + free(output_buffer); + } + + // Очистка + printf("\nЗавершение работы сервера...\n"); + unlink(FIFO_REQUEST); + unlink(FIFO_RESPONSE); + + return 0; +} diff --git a/mine/lab_4/vlad's/Makefile b/mine/lab_4/vlad's/Makefile new file mode 100644 index 0000000..c6f4dd3 --- /dev/null +++ b/mine/lab_4/vlad's/Makefile @@ -0,0 +1,99 @@ +# Makefile for lab 4 - FIFO (named pipes) only + +CC = gcc +CFLAGS = -Wall -Wextra -std=c99 -g + +# Default target - build FIFO programs +all: fifo + +# ===== FIFO targets ===== +fifo: fifo_server fifo_client + +fifo_server: fifo_server.c + $(CC) $(CFLAGS) -o $@ $< + +fifo_client: fifo_client.c + $(CC) $(CFLAGS) -o $@ $< + +# ===== Test files ===== +test_files: + @echo "Создание тестовых файлов..." + echo "abbaabbaabbaabbaabbaabbaabbaabba" > input1.txt + echo "xyzxyzxyzxyzxyzxyzxyzxyz" >> input1.txt + echo "hello world hello" >> input1.txt + echo "testtest" > input2.txt + echo "aaaaaaa" >> input2.txt + echo "programming" > input3.txt + echo "ppppython" >> input3.txt + +# ===== FIFO tests ===== +test_fifo_server: fifo test_files + @echo "=== Запуск FIFO сервера ===" + @echo "В другом терминале выполните: make test_fifo_client" + ./fifo_server 10 + +test_fifo_client: fifo test_files + @echo "=== Запуск FIFO клиента ===" & \ + ./fifo_client input1.txt output1_fifo.txt; \ + ./fifo_client input2.txt output2_fifo.txt; \ + ./fifo_client input3.txt output3_fifo.txt; + @echo "\n=== Результаты FIFO ===" + @echo "--- output1_fifo.txt ---" + @cat output1_fifo.txt || true + @echo "\n--- output2_fifo.txt ---" + @cat output2_fifo.txt || true + @echo "\n--- output3_fifo.txt ---" + @cat output3_fifo.txt || true + +# Automatic FIFO test (server in background) +test_all: fifo test_files + @echo "=== Автоматический тест FIFO ===" + @./fifo_server 10 & \ + SERVER_PID=$$!; \ + sleep 1; \ + ./fifo_client input1.txt output1_fifo.txt; \ + ./fifo_client input2.txt output2_fifo.txt; \ + ./fifo_client input3.txt output3_fifo.txt; \ + kill $$SERVER_PID 2>/dev/null || true; \ + wait $$SERVER_PID 2>/dev/null || true + @echo "\n=== Результаты FIFO ===" + @echo "--- output1_fifo.txt ---" + @cat output1_fifo.txt || true + @echo "\n--- output2_fifo.txt ---" + @cat output2_fifo.txt || true + @echo "\n--- output3_fifo.txt ---" + @cat output3_fifo.txt || true + +# Error handling test for FIFO +test_error: fifo + @echo "\n=== Тест обработки ошибки (несуществующий файл) - FIFO ===" + @./fifo_server 5 & \ + SERVER_PID=$$!; \ + sleep 1; \ + ./fifo_client nonexistent.txt output_error.txt || true; \ + kill $$SERVER_PID 2>/dev/null || true; \ + wait $$SERVER_PID 2>/dev/null || true + +# Cleanup +clean: + @echo "Очистка..." + rm -f fifo_server fifo_client + rm -f input1.txt input2.txt input3.txt + rm -f output*.txt + rm -f /tmp/fifo_request /tmp/fifo_response + +# Help +help: + @echo "Доступные цели:" + @echo " all - Скомпилировать FIFO программы" + @echo " fifo - Скомпилировать fifo_server и fifo_client" + @echo " test_files - Создать тестовые входные файлы" + @echo " test_fifo_server - Запустить FIFO сервер (использовать с клиентом в другом терминале)" + @echo " test_fifo_client - Запустить FIFO клиент и показать результат" + @echo " test_fifo_auto - Автоматический тест FIFO (сервер в фоне)" + @echo " test_all - Запустить все тесты (FIFO)" + @echo " test_error_fifo - Тест обработки ошибок (несуществующий файл)" + @echo " clean - Удалить скомпилированные файлы и тесты" + +.PHONY: all fifo test_files test_fifo_server test_fifo_client test_all \ + test_error clean help diff --git a/mine/lab_4/vlad's/fifo_client.c b/mine/lab_4/vlad's/fifo_client.c new file mode 100644 index 0000000..9abe125 --- /dev/null +++ b/mine/lab_4/vlad's/fifo_client.c @@ -0,0 +1,144 @@ +// fifo_client.c - Клиентская программа с использованием именованных каналов (FIFO) + +#include +#include +#include +#include +#include +#include +#include +#include + +#define FIFO_REQUEST "/tmp/fifo_request" +#define FIFO_RESPONSE "/tmp/fifo_response" +#define BUFFER_SIZE 4096 + +void print_usage(const char *progname) { + fprintf(stderr, "Usage: %s \n", progname); + fprintf(stderr, "Example: %s input.txt output.txt\n", progname); +} + +int main(int argc, char *argv[]) { + if (argc != 3) { + fprintf(stderr, "ERROR: Неверное количество аргументов\n"); + print_usage(argv[0]); + return 1; + } + + const char *input_file = argv[1]; + const char *output_file = argv[2]; + + printf("=== FIFO Client ===\n"); + printf("Client PID: %d\n", getpid()); + printf("Входной файл: %s\n", input_file); + printf("Выходной файл: %s\n", output_file); + + // Открываем входной файл + int in_fd = open(input_file, O_RDONLY); + if (in_fd < 0) { + fprintf(stderr, "ERROR: Не удалось открыть входной файл %s: %s\n", + input_file, strerror(errno)); + return 1; + } + + // Читаем данные из файла + char *buffer = malloc(BUFFER_SIZE); + if (!buffer) { + fprintf(stderr, "ERROR: Не удалось выделить память\n"); + close(in_fd); + return 1; + } + + ssize_t bytes_read = read(in_fd, buffer, BUFFER_SIZE - 1); + close(in_fd); + + if (bytes_read < 0) { + fprintf(stderr, "ERROR: Не удалось прочитать файл: %s\n", strerror(errno)); + free(buffer); + return 1; + } + + buffer[bytes_read] = '\0'; + printf("Прочитано байт из файла: %zd\n", bytes_read); + + // Открываем FIFO для отправки запроса + printf("Отправка запроса серверу...\n"); + int fd_req = open(FIFO_REQUEST, O_WRONLY); + if (fd_req == -1) { + fprintf(stderr, "ERROR: Не удалось открыть FIFO запроса: %s\n", strerror(errno)); + fprintf(stderr, "Убедитесь, что сервер запущен!\n"); + free(buffer); + return 1; + } + + // Отправляем данные серверу + ssize_t bytes_written = write(fd_req, buffer, bytes_read); + close(fd_req); + + if (bytes_written != bytes_read) { + fprintf(stderr, "ERROR: Ошибка отправки данных\n"); + free(buffer); + return 1; + } + + printf("Отправлено байт: %zd\n", bytes_written); + + // Открываем FIFO для получения ответа + printf("Ожидание ответа от сервера...\n"); + int fd_resp = open(FIFO_RESPONSE, O_RDONLY); + if (fd_resp == -1) { + fprintf(stderr, "ERROR: Не удалось открыть FIFO ответа: %s\n", strerror(errno)); + free(buffer); + return 1; + } + + // Читаем обработанные данные + ssize_t response_bytes = read(fd_resp, buffer, BUFFER_SIZE - 1); + close(fd_resp); + + if (response_bytes < 0) { + fprintf(stderr, "ERROR: Ошибка чтения ответа\n"); + free(buffer); + return 1; + } + + buffer[response_bytes] = '\0'; + printf("Получено байт от сервера: %zd\n", response_bytes); + + // Ищем информацию о количестве замен + char *replacements_info = strstr(buffer, "\nREPLACEMENTS:"); + long long replacements = 0; + + if (replacements_info) { + sscanf(replacements_info, "\nREPLACEMENTS:%lld", &replacements); + *replacements_info = '\0'; // Обрезаем служебную информацию + response_bytes = replacements_info - buffer; + } + + // Открываем выходной файл + int out_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + if (out_fd < 0) { + fprintf(stderr, "ERROR: Не удалось открыть выходной файл %s: %s\n", + output_file, strerror(errno)); + free(buffer); + return 1; + } + + // Записываем обработанные данные + ssize_t written = write(out_fd, buffer, response_bytes); + close(out_fd); + + if (written != response_bytes) { + fprintf(stderr, "ERROR: Ошибка записи в выходной файл\n"); + free(buffer); + return 1; + } + + printf("Записано байт в выходной файл: %zd\n", written); + printf("Количество выполненных замен: %lld\n", replacements); + printf("\nОбработка завершена успешно!\n"); + + free(buffer); + return 0; +} diff --git a/lab_4/vlad's/fifo_server.c b/mine/lab_4/vlad's/fifo_server.c similarity index 100% rename from lab_4/vlad's/fifo_server.c rename to mine/lab_4/vlad's/fifo_server.c diff --git a/lab_5/Makefile b/mine/lab_5/Makefile similarity index 100% rename from lab_5/Makefile rename to mine/lab_5/Makefile diff --git a/lab_5/common.h b/mine/lab_5/common.h similarity index 100% rename from lab_5/common.h rename to mine/lab_5/common.h diff --git a/lab_5/server.c b/mine/lab_5/server.c similarity index 100% rename from lab_5/server.c rename to mine/lab_5/server.c diff --git a/lab_5/worker.c b/mine/lab_5/worker.c similarity index 100% rename from lab_5/worker.c rename to mine/lab_5/worker.c diff --git a/lab_6/Makefile b/mine/lab_6/Makefile similarity index 100% rename from lab_6/Makefile rename to mine/lab_6/Makefile diff --git a/lab_6/client.c b/mine/lab_6/client.c similarity index 100% rename from lab_6/client.c rename to mine/lab_6/client.c diff --git a/lab_6/kirill/input.txt b/mine/lab_6/input.txt similarity index 100% rename from lab_6/kirill/input.txt rename to mine/lab_6/input.txt diff --git a/lab_6/vlad/Makefile b/mine/lab_6/kirill/Makefile similarity index 100% rename from lab_6/vlad/Makefile rename to mine/lab_6/kirill/Makefile diff --git a/mine/lab_6/kirill/client.c b/mine/lab_6/kirill/client.c new file mode 100644 index 0000000..7e34809 --- /dev/null +++ b/mine/lab_6/kirill/client.c @@ -0,0 +1,141 @@ +// client.c + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHM_BUFFER_SIZE 1024 + +typedef struct { + int has_data; + int result_code; + char buffer[SHM_BUFFER_SIZE]; +} shared_block_t; + +int main(int argc, char *argv[]) { + if (argc < 4) { + fprintf(stderr, + "Usage: %s \n", + argv[0]); + return -1; + } + + const char *shm_name = argv[1]; + const char *sem_client_name = argv[2]; + const char *sem_server_name = argv[3]; + + int shm_fd = shm_open(shm_name, O_RDWR, 0); + if (shm_fd == -1) { + perror("shm_open"); + return -1; + } + + shared_block_t *shm_ptr = mmap(NULL, + sizeof(shared_block_t), + PROT_READ | PROT_WRITE, + MAP_SHARED, + shm_fd, + 0); + if (shm_ptr == MAP_FAILED) { + perror("mmap"); + close(shm_fd); + return -1; + } + + if (close(shm_fd) == -1) { + perror("close"); + } + + sem_t *sem_client = sem_open(sem_client_name, 0); + if (sem_client == SEM_FAILED) { + perror("sem_open(sem_client)"); + munmap(shm_ptr, sizeof(shared_block_t)); + return -1; + } + + sem_t *sem_server = sem_open(sem_server_name, 0); + if (sem_server == SEM_FAILED) { + perror("sem_open(sem_server)"); + sem_close(sem_client); + munmap(shm_ptr, sizeof(shared_block_t)); + return -1; + } + + FILE *fin = fopen("input.txt", "r"); + if (!fin) { + perror("fopen(input.txt)"); + sem_close(sem_client); + sem_close(sem_server); + munmap(shm_ptr, sizeof(shared_block_t)); + return -1; + } + + char input[SHM_BUFFER_SIZE]; + + while (fgets(input, sizeof(input), fin) != NULL) { + size_t len = strlen(input); + if (len > 0 && input[len - 1] == '\n') { + input[len - 1] = '\0'; + } + + memset(shm_ptr->buffer, 0, sizeof(shm_ptr->buffer)); + strncpy(shm_ptr->buffer, input, SHM_BUFFER_SIZE - 1); + shm_ptr->buffer[SHM_BUFFER_SIZE - 1] = '\0'; + + shm_ptr->has_data = 1; + + if (sem_post(sem_client) == -1) { + perror("sem_post(sem_client)"); + fclose(fin); + sem_close(sem_client); + sem_close(sem_server); + munmap(shm_ptr, sizeof(shared_block_t)); + return -1; + } + + if (sem_wait(sem_server) == -1) { + perror("sem_wait(sem_server)"); + fclose(fin); + sem_close(sem_client); + sem_close(sem_server); + munmap(shm_ptr, sizeof(shared_block_t)); + return -1; + } + + if (shm_ptr->result_code != 0) { + fprintf(stderr, + "Server reported error, result_code = %d\n", + shm_ptr->result_code); + fclose(fin); + sem_close(sem_client); + sem_close(sem_server); + munmap(shm_ptr, sizeof(shared_block_t)); + return -1; + } + + printf("%s\n", shm_ptr->buffer); + } + + if (fclose(fin) == EOF) { + perror("fclose(input.txt)"); + } + + if (sem_close(sem_client) == -1) { + perror("sem_close(sem_client)"); + } + if (sem_close(sem_server) == -1) { + perror("sem_close(sem_server)"); + } + + if (munmap(shm_ptr, sizeof(shared_block_t)) == -1) { + perror("munmap"); + } + + return 0; +} diff --git a/lab_6/vlad/input.txt b/mine/lab_6/kirill/input.txt similarity index 100% rename from lab_6/vlad/input.txt rename to mine/lab_6/kirill/input.txt diff --git a/mine/lab_6/kirill/server.c b/mine/lab_6/kirill/server.c new file mode 100644 index 0000000..fe5be65 --- /dev/null +++ b/mine/lab_6/kirill/server.c @@ -0,0 +1,185 @@ +// server.c + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHM_BUFFER_SIZE 1024 + +typedef struct { + int has_data; + int result_code; + char buffer[SHM_BUFFER_SIZE]; +} shared_block_t; + +static void process_line(char *s) { + if (!s) return; + for (size_t i = 0; s[i] != '\0'; ++i) { + if ((i + 1) % 3 == 0) { + s[i] = ' '; + } + } +} + +int main(int argc, char *argv[]) { + if (argc < 4) { + fprintf(stderr, + "Usage: %s [iterations]\n", + argv[0]); + return -1; + } + + const char *shm_name = argv[1]; + const char *sem_client_name = argv[2]; + const char *sem_server_name = argv[3]; + + int iterations = -1; + if (argc >= 5) { + char *endptr = NULL; + unsigned long tmp = strtoul(argv[4], &endptr, 10); + if (endptr == argv[4] || *endptr != '\0') { + fprintf(stderr, "Invalid iterations value: '%s'\n", argv[4]); + return -1; + } + iterations = (int) tmp; + } + + shm_unlink(shm_name); + sem_unlink(sem_client_name); + sem_unlink(sem_server_name); + + int shm_fd = shm_open(shm_name, + O_CREAT | O_EXCL | O_RDWR, + S_IRUSR | S_IWUSR); + if (shm_fd == -1) { + perror("shm_open"); + return -1; + } + + if (ftruncate(shm_fd, sizeof(shared_block_t)) == -1) { + perror("ftruncate"); + close(shm_fd); + shm_unlink(shm_name); + return -1; + } + + shared_block_t *shm_ptr = mmap(NULL, + sizeof(shared_block_t), + PROT_READ | PROT_WRITE, + MAP_SHARED, + shm_fd, + 0); + if (shm_ptr == MAP_FAILED) { + perror("mmap"); + close(shm_fd); + shm_unlink(shm_name); + return -1; + } + + if (close(shm_fd) == -1) { + perror("close"); + } + + shm_ptr->has_data = 0; + shm_ptr->result_code = 0; + memset(shm_ptr->buffer, 0, sizeof(shm_ptr->buffer)); + + sem_t *sem_client = sem_open(sem_client_name, + O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR, + 0); + if (sem_client == SEM_FAILED) { + perror("sem_open(sem_client)"); + munmap(shm_ptr, sizeof(shared_block_t)); + shm_unlink(shm_name); + return -1; + } + + sem_t *sem_server = sem_open(sem_server_name, + O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR, + 0); + if (sem_server == SEM_FAILED) { + perror("sem_open(sem_server)"); + sem_close(sem_client); + sem_unlink(sem_client_name); + munmap(shm_ptr, sizeof(shared_block_t)); + shm_unlink(shm_name); + return -1; + } + + FILE *fout = fopen("output.txt", "w"); + if (!fout) { + perror("fopen(output.txt)"); + } + + int processed_count = 0; + + while (iterations < 0 || processed_count < iterations) { + if (sem_wait(sem_client) == -1) { + perror("sem_wait(sem_client)"); + processed_count = -1; + break; + } + + if (!shm_ptr->has_data) { + fprintf(stderr, "Warning: sem_client posted, but has_data == 0\n"); + shm_ptr->result_code = -1; + } else { + process_line(shm_ptr->buffer); + shm_ptr->result_code = 0; + shm_ptr->has_data = 0; + processed_count++; + + if (fout) { + fprintf(fout, "%s\n", shm_ptr->buffer); + fflush(fout); + } + } + + if (sem_post(sem_server) == -1) { + perror("sem_post(sem_server)"); + processed_count = -1; + break; + } + } + + if (fout && fclose(fout) == EOF) { + perror("fclose(output.txt)"); + } + + if (processed_count >= 0) { + printf("%d\n", processed_count); + } else { + printf("-1\n"); + } + + if (sem_close(sem_client) == -1) { + perror("sem_close(sem_client)"); + } + if (sem_close(sem_server) == -1) { + perror("sem_close(sem_server)"); + } + + if (sem_unlink(sem_client_name) == -1) { + perror("sem_unlink(sem_client)"); + } + if (sem_unlink(sem_server_name) == -1) { + perror("sem_unlink(sem_server)"); + } + + if (munmap(shm_ptr, sizeof(shared_block_t)) == -1) { + perror("munmap"); + } + if (shm_unlink(shm_name) == -1) { + perror("shm_unlink"); + } + + return 0; +} diff --git a/lab_6/server.c b/mine/lab_6/server.c similarity index 100% rename from lab_6/server.c rename to mine/lab_6/server.c diff --git a/mine/lab_6/vlad/Makefile b/mine/lab_6/vlad/Makefile new file mode 100644 index 0000000..6eefc14 --- /dev/null +++ b/mine/lab_6/vlad/Makefile @@ -0,0 +1,51 @@ +CC = gcc +CFLAGS = -Wall -Wextra -std=c99 -g +LDFLAGS_IPC = -lrt -pthread + +SHM_NAME ?= /myshm +SEM_CLIENT_NAME ?= /sem_client +SEM_SERVER_NAME ?= /sem_server +SERVER_ITERS ?= 1000 + +all: shm + +shm: server client + +server: server.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS_IPC) + +client: client.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS_IPC) + +test_server: shm + @echo "=== Запуск сервера POSIX SHM+SEM ===" + @echo "В другом терминале выполните: make test_client" + ./server $(SHM_NAME) $(SEM_CLIENT_NAME) $(SEM_SERVER_NAME) $(SERVER_ITERS) + +test_client: shm + @echo "=== Запуск клиента, чтение input.txt, вывод на stdout ===" + ./client $(SHM_NAME) $(SEM_CLIENT_NAME) $(SEM_SERVER_NAME) + +test_all: shm + @echo "=== Автотест POSIX SHM+SEM с файлами input.txt/output.txt ===" + ./server $(SHM_NAME) $(SEM_CLIENT_NAME) $(SEM_SERVER_NAME) $(SERVER_ITERS) & \ + SRV=$$!; \ + sleep 1; \ + ./client $(SHM_NAME) $(SEM_CLIENT_NAME) $(SEM_SERVER_NAME); \ + wait $$SRV + +clean: + @echo "Очистка..." + rm -f server client + rm -f output.txt + +help: + @echo "Available targets:" + @echo " shm - Build POSIX SHM+SEM programs" + @echo " test_server - Run SHM+SEM server (client in another terminal)" + @echo " test_client - Run client reading input.txt" + @echo " test_all - Automatic end-to-end test with input.txt/output.txt" + @echo " clean - Remove built files" + @echo " help - Show this help" + +.PHONY: all shm test_server test_client test_all clean help diff --git a/lab_6/vlad/client.c b/mine/lab_6/vlad/client.c similarity index 100% rename from lab_6/vlad/client.c rename to mine/lab_6/vlad/client.c diff --git a/mine/lab_6/vlad/input.txt b/mine/lab_6/vlad/input.txt new file mode 100644 index 0000000..c640816 --- /dev/null +++ b/mine/lab_6/vlad/input.txt @@ -0,0 +1,6 @@ +abacaba +xxxxxx +hello +aaaaa +1abc1d1e1 +qwerty diff --git a/lab_6/vlad/server.c b/mine/lab_6/vlad/server.c similarity index 100% rename from lab_6/vlad/server.c rename to mine/lab_6/vlad/server.c diff --git a/lab_7/Makefile b/mine/lab_7/Makefile similarity index 100% rename from lab_7/Makefile rename to mine/lab_7/Makefile diff --git a/mine/lab_7/kirill/Makefile b/mine/lab_7/kirill/Makefile new file mode 100644 index 0000000..08031b7 --- /dev/null +++ b/mine/lab_7/kirill/Makefile @@ -0,0 +1,43 @@ +CC = gcc +CFLAGS = -Wall -Wextra -std=c11 -g -pthread + +MAX_REPL ?= 100 +INPUT1 ?= in1.txt +OUTPUT1 ?= out1.txt +INPUT2 ?= in2.txt +OUTPUT2 ?= out2.txt + +all: threads_third + +threads_third: main.c + $(CC) $(CFLAGS) -o $@ $< + +test_two: threads_third + @echo "=== Тест с двумя файлами ===" + @printf "abcdefghijk\n" > $(INPUT1) + @printf "xxxxxxxxxx\n" > $(INPUT2) + ./threads_third $(MAX_REPL) $(INPUT1) $(OUTPUT1) $(INPUT2) $(OUTPUT2) + @echo "--- $(INPUT1) -> $(OUTPUT1) ---" + @cat $(INPUT1) + @echo "-----" + @cat $(OUTPUT1) + @echo + @echo "--- $(INPUT2) -> $(OUTPUT2) ---" + @cat $(INPUT2) + @echo "-----" + @cat $(OUTPUT2) + + +clean: + @echo "Очистка..." + rm -f threads_third + rm -f in1.txt out1.txt in2.txt out2.txt + +help: + @echo "Available targets:" + @echo " all - build threads_third" + @echo " test_two - run two-file multithread test" + @echo " clean - remove binaries and test files" + @echo " help - show this help" + +.PHONY: all test_one test_two test_all clean help diff --git a/mine/lab_7/kirill/main.c b/mine/lab_7/kirill/main.c new file mode 100644 index 0000000..b40952f --- /dev/null +++ b/mine/lab_7/kirill/main.c @@ -0,0 +1,276 @@ +// threads_third.c + +#include +#include +#include +#include +#include +#include +#include +#include + +#define RBUFSZ 4096 +#define WBUFSZ 4096 +#define MAX_THREADS 100 + +typedef struct { + const char *input_path; + const char *output_path; + long long max_repl; + int thread_index; + int result; +} ThreadTask; + +static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void die_perror_thread(const char *what, + const char *path, + int exit_code) { + int saved = errno; + char msg[512]; + + if (path) { + snprintf(msg, sizeof(msg), + "%s: %s: %s\n", what, path, strerror(saved)); + } else { + snprintf(msg, sizeof(msg), + "%s: %s\n", what, strerror(saved)); + } + + pthread_mutex_lock(&log_mutex); + write(STDERR_FILENO, msg, strlen(msg)); + pthread_mutex_unlock(&log_mutex); + + (void) exit_code; +} + +static long long parse_ll(const char *s) { + char *end = NULL; + errno = 0; + long long v = strtoll(s, &end, 10); + if (errno != 0 || end == s || *end != '\0' || v < 0) { + errno = EINVAL; + return -1; + } + return v; +} + +static int process_file_third(const char *in_path, + const char *out_path, + long long cap) { + int in_fd = open(in_path, O_RDONLY); + if (in_fd < 0) { + die_perror_thread("open input failed", in_path, -1); + return -1; + } + + mode_t perms = S_IRUSR | S_IWUSR | + S_IRGRP | S_IWGRP | + S_IROTH | S_IWOTH; + + int out_fd = open(out_path, + O_CREAT | O_WRONLY | O_TRUNC, + perms); + if (out_fd < 0) { + die_perror_thread("open output failed", out_path, -1); + close(in_fd); + return -1; + } + + char rbuf[RBUFSZ]; + char wbuf[WBUFSZ]; + size_t wlen = 0; + + long long total_replacements = 0; + long long pos_in_file = 0; + + for (;;) { + ssize_t n = read(in_fd, rbuf, sizeof(rbuf)); + if (n > 0) { + for (ssize_t i = 0; i < n; i++, pos_in_file++) { + unsigned char c = (unsigned char) rbuf[i]; + unsigned char outc = c; + + if ((pos_in_file + 1) % 3 == 0 && total_replacements < cap) { + outc = ' '; + total_replacements++; + } + + if (wlen == sizeof(wbuf)) { + ssize_t wrote = write(out_fd, wbuf, wlen); + if (wrote < 0 || (size_t) wrote != wlen) { + die_perror_thread("write failed", out_path, -1); + close(in_fd); + close(out_fd); + return -1; + } + wlen = 0; + } + + wbuf[wlen++] = (char) outc; + } + } else if (n == 0) { + if (wlen > 0) { + ssize_t wrote = write(out_fd, wbuf, wlen); + if (wrote < 0 || (size_t) wrote != wlen) { + die_perror_thread("write failed", out_path, -1); + close(in_fd); + close(out_fd); + return -1; + } + wlen = 0; + } + break; + } else { + die_perror_thread("read failed", in_path, -1); + close(in_fd); + close(out_fd); + return -1; + } + } + + if (close(in_fd) < 0) { + die_perror_thread("close input failed", in_path, -1); + close(out_fd); + return -1; + } + + if (close(out_fd) < 0) { + die_perror_thread("close output failed", out_path, -1); + return -1; + } + + pthread_mutex_lock(&log_mutex); + char res[64]; + int m = snprintf(res, sizeof(res), "%lld\n", total_replacements); + if (m > 0) { + write(STDOUT_FILENO, res, (size_t) m); + } + pthread_mutex_unlock(&log_mutex); + + return 0; +} + +static void *thread_func(void *arg) { + ThreadTask *task = (ThreadTask *) arg; + + pthread_mutex_lock(&log_mutex); + printf("[thread %d] start: %s -> %s, max_repl=%lld\n", + task->thread_index, + task->input_path, + task->output_path, + task->max_repl); + pthread_mutex_unlock(&log_mutex); + + int rc = process_file_third(task->input_path, + task->output_path, + task->max_repl); + + task->result = rc; + + pthread_mutex_lock(&log_mutex); + printf("[thread %d] finished with code %d\n", + task->thread_index, + task->result); + pthread_mutex_unlock(&log_mutex); + + return NULL; +} + +static void print_usage(const char *progname) { + fprintf(stderr, + "Usage: %s [ ...]\n", + progname); + fprintf(stderr, + "Example: %s 100 in1.txt out1.txt in2.txt out2.txt\n", + progname); +} + +int main(int argc, char *argv[]) { + if (argc < 4 || ((argc - 2) % 2) != 0) { + fprintf(stderr, + "ERROR: Недостаточное или неверное количество аргументов\n"); + print_usage(argv[0]); + return -1; + } + + long long cap = parse_ll(argv[1]); + if (cap < 0) { + die_perror_thread("invalid max_replacements", argv[1], -1); + return -1; + } + + int num_files = (argc - 2) / 2; + if (num_files > MAX_THREADS) { + fprintf(stderr, + "ERROR: Слишком много файлов (максимум %d пар)\n", + MAX_THREADS); + return -1; + } + + printf("=== Многопоточная обработка: каждый третий символ -> пробел ===\n"); + printf("Главный поток TID: %lu\n", (unsigned long) pthread_self()); + printf("Максимум замен на файл: %lld\n", cap); + printf("Количество файловых пар: %d\n\n", num_files); + + pthread_t threads[MAX_THREADS]; + ThreadTask tasks[MAX_THREADS]; + + for (int i = 0; i < num_files; i++) { + const char *input_path = argv[2 + i * 2]; + const char *output_path = argv[3 + i * 2]; + + tasks[i].input_path = input_path; + tasks[i].output_path = output_path; + tasks[i].max_repl = cap; + tasks[i].thread_index = i + 1; + tasks[i].result = -1; + + int rc = pthread_create(&threads[i], + NULL, + thread_func, + &tasks[i]); + if (rc != 0) { + errno = rc; + die_perror_thread("pthread_create failed", NULL, -1); + tasks[i].result = -1; + } + } + + int success_count = 0; + int error_count = 0; + + for (int i = 0; i < num_files; i++) { + if (!threads[i]) { + error_count++; + continue; + } + + int rc = pthread_join(threads[i], NULL); + if (rc != 0) { + errno = rc; + die_perror_thread("pthread_join failed", NULL, -1); + error_count++; + continue; + } + + if (tasks[i].result == 0) { + success_count++; + } else { + error_count++; + } + } + + printf("\n=== Итоговая статистика ===\n"); + printf("Всего потоков: %d\n", num_files); + printf("Успешно завершено: %d\n", success_count); + printf("С ошибкой: %d\n", error_count); + + if (error_count > 0) { + printf("\nОБЩИЙ СТАТУС: Завершено с ошибками\n"); + return -1; + } else { + printf("\nОБЩИЙ СТАТУС: Все потоки завершены успешно\n"); + return 0; + } +} diff --git a/lab_7/threads_var12.c b/mine/lab_7/threads_var12.c similarity index 100% rename from lab_7/threads_var12.c rename to mine/lab_7/threads_var12.c diff --git a/lab_7/vlad/Makefile b/mine/lab_7/vlad/Makefile similarity index 100% rename from lab_7/vlad/Makefile rename to mine/lab_7/vlad/Makefile diff --git a/lab_7/vlad/main.c b/mine/lab_7/vlad/main.c similarity index 100% rename from lab_7/vlad/main.c rename to mine/lab_7/vlad/main.c diff --git a/lab_8/Makefile b/mine/lab_8/Makefile similarity index 100% rename from lab_8/Makefile rename to mine/lab_8/Makefile diff --git a/lab_8/client.c b/mine/lab_8/client.c similarity index 100% rename from lab_8/client.c rename to mine/lab_8/client.c diff --git a/mine/lab_8/kirill/Makefile b/mine/lab_8/kirill/Makefile new file mode 100644 index 0000000..96ea315 --- /dev/null +++ b/mine/lab_8/kirill/Makefile @@ -0,0 +1,50 @@ +CC = gcc +CFLAGS = -Wall -Wextra -std=c11 -g + +all: server_udp_third client_udp_third + +server_udp_third: server.c + $(CC) $(CFLAGS) -o $@ $< + +client_udp_third: client.c + $(CC) $(CFLAGS) -o $@ $< + +test_server: server_udp_third + @echo "=== Запуск UDP-сервера (каждый третий символ -> пробел) ===" + @echo "Выходной файл: out.txt" + ./server_udp_third 5000 out.txt + +test_client: client_udp_third + @echo "=== Запуск UDP-клиента ===" + @echo "Создаём input.txt" + @printf "abcdefghi\n1234567890\n" > input.txt + ./client_udp_third 127.0.0.1 5000 input.txt + +test_all: all + @echo "=== Автотест UDP (каждый третий символ -> пробел) ===" + @printf "abcdefghi\n1234567890\n" > input.txt + ./server_udp_third 5000 out.txt & \ + SRV=$$!; \ + sleep 1; \ + ./client_udp_third 127.0.0.1 5000 input.txt; \ + wait $$SRV; \ + echo "--- input.txt ---"; \ + cat input.txt; \ + echo "--- out.txt ---"; \ + cat out.txt + +clean: + @echo "Очистка..." + rm -f server_udp_third client_udp_third + rm -f input.txt out.txt + +help: + @echo "Targets:" + @echo " all - build server_udp_third and client_udp_third" + @echo " test_server - run server (port 5000, out.txt)" + @echo " test_client - run client to send input.txt" + @echo " test_all - end-to-end UDP test" + @echo " clean - remove binaries and test files" + @echo " help - show this help" + +.PHONY: all test_server test_client test_all clean help diff --git a/mine/lab_8/kirill/client.c b/mine/lab_8/kirill/client.c new file mode 100644 index 0000000..10706ea --- /dev/null +++ b/mine/lab_8/kirill/client.c @@ -0,0 +1,117 @@ +// client_udp_third.c + +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUF_SIZE 4096 +#define END_MARKER "END_OF_TRANSMISSION" + +int main(int argc, char *argv[]) { + if (argc < 4) { + fprintf(stderr, + "Usage: %s \n", + argv[0]); + fprintf(stderr, + "Example: %s 127.0.0.1 5000 input.txt\n", + argv[0]); + return -1; + } + + const char *server_ip = argv[1]; + const char *server_port = argv[2]; + const char *input_path = argv[3]; + + char *endptr = NULL; + errno = 0; + long port_long = strtol(server_port, &endptr, 10); + if (errno != 0 || endptr == server_port || *endptr != '\0' || + port_long <= 0 || port_long > 65535) { + fprintf(stderr, "ERROR: invalid port '%s'\n", server_port); + return -1; + } + int port = (int) port_long; + + FILE *fin = fopen(input_path, "r"); + if (!fin) { + perror("fopen(input_file)"); + return -1; + } + + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + perror("socket"); + fclose(fin); + return -1; + } + + struct sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(port); + + if (inet_pton(AF_INET, server_ip, &servaddr.sin_addr) != 1) { + fprintf(stderr, "ERROR: invalid IP address '%s'\n", server_ip); + close(sockfd); + fclose(fin); + return -1; + } + + char buf[BUF_SIZE]; + + for (;;) { + size_t n = fread(buf, 1, sizeof(buf), fin); + if (n > 0) { + ssize_t sent = sendto(sockfd, + buf, + n, + 0, + (struct sockaddr *) &servaddr, + sizeof(servaddr)); + if (sent < 0 || (size_t) sent != n) { + perror("sendto"); + close(sockfd); + fclose(fin); + return -1; + } + } + + if (n < sizeof(buf)) { + if (ferror(fin)) { + perror("fread"); + close(sockfd); + fclose(fin); + return -1; + } + break; + } + } + + if (fclose(fin) == EOF) { + perror("fclose(input_file)"); + } + + ssize_t sent = sendto(sockfd, + END_MARKER, + strlen(END_MARKER), + 0, + (struct sockaddr *) &servaddr, + sizeof(servaddr)); + if (sent < 0 || (size_t) sent != strlen(END_MARKER)) { + perror("sendto(END_MARKER)"); + close(sockfd); + return -1; + } + + printf("File '%s' sent to %s:%d, END marker transmitted.\n", + input_path, server_ip, port); + + close(sockfd); + + return 0; +} diff --git a/mine/lab_8/kirill/server.c b/mine/lab_8/kirill/server.c new file mode 100644 index 0000000..19b54b4 --- /dev/null +++ b/mine/lab_8/kirill/server.c @@ -0,0 +1,142 @@ +// server_udp_third.c + +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUF_SIZE 4096 +#define END_MARKER "END_OF_TRANSMISSION" + +static void process_block(char *buf, + ssize_t len, + long long *pos_counter, + long long *repl_counter) { + if (!buf || !pos_counter || !repl_counter) { + return; + } + + for (ssize_t i = 0; i < len; i++) { + char c = buf[i]; + long long pos = *pos_counter; + + if (((pos + 1) % 3 == 0) && c != '\0') { + buf[i] = ' '; + (*repl_counter)++; + } + + (*pos_counter)++; + } +} + +int main(int argc, char *argv[]) { + if (argc < 3) { + fprintf(stderr, + "Usage: %s \n", + argv[0]); + fprintf(stderr, + "Example: %s 5000 out.txt\n", + argv[0]); + return -1; + } + + char *endptr = NULL; + errno = 0; + long port_long = strtol(argv[1], &endptr, 10); + if (errno != 0 || endptr == argv[1] || *endptr != '\0' || + port_long <= 0 || port_long > 65535) { + fprintf(stderr, "ERROR: invalid port '%s'\n", argv[1]); + return -1; + } + int port = (int) port_long; + + const char *out_path = argv[2]; + + FILE *fout = fopen(out_path, "w"); + if (!fout) { + perror("fopen(output_file)"); + return -1; + } + + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + perror("socket"); + fclose(fout); + return -1; + } + + struct sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(port); + + if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { + perror("bind"); + close(sockfd); + fclose(fout); + return -1; + } + + printf("UDP server (third-char) listening on port %d, output file: %s\n", + port, out_path); + + char buf[BUF_SIZE]; + long long pos_counter = 0; + long long repl_counter = 0; + int done = 0; + + while (!done) { + struct sockaddr_in cliaddr; + socklen_t cli_len = sizeof(cliaddr); + + ssize_t n = recvfrom(sockfd, + buf, + sizeof(buf) - 1, + 0, + (struct sockaddr *) &cliaddr, + &cli_len); + if (n < 0) { + perror("recvfrom"); + repl_counter = -1; + break; + } + + buf[n] = '\0'; + + if (strcmp(buf, END_MARKER) == 0) { + printf("Received END marker from %s:%d, finishing.\n", + inet_ntoa(cliaddr.sin_addr), + ntohs(cliaddr.sin_port)); + done = 1; + break; + } + + process_block(buf, n, &pos_counter, &repl_counter); + + if (fwrite(buf, 1, (size_t) n, fout) != (size_t) n) { + perror("fwrite"); + repl_counter = -1; + done = 1; + break; + } + } + + if (fclose(fout) == EOF) { + perror("fclose(output_file)"); + repl_counter = -1; + } + + close(sockfd); + + printf("Total replacements (every 3rd char): %lld\n", repl_counter); + if (repl_counter < 0) { + return -1; + } + + return 0; +} diff --git a/lab_8/server.c b/mine/lab_8/server.c similarity index 100% rename from lab_8/server.c rename to mine/lab_8/server.c diff --git a/lab_8/vlad/Makefile b/mine/lab_8/vlad/Makefile similarity index 100% rename from lab_8/vlad/Makefile rename to mine/lab_8/vlad/Makefile diff --git a/lab_8/vlad/client.c b/mine/lab_8/vlad/client.c similarity index 100% rename from lab_8/vlad/client.c rename to mine/lab_8/vlad/client.c diff --git a/lab_8/vlad/server.c b/mine/lab_8/vlad/server.c similarity index 100% rename from lab_8/vlad/server.c rename to mine/lab_8/vlad/server.c diff --git a/rgz/Makefile b/mine/rgz/Makefile similarity index 100% rename from rgz/Makefile rename to mine/rgz/Makefile diff --git a/rgz/fifo_client.c b/mine/rgz/fifo_client.c similarity index 100% rename from rgz/fifo_client.c rename to mine/rgz/fifo_client.c diff --git a/rgz/fifo_server_daemon.c b/mine/rgz/fifo_server_daemon.c similarity index 100% rename from rgz/fifo_server_daemon.c rename to mine/rgz/fifo_server_daemon.c diff --git a/rgz/input.txt b/mine/rgz/input.txt similarity index 100% rename from rgz/input.txt rename to mine/rgz/input.txt