i dont even know
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
#define NAME_MAXLEN 64
|
||||
|
||||
typedef struct {
|
||||
pid_t pid; // PID пчелы
|
||||
int want; // желаемая порция; 0 => STOP-маркер
|
||||
char replyq[NAME_MAXLEN]; // имя очереди для ответов ("/bee_<pid>")
|
||||
pid_t pid;
|
||||
int want;
|
||||
char replyq[NAME_MAXLEN];
|
||||
} req_msg_t;
|
||||
|
||||
typedef struct {
|
||||
int granted; // выдано меда; 0 => стоп
|
||||
int remain; // остаток мёда
|
||||
int granted;
|
||||
int remain;
|
||||
} rep_msg_t;
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.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 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);
|
||||
}
|
||||
|
||||
@@ -35,8 +34,8 @@ static void add_client(const char *name) {
|
||||
if (strncmp(clients[i], name, NAME_MAXLEN) == 0) return;
|
||||
}
|
||||
if (client_count < MAX_CLIENTS) {
|
||||
strncpy(clients[client_count], name, NAME_MAXLEN-1);
|
||||
clients[client_count][NAME_MAXLEN-1] = '\0';
|
||||
strncpy(clients[client_count], name, NAME_MAXLEN - 1);
|
||||
clients[client_count][NAME_MAXLEN - 1] = '\0';
|
||||
client_count++;
|
||||
}
|
||||
}
|
||||
@@ -49,11 +48,11 @@ static void send_stop_to_clients(void) {
|
||||
stoprep.remain = 0;
|
||||
for (int i = 0; i < client_count; ++i) {
|
||||
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
|
||||
continue;
|
||||
}
|
||||
mq_send(q, (const char*)&stoprep, sizeof(stoprep), 0);
|
||||
mq_send(q, (const char *) &stoprep, sizeof(stoprep), 0);
|
||||
mq_close(q);
|
||||
}
|
||||
}
|
||||
@@ -73,13 +72,14 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
mq_unlink(REQ_QUEUE);
|
||||
struct mq_attr attr; memset(&attr, 0, sizeof(attr));
|
||||
struct mq_attr attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
attr.mq_maxmsg = 10; // ваш системный максимум = 10
|
||||
attr.mq_msgsize = sizeof(req_msg_t);
|
||||
|
||||
// Открываем общую очередь на O_RDWR: читаем заявки и посылаем STOP
|
||||
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");
|
||||
fprintf(stderr, "Hint: ensure msg_max>=10 and msgsize_max>=%zu\n", sizeof(req_msg_t));
|
||||
return 1;
|
||||
@@ -97,23 +97,28 @@ int main(int argc, char **argv) {
|
||||
|
||||
struct timespec 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 last_feed_ns = (long long)now.tv_sec*1000000000LL + now.tv_nsec;
|
||||
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;
|
||||
|
||||
req_msg_t req;
|
||||
bool need_stop_broadcast = false;
|
||||
|
||||
while (!stop_flag) {
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
long long now_ns = (long long)now.tv_sec*1000000000LL + now.tv_nsec;
|
||||
int sleep_ms = (int)((next_eat_ns - now_ns)/1000000LL);
|
||||
long long now_ns = (long long) now.tv_sec * 1000000000LL + now.tv_nsec;
|
||||
int sleep_ms = (int) ((next_eat_ns - now_ns) / 1000000LL);
|
||||
if (sleep_ms < 0) sleep_ms = 0;
|
||||
|
||||
struct timespec deadline = { .tv_sec = now.tv_sec + sleep_ms/1000,
|
||||
.tv_nsec = now.tv_nsec + (sleep_ms%1000)*1000000L };
|
||||
if (deadline.tv_nsec >= 1000000000L) { deadline.tv_sec++; deadline.tv_nsec -= 1000000000L; }
|
||||
struct timespec deadline = {
|
||||
.tv_sec = now.tv_sec + sleep_ms / 1000,
|
||||
.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 (req.want != 0) {
|
||||
/* record client's reply queue so we can notify it on shutdown */
|
||||
@@ -131,8 +136,8 @@ int main(int argc, char **argv) {
|
||||
rep.remain = 0;
|
||||
}
|
||||
mqd_t qrep = mq_open(req.replyq, O_WRONLY);
|
||||
if (qrep != (mqd_t)-1) {
|
||||
mq_send(qrep, (const char*)&rep, sizeof(rep), 0);
|
||||
if (qrep != (mqd_t) -1) {
|
||||
mq_send(qrep, (const char *) &rep, sizeof(rep), 0);
|
||||
mq_close(qrep);
|
||||
}
|
||||
}
|
||||
@@ -141,7 +146,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
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 (remain > 0) {
|
||||
int eat = portion;
|
||||
@@ -150,13 +155,13 @@ int main(int argc, char **argv) {
|
||||
last_feed_ns = now_ns;
|
||||
fprintf(stderr, "Winnie eats %d, remain=%d\n", eat, remain);
|
||||
} 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");
|
||||
need_stop_broadcast = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
next_eat_ns = now_ns + (long long)period_ms*1000000LL;
|
||||
next_eat_ns = now_ns + (long long) period_ms * 1000000LL;
|
||||
}
|
||||
|
||||
if (remain <= 0) {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include "common.h"
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -25,51 +25,62 @@ int main(int argc, char **argv) {
|
||||
return 2;
|
||||
}
|
||||
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();
|
||||
char replyq[NAME_MAXLEN];
|
||||
snprintf(replyq, sizeof(replyq), "/bee_%d", (int)me);
|
||||
snprintf(replyq, sizeof(replyq), "/bee_%d", (int) me);
|
||||
|
||||
mq_unlink(replyq);
|
||||
struct mq_attr attr; memset(&attr, 0, sizeof(attr));
|
||||
struct mq_attr attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
attr.mq_maxmsg = 10; // также укладываемся в лимит
|
||||
attr.mq_msgsize = sizeof(rep_msg_t);
|
||||
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++) {
|
||||
qreq = mq_open(REQ_QUEUE, O_WRONLY);
|
||||
if (qreq != (mqd_t)-1) break;
|
||||
if (errno != ENOENT) { perror("mq_open req"); break; }
|
||||
if (qreq != (mqd_t) -1) break;
|
||||
if (errno != ENOENT) {
|
||||
perror("mq_open req");
|
||||
break;
|
||||
}
|
||||
msleep(100);
|
||||
}
|
||||
if (qreq == (mqd_t)-1) {
|
||||
if (qreq == (mqd_t) -1) {
|
||||
perror("mq_open req");
|
||||
mq_close(qrep);
|
||||
mq_unlink(replyq);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned seed = (unsigned)(time(NULL) ^ (uintptr_t)me);
|
||||
unsigned seed = (unsigned) (time(NULL) ^ (uintptr_t) me);
|
||||
|
||||
while (1) {
|
||||
int ms = 100 + (rand_r(&seed) % 600);
|
||||
msleep(ms);
|
||||
|
||||
req_msg_t req; memset(&req, 0, sizeof(req));
|
||||
req_msg_t req;
|
||||
memset(&req, 0, sizeof(req));
|
||||
req.pid = me;
|
||||
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");
|
||||
break;
|
||||
}
|
||||
|
||||
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) {
|
||||
perror("mq_receive");
|
||||
break;
|
||||
@@ -77,7 +88,7 @@ int main(int argc, char **argv) {
|
||||
if (rep.granted <= 0) {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user