i dont even know
This commit is contained in:
@@ -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));
|
||||
attr.mq_maxmsg = 10; // ваш системный максимум = 10
|
||||
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 */
|
||||
@@ -125,14 +130,14 @@ int main(int argc, char **argv) {
|
||||
if (grant > remain) grant = remain;
|
||||
remain -= grant;
|
||||
rep.granted = grant;
|
||||
rep.remain = remain;
|
||||
rep.remain = remain;
|
||||
} else {
|
||||
rep.granted = 0;
|
||||
rep.remain = 0;
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user