Commit d2f382e7 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov

test: Add test-rnd-from-file test

Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@gmail.com>
parent 37e73d6a
SRCS += test-counter.c
SRCS += test-rnd-from-file.c
OBJS := $(patsubst %.c,%.o,$(SRCS))
PROGS := $(patsubst %.c,%,$(SRCS))
......@@ -7,7 +8,7 @@ all: $(PROGS)
$(PROGS): $(OBJS)
$(E) " LINK " $@
$(Q) $(CC) $< -o $@
$(Q) $(CC) $@.o -o $@
%.o: %.c
$(E) " CC " $@
......
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
const char fname_rnd[] = "random-data.o"; /* so make clean drops it */
const int limit = 10;
int counter, fd, rnd;
unlink((char *)fname_rnd);
fd = open(fname_rnd, O_RDWR | O_CREAT | O_EXCL, 0644);
if (fd < 0) {
perror("Can't open file");
return fd;
}
counter = 0;
while (counter++ < limit) {
rnd = rand();
write(fd, &rnd, sizeof(rnd));
}
counter = 0;
while (1) {
lseek(fd, 0, SEEK_SET);
while (counter++ < limit) {
read(fd, &rnd, sizeof(rnd));
printf("Pid: %10d Rnd: %10d\n",
getpid(), rnd);
sleep(3);
}
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment