FDD-book/examples/part-05/ch25-advanced/lab01-log-flood/flood.c
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

48 lines
1,010 B
C

/*
* flood.c - user-space log-flood driver for Lab 1.
*
* Calls read() on /dev/myfirst0 as fast as the kernel will let us.
* With MYF_DBG_IO enabled on the read path, each call produces one
* log message. Run once against the "unlimited" driver variant and
* once against the "limited" (DLOG_RL) variant to see the difference.
*/
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define DEVNODE "/dev/myfirst0"
int
main(int argc, char **argv)
{
int fd;
long i, n;
char buf[64];
if (argc != 2) {
fprintf(stderr, "usage: flood <count>\n");
return (2);
}
n = strtol(argv[1], NULL, 10);
if (n <= 0)
errx(2, "count must be positive");
fd = open(DEVNODE, O_RDONLY);
if (fd < 0)
err(1, "open %s", DEVNODE);
for (i = 0; i < n; i++) {
if (lseek(fd, 0, SEEK_SET) < 0)
err(1, "lseek");
if (read(fd, buf, sizeof(buf)) < 0)
err(1, "read");
}
close(fd);
printf("flood: %ld reads completed\n", n);
return (0);
}