some fixes in lab2

This commit is contained in:
Ilya Bezrukov 2026-02-25 21:23:07 +03:00
parent a0d6e0bdf5
commit 160886c6c4
3 changed files with 16 additions and 14 deletions

View File

@ -6,13 +6,13 @@ LIBS := -lpthread
SSOURCES := server.c SSOURCES := server.c
SOBJECTS = $(addprefix $(BIN), $(SSOURCES:.c=.o)) SOBJECTS = $(addprefix $(BIN), $(SSOURCES:.c=.o))
SERVERNAME := otpserver SERVERNAME := server
SERVERNAME := $(addprefix $(BIN), $(SERVERNAME)) SERVERNAME := $(addprefix $(BIN), $(SERVERNAME))
CSOURCES := client.c CSOURCES := client.c
COBJECTS = $(addprefix $(BIN), $(CSOURCES:.c=.o)) COBJECTS = $(addprefix $(BIN), $(CSOURCES:.c=.o))
CLIENTNAME := otpclient CLIENTNAME := client
CLIENTNAME := $(addprefix $(BIN), $(CLIENTNAME)) CLIENTNAME := $(addprefix $(BIN), $(CLIENTNAME))

View File

@ -8,7 +8,7 @@
#include <stdint.h> #include <stdint.h>
#include <poll.h> #include <poll.h>
#define SOCKET_PATH "/tmp/otp_socket" #define SOCKET_PATH "/tmp/socket"
#define CMD_ECHO 1 #define CMD_ECHO 1
#define CMD_TIMER 2 #define CMD_TIMER 2
@ -25,7 +25,7 @@ void send_fd(int socket, int fd) {
char buf[CMSG_SPACE(sizeof(fd))]; char buf[CMSG_SPACE(sizeof(fd))];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
struct iovec io = { .iov_base = (void*)"F", .iov_len = 1 }; struct iovec io = { .iov_base = "F", .iov_len = 1 };
msg.msg_iov = &io; msg.msg_iov = &io;
msg.msg_iovlen = 1; msg.msg_iovlen = 1;
@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
req.command = atoi(argv[2]); req.command = atoi(argv[2]);
req.value = atoi(argv[3]); req.value = atoi(argv[3]);
write(sock, &req, sizeof(req)); write(sock, &req, sizeof(struct request));
int response; int response;
read(sock, &response, sizeof(response)); read(sock, &response, sizeof(response));

View File

@ -11,7 +11,7 @@
#include <stdint.h> #include <stdint.h>
#include <time.h> #include <time.h>
#define SOCKET_PATH "/tmp/otp_socket" #define SOCKET_PATH "/tmp/socket"
#define MAX_CLIENTS 10 #define MAX_CLIENTS 10
#define CMD_ECHO 1 #define CMD_ECHO 1
@ -71,18 +71,20 @@ void* worker_thread(void *arg) {
continue; continue;
} }
struct request *req = &(node->req);
char buffer[128]; char buffer[128];
sprintf(buffer, "Processing request with priority %d", node->req.priority); sprintf(buffer, "Processing (p: %d, c: %d, v: %d)", req->priority, req->command, req->value);
log_msg(buffer); log_msg(buffer);
int response = 0; int response = 0;
if (node->req.command == CMD_ECHO) { if (req->command == CMD_ECHO) {
response = node->req.value; response = req->value;
write(node->client_fd, &response, sizeof(response)); write(node->client_fd, &response, sizeof(response));
} }
else if (node->req.command == CMD_TIMER) { else if (req->command == CMD_TIMER) {
sleep(node->req.value); sleep(req->value);
uint64_t val = 1; uint64_t val = 1;
write(node->event_fd, &val, sizeof(val)); write(node->event_fd, &val, sizeof(val));
response = 0; response = 0;
@ -99,7 +101,7 @@ int recv_fd(int socket) {
char buf[CMSG_SPACE(sizeof(int))]; char buf[CMSG_SPACE(sizeof(int))];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
struct iovec io = { .iov_base = (void*)"F", .iov_len = 1 }; struct iovec io = { .iov_base = "F", .iov_len = 1 };
msg.msg_iov = &io; msg.msg_iov = &io;
msg.msg_iovlen = 1; msg.msg_iovlen = 1;
@ -138,8 +140,8 @@ int main() {
int event_fd = recv_fd(client_fd); int event_fd = recv_fd(client_fd);
struct request req; struct request req = {0, 0, 0};
read(client_fd, &req, sizeof(req)); read(client_fd, &req, sizeof(struct request));
client_request_t *node = malloc(sizeof(client_request_t)); client_request_t *node = malloc(sizeof(client_request_t));
node->client_fd = client_fd; node->client_fd = client_fd;