/* FIFO-based client example */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#define oracle_fifo_name ".oracle_fifo"
int main(void)
{
int fd_out, fd_in;
int n;
char c, buf[512];
extern int errno;
/* send request */
if ((fd_out = open(oracle_fifo_name, O_WRONLY)) == -1)
perror("cannot open oracle fifo");
sprintf(buf, "%d#", getpid());
if (write(fd_out, buf, strlen(buf)) == -1)
perror("write");
/* receive feedback */
sprintf(buf, "%s_%d", oracle_fifo_name, getpid());
while ((fd_in = open(buf, O_RDONLY)) == -1)
if (errno == ENOENT) {
sleep(1);
continue;
}
else
perror("open");
if ((n = read(fd_in, buf, sizeof(buf))) == -1)
perror("read");
buf[n] = '\0';
fprintf(stdout, "client: (%d) oracle says -\n\n", getpid());
fprintf(stdout, "%s\n", buf);
exit(0);
}