From 5411b208af6b45cd45b00f45bc135b65a7ae899f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 17 May 2015 14:49:22 +0200 Subject: timed write example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/examples/timed/.gitignore | 1 + doc/examples/timed/Makefile | 2 +- doc/examples/timed/README | 12 +++++++++-- doc/examples/timed/poll.c | 1 - doc/examples/timed/slow-poll.c | 46 ++++++++++++++++++++++++++++++++++++++++++ doc/examples/timed/write.c | 5 ++++- 6 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 doc/examples/timed/slow-poll.c diff --git a/doc/examples/timed/.gitignore b/doc/examples/timed/.gitignore index b1076f0..cb90bfb 100644 --- a/doc/examples/timed/.gitignore +++ b/doc/examples/timed/.gitignore @@ -3,4 +3,5 @@ init write poll read +slow-poll diff --git a/doc/examples/timed/Makefile b/doc/examples/timed/Makefile index fe1285c..9506947 100644 --- a/doc/examples/timed/Makefile +++ b/doc/examples/timed/Makefile @@ -1,4 +1,4 @@ -COMMANDS = init cleanup write poll read +COMMANDS = init cleanup write poll read slow-poll all: ${COMMANDS} diff --git a/doc/examples/timed/README b/doc/examples/timed/README index 215a738..8293758 100644 --- a/doc/examples/timed/README +++ b/doc/examples/timed/README @@ -12,11 +12,19 @@ When you are done run ./cleanup. Running instances of ./poll will wait for new messages continuously, but with one second timeouts. +./slow-poll works like ./poll, execpt it will sleep +for one second every time it receives a message. + Running instances of ./read will read for ten seconds then time out. -./poll and ./read will stop if the message "stop" is -broadcasted. +./poll, ./read, and ./slow-poll will stop if the message +"stop" is broadcasted. + +./write will wait for atmost a tenth of a seconds before +failing. This means that if two instances of ./write is +started at the same time one of them will fail if +./slow-poll is running. ./poll, ./read, ./init and ./cleanup are run without any additional arguments. ./write is run with the message diff --git a/doc/examples/timed/poll.c b/doc/examples/timed/poll.c index d57d4ea..5ae45d4 100644 --- a/doc/examples/timed/poll.c +++ b/doc/examples/timed/poll.c @@ -24,7 +24,6 @@ main() if (message == NULL) { t(errno != EAGAIN); printf("waiting... %lli\n", ++tick); - sleep(1); continue; } tick = 0; diff --git a/doc/examples/timed/slow-poll.c b/doc/examples/timed/slow-poll.c new file mode 100644 index 0000000..5076ef6 --- /dev/null +++ b/doc/examples/timed/slow-poll.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + + +int +main() +{ + bus_t bus; + const char *message; + long long tick = 0; + struct timespec timeout; + t(bus_open(&bus, "/tmp/example-bus", BUS_RDONLY)); + t(bus_poll_start(&bus)); + for (;;) { + t(clock_gettime(CLOCK_MONOTONIC, &timeout)); + timeout.tv_sec += 1; + message = bus_poll_timed(&bus, &timeout, CLOCK_MONOTONIC); + if (message == NULL) { + t(errno != EAGAIN); + printf("waiting... %lli\n", ++tick); + continue; + } + tick = 0; + message = strchr(message, ' ') + 1; + if (!strcmp(message, "stop")) + break; + printf("\033[01m%s\033[21m\n", message); + sleep(1); + } + t(bus_poll_stop(&bus)); + bus_close(&bus); + return 0; + +fail: + perror("poll"); + bus_poll_stop(&bus); + bus_close(&bus); + return 1; +} + diff --git a/doc/examples/timed/write.c b/doc/examples/timed/write.c index 250ba14..acd8c6f 100644 --- a/doc/examples/timed/write.c +++ b/doc/examples/timed/write.c @@ -15,13 +15,16 @@ int main(int argc, char *argv[]) { bus_t bus; + struct timespec timeout; if (argc < 2) { fprintf(stderr, "%s: USAGE: %s message\n", argv[0], argv[0]); return 2; } sprintf(message, "0 %s", argv[1]); t(bus_open(&bus, "/tmp/example-bus", BUS_WRONLY)); - t(bus_write(&bus, message, 0)); + t(clock_gettime(CLOCK_MONOTONIC, &timeout)); + timeout.tv_nsec += 100000000L; + t(bus_write_timed(&bus, message, &timeout, CLOCK_MONOTONIC)); bus_close(&bus); return 0; -- cgit v1.2.3-70-g09d2