i dont even know

This commit is contained in:
2025-11-26 16:42:43 +07:00
parent 42bfff63e5
commit a1cf4b157d
3 changed files with 61 additions and 45 deletions

View File

@@ -5,12 +5,12 @@
#define NAME_MAXLEN 64 #define NAME_MAXLEN 64
typedef struct { typedef struct {
pid_t pid; // PID пчелы pid_t pid;
int want; // желаемая порция; 0 => STOP-маркер int want;
char replyq[NAME_MAXLEN]; // имя очереди для ответов ("/bee_<pid>") char replyq[NAME_MAXLEN];
} req_msg_t; } req_msg_t;
typedef struct { typedef struct {
int granted; // выдано меда; 0 => стоп int granted;
int remain; // остаток мёда int remain;
} rep_msg_t; } rep_msg_t;

View File

@@ -9,7 +9,6 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "common.h" #include "common.h"
@@ -21,7 +20,7 @@ static volatile sig_atomic_t stop_flag = 0;
static void on_sigint(int) { stop_flag = 1; } static void on_sigint(int) { stop_flag = 1; }
static void msleep(int ms) { static void msleep(int ms) {
struct timespec ts = { .tv_sec = ms/1000, .tv_nsec = (ms%1000)*1000000L }; struct timespec ts = {.tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000L};
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
} }
@@ -35,8 +34,8 @@ static void add_client(const char *name) {
if (strncmp(clients[i], name, NAME_MAXLEN) == 0) return; if (strncmp(clients[i], name, NAME_MAXLEN) == 0) return;
} }
if (client_count < MAX_CLIENTS) { if (client_count < MAX_CLIENTS) {
strncpy(clients[client_count], name, NAME_MAXLEN-1); strncpy(clients[client_count], name, NAME_MAXLEN - 1);
clients[client_count][NAME_MAXLEN-1] = '\0'; clients[client_count][NAME_MAXLEN - 1] = '\0';
client_count++; client_count++;
} }
} }
@@ -49,11 +48,11 @@ static void send_stop_to_clients(void) {
stoprep.remain = 0; stoprep.remain = 0;
for (int i = 0; i < client_count; ++i) { for (int i = 0; i < client_count; ++i) {
mqd_t q = mq_open(clients[i], O_WRONLY); mqd_t q = mq_open(clients[i], O_WRONLY);
if (q == (mqd_t)-1) { if (q == (mqd_t) -1) {
// best-effort: skip if cannot open // best-effort: skip if cannot open
continue; continue;
} }
mq_send(q, (const char*)&stoprep, sizeof(stoprep), 0); mq_send(q, (const char *) &stoprep, sizeof(stoprep), 0);
mq_close(q); mq_close(q);
} }
} }
@@ -73,13 +72,14 @@ int main(int argc, char **argv) {
} }
mq_unlink(REQ_QUEUE); mq_unlink(REQ_QUEUE);
struct mq_attr attr; memset(&attr, 0, sizeof(attr)); struct mq_attr attr;
attr.mq_maxmsg = 10; // ваш системный максимум = 10 memset(&attr, 0, sizeof(attr));
attr.mq_maxmsg = 10; // ваш системный максимум = 10
attr.mq_msgsize = sizeof(req_msg_t); attr.mq_msgsize = sizeof(req_msg_t);
// Открываем общую очередь на O_RDWR: читаем заявки и посылаем STOP // Открываем общую очередь на O_RDWR: читаем заявки и посылаем STOP
mqd_t qreq = mq_open(REQ_QUEUE, O_CREAT | O_RDWR, 0666, &attr); mqd_t qreq = mq_open(REQ_QUEUE, O_CREAT | O_RDWR, 0666, &attr);
if (qreq == (mqd_t)-1) { if (qreq == (mqd_t) -1) {
perror("mq_open qreq"); perror("mq_open qreq");
fprintf(stderr, "Hint: ensure msg_max>=10 and msgsize_max>=%zu\n", sizeof(req_msg_t)); fprintf(stderr, "Hint: ensure msg_max>=10 and msgsize_max>=%zu\n", sizeof(req_msg_t));
return 1; return 1;
@@ -97,23 +97,28 @@ int main(int argc, char **argv) {
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
long long next_eat_ns = (long long)now.tv_sec*1000000000LL + now.tv_nsec + (long long)period_ms*1000000LL; long long next_eat_ns = (long long) now.tv_sec * 1000000000LL + now.tv_nsec + (long long) period_ms * 1000000LL;
long long last_feed_ns = (long long)now.tv_sec*1000000000LL + now.tv_nsec; long long last_feed_ns = (long long) now.tv_sec * 1000000000LL + now.tv_nsec;
req_msg_t req; req_msg_t req;
bool need_stop_broadcast = false; bool need_stop_broadcast = false;
while (!stop_flag) { while (!stop_flag) {
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
long long now_ns = (long long)now.tv_sec*1000000000LL + now.tv_nsec; long long now_ns = (long long) now.tv_sec * 1000000000LL + now.tv_nsec;
int sleep_ms = (int)((next_eat_ns - now_ns)/1000000LL); int sleep_ms = (int) ((next_eat_ns - now_ns) / 1000000LL);
if (sleep_ms < 0) sleep_ms = 0; if (sleep_ms < 0) sleep_ms = 0;
struct timespec deadline = { .tv_sec = now.tv_sec + sleep_ms/1000, struct timespec deadline = {
.tv_nsec = now.tv_nsec + (sleep_ms%1000)*1000000L }; .tv_sec = now.tv_sec + sleep_ms / 1000,
if (deadline.tv_nsec >= 1000000000L) { deadline.tv_sec++; deadline.tv_nsec -= 1000000000L; } .tv_nsec = now.tv_nsec + (sleep_ms % 1000) * 1000000L
};
if (deadline.tv_nsec >= 1000000000L) {
deadline.tv_sec++;
deadline.tv_nsec -= 1000000000L;
}
ssize_t rd = mq_timedreceive(qreq, (char*)&req, sizeof(req), NULL, &deadline); ssize_t rd = mq_timedreceive(qreq, (char *) &req, sizeof(req), NULL, &deadline);
if (rd >= 0) { if (rd >= 0) {
if (req.want != 0) { if (req.want != 0) {
/* record client's reply queue so we can notify it on shutdown */ /* record client's reply queue so we can notify it on shutdown */
@@ -125,14 +130,14 @@ int main(int argc, char **argv) {
if (grant > remain) grant = remain; if (grant > remain) grant = remain;
remain -= grant; remain -= grant;
rep.granted = grant; rep.granted = grant;
rep.remain = remain; rep.remain = remain;
} else { } else {
rep.granted = 0; rep.granted = 0;
rep.remain = 0; rep.remain = 0;
} }
mqd_t qrep = mq_open(req.replyq, O_WRONLY); mqd_t qrep = mq_open(req.replyq, O_WRONLY);
if (qrep != (mqd_t)-1) { if (qrep != (mqd_t) -1) {
mq_send(qrep, (const char*)&rep, sizeof(rep), 0); mq_send(qrep, (const char *) &rep, sizeof(rep), 0);
mq_close(qrep); mq_close(qrep);
} }
} }
@@ -141,7 +146,7 @@ int main(int argc, char **argv) {
} }
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
now_ns = (long long)now.tv_sec*1000000000LL + now.tv_nsec; now_ns = (long long) now.tv_sec * 1000000000LL + now.tv_nsec;
if (now_ns >= next_eat_ns) { if (now_ns >= next_eat_ns) {
if (remain > 0) { if (remain > 0) {
int eat = portion; int eat = portion;
@@ -150,13 +155,13 @@ int main(int argc, char **argv) {
last_feed_ns = now_ns; last_feed_ns = now_ns;
fprintf(stderr, "Winnie eats %d, remain=%d\n", eat, remain); fprintf(stderr, "Winnie eats %d, remain=%d\n", eat, remain);
} else { } else {
if (starvation_ms > 0 && (now_ns - last_feed_ns)/1000000LL >= starvation_ms) { if (starvation_ms > 0 && (now_ns - last_feed_ns) / 1000000LL >= starvation_ms) {
fprintf(stderr, "Winnie starved, stopping\n"); fprintf(stderr, "Winnie starved, stopping\n");
need_stop_broadcast = true; need_stop_broadcast = true;
break; break;
} }
} }
next_eat_ns = now_ns + (long long)period_ms*1000000LL; next_eat_ns = now_ns + (long long) period_ms * 1000000LL;
} }
if (remain <= 0) { if (remain <= 0) {

View File

@@ -15,7 +15,7 @@
#include "common.h" #include "common.h"
static void msleep(int ms) { static void msleep(int ms) {
struct timespec ts = { .tv_sec = ms/1000, .tv_nsec = (ms%1000)*1000000L }; struct timespec ts = {.tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000L};
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
} }
@@ -25,51 +25,62 @@ int main(int argc, char **argv) {
return 2; return 2;
} }
int portion = atoi(argv[1]); int portion = atoi(argv[1]);
if (portion <= 0) { fprintf(stderr, "portion must be >0\n"); return 2; } if (portion <= 0) {
fprintf(stderr, "portion must be >0\n");
return 2;
}
pid_t me = getpid(); pid_t me = getpid();
char replyq[NAME_MAXLEN]; char replyq[NAME_MAXLEN];
snprintf(replyq, sizeof(replyq), "/bee_%d", (int)me); snprintf(replyq, sizeof(replyq), "/bee_%d", (int) me);
mq_unlink(replyq); mq_unlink(replyq);
struct mq_attr attr; memset(&attr, 0, sizeof(attr)); struct mq_attr attr;
attr.mq_maxmsg = 10; // также укладываемся в лимит memset(&attr, 0, sizeof(attr));
attr.mq_maxmsg = 10; // также укладываемся в лимит
attr.mq_msgsize = sizeof(rep_msg_t); attr.mq_msgsize = sizeof(rep_msg_t);
mqd_t qrep = mq_open(replyq, O_CREAT | O_RDONLY, 0666, &attr); mqd_t qrep = mq_open(replyq, O_CREAT | O_RDONLY, 0666, &attr);
if (qrep == (mqd_t)-1) { perror("mq_open reply"); return 1; } if (qrep == (mqd_t) -1) {
perror("mq_open reply");
return 1;
}
mqd_t qreq = (mqd_t)-1; mqd_t qreq = (mqd_t) -1;
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
qreq = mq_open(REQ_QUEUE, O_WRONLY); qreq = mq_open(REQ_QUEUE, O_WRONLY);
if (qreq != (mqd_t)-1) break; if (qreq != (mqd_t) -1) break;
if (errno != ENOENT) { perror("mq_open req"); break; } if (errno != ENOENT) {
perror("mq_open req");
break;
}
msleep(100); msleep(100);
} }
if (qreq == (mqd_t)-1) { if (qreq == (mqd_t) -1) {
perror("mq_open req"); perror("mq_open req");
mq_close(qrep); mq_close(qrep);
mq_unlink(replyq); mq_unlink(replyq);
return 1; return 1;
} }
unsigned seed = (unsigned)(time(NULL) ^ (uintptr_t)me); unsigned seed = (unsigned) (time(NULL) ^ (uintptr_t) me);
while (1) { while (1) {
int ms = 100 + (rand_r(&seed) % 600); int ms = 100 + (rand_r(&seed) % 600);
msleep(ms); msleep(ms);
req_msg_t req; memset(&req, 0, sizeof(req)); req_msg_t req;
memset(&req, 0, sizeof(req));
req.pid = me; req.pid = me;
req.want = portion; req.want = portion;
strncpy(req.replyq, replyq, sizeof(req.replyq)-1); strncpy(req.replyq, replyq, sizeof(req.replyq) - 1);
if (mq_send(qreq, (const char*)&req, sizeof(req), 0) == -1) { if (mq_send(qreq, (const char *) &req, sizeof(req), 0) == -1) {
perror("mq_send"); perror("mq_send");
break; break;
} }
rep_msg_t rep; rep_msg_t rep;
ssize_t rd = mq_receive(qrep, (char*)&rep, sizeof(rep), NULL); ssize_t rd = mq_receive(qrep, (char *) &rep, sizeof(rep), NULL);
if (rd == -1) { if (rd == -1) {
perror("mq_receive"); perror("mq_receive");
break; break;
@@ -77,7 +88,7 @@ int main(int argc, char **argv) {
if (rep.granted <= 0) { if (rep.granted <= 0) {
break; break;
} }
dprintf(STDOUT_FILENO, "Bee %d got %d, remain %d\n", (int)me, rep.granted, rep.remain); dprintf(STDOUT_FILENO, "Bee %d got %d, remain %d\n", (int) me, rep.granted, rep.remain);
} }
mq_close(qreq); mq_close(qreq);