2

I write a test for FIFO. Server writes string "hello" to the client through FIFO. But it seems that the two processes are blocked.I think the FIFO are opened for writing and reading by server and client. But the two processes output nothing.

/* FIFO test */ #include <stdio.h> #include <sys/types.h> #include <sys.stat.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <errno.h> #define FIFOPATH "/home/hel/fifo" // define file path int client(void); int server(void); int main(void) { pid_t pid; /* create FIFO */ if (mkfifo(FIFOPATH, S_IRUSR | S_IWUSR) < 0) { if (errno == EEXIST) { // already exists, ok } /* error */ else { exit(-1); } } /* create process */ pid = fork(); if (pid < 0) { // error, process exits. exit(-1); } else if (pid == 0) { // child, server server(); return 0; // exit } /* parent, client */ client(); return 0; } /* server */ int server(void) { int ret; int fd; /* open fifo for writing */ if ((fd = open(FIFOPATH, 0200)) < 0) { printf("%s\n", strerror(errno)); return -1; // error } ret = write(fd, "hello", 5); close(fd); return 0; } /* client */ int client(void) { char recvBuf[100]; int fd; int ret; /* open fifo for reading */ if ((fd = open(FIFOPATH, 0400)) < 0) { printf("%s\n", strerror(errno)); return -1; // error } ret = read(fd, recvBuf, 5); printf("ret: %d %d\n", ret, fd); printf("client receive %s\n", recvBuf); close(fd); return 0; } 

2 Answers 2

2

Your code has two problems. The first one is the main problem.

  1. The flags parameters passed to open are incorrect. They are not supposed to be unix file permission flags as it appears you have provided. The server should use O_WRONLY and the client should use O_RDONLY.
  2. write(fd, "hello", 5); and read(fd, recvBuf, 5); are not writing and reading the terminating NUL character of the string. But then it is printed as a string: printf("client receive %s\n", recvBuf);. This invokes Undefined Behaviour (even though there is a good chance the program may appear to "work"). Change 5 to 6.
Sign up to request clarification or add additional context in comments.

Comments

0

open() uses following flags:-

 O_RDONLY open for reading only O_WRONLY open for writing only O_RDWR open for reading and writing O_NONBLOCK do not block on open or for data to become available O_APPEND append on each write O_CREAT create file if it does not exist O_TRUNC truncate size to 0 O_EXCL error if O_CREAT and the file exists O_SHLOCK atomically obtain a shared lock O_EXLOCK atomically obtain an exclusive lock O_NOFOLLOW do not follow symlinks O_SYMLINK allow open of symlinks O_EVTONLY descriptor requested for event notifications only O_CLOEXEC mark as close-on-exec 

for FIFO you must use O_RDONLY in client and O_WRONLY in server in your program.

0200 and 0400 permissions are not working for open(). you can check the the flag value in as

#define O_RDONLY 0x0000 / open for reading only */

#define O_WRONLY 0x0001 / open for writing only */

thats why open blocks in your case as it doesn't get correct flag.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.