Issues with FIFO between a C process and a JAVA application Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Issues with FIFO between a C process and a JAVA application without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have 2 applications, one written in C and the other in JAVA. Lets call these applications ‘C’ and ‘JAVA’.

  1. C Creates a named pipe via mkfifo (have also tried mknod – no difference to the outcome)
  2. C forks off with the child process invoking a system() call to fire-up the JAVA application, passing to it, the name of the FIFO file. IE: system(“name-of-java-app ‘name-of-fifo-file'”)
  3. C parent opens the FIFO for write only

JAVA opens the FIFO for read.

  1. C sends a ‘handshake’ message to the FIFO via write().
  2. JAVA receives ‘handshake’
  3. C then tries to send subsequent messages to JAVA, via the FIFO.

Here is where it get weird. Unless C closes (not removes) the FIFO FD or if the parent process terminates, JAVA will not read from the FIFO. If C sends ‘handshake’ then closes, all is well and as expected.
If C then re-opens the FIFO, sends a ‘message’ to JAVA then C closes, JAVA will read it.

From all of the examples of FIFO between C and JAVA that I have studied, they only include a single transfer of data. IE, open, write/read, close but not a continuing stream from source to destination, as I require.

Implementing an ‘open, read/write, close’ at each end works but surely, there must be a better/more efficient method – more congruent to the ethos of pipes. If so, I would greatly appreciate any input!

Answer

SOLVED

JAVA’s readline expects either ‘r’ or ‘n’ to terminate a string. Coming from a UNIX C environment (where one expects ‘’ as the ‘standard’ terminator) and being fairly new to JAVA, this was an interesting lesson to learn!

Tnx chrylis and fge for your input!

We are here to answer your question about Issues with FIFO between a C process and a JAVA application - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji