1

I want to use good old FIFO to pass data to another part of my own iOS application. I looked at dispatch_io_create_with_path but I want the other side to be able to read data with plain read(). Is it possible to use FIFO file? how?

I tried next code in Xcode7 ObjC (path points to Documents dir + filename):

res = mkfifo(path, 0x777);

int fd;
fd = open(path, O_WRONLY);
write(fd, "test", 5);
close(fd);

mkfifo() returns 0 but open() fails with -1. Interestingly if I try open() with 'O_RDONLY' program is waiting so something is happening over there..

gheni4
  • 273
  • 3
  • 16
  • Why do you want to do it via a file? Have you looked at this http://stackoverflow.com/questions/13129233/objective-c-manual-array-fifo-queue ? How does it differ from what you want / need? – Losiowaty Jul 06 '16 at 18:55
  • Not sure if it's helpful, but looking up FIFO files I found `However, it has to be open at both ends simultaneously before you can proceed to do any input or output operations on it. Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.` – Putz1103 Jul 06 '16 at 18:58
  • @Losiowaty Please note I'm asking about the 'other FIFO'. Not First In First Out but a file type supported by Unix/Linux/Mac OS X. I want to pass data to a static library that wants 'filePath' without actually writing data to file. I don't expect any seek() – gheni4 Jul 06 '16 at 19:00
  • @Putz1103 Exactly my point - open() 'read' blocks so it looks like it works but I can't get it to open for write() – gheni4 Jul 06 '16 at 19:02
  • 1
    Oh, ok. I didn't know about them, I guess I'm gonna learn something new tonight :) – Losiowaty Jul 06 '16 at 19:02
  • Did you try opening if for read in an asynchronous block (that blocks and waits) and then opening it for write? – Putz1103 Jul 06 '16 at 19:09
  • @Putz1103 Tried several scenarios with dispatch_async() and dispatch_after() to open for write() when reader is ready and waiting. Same result: reader's queue is blocked and writer fails to open() – gheni4 Jul 07 '16 at 08:10

1 Answers1

2

omg can't believe I wasted hours on that one... well here it is: The error is in the "0x777" part. Should be "0777". After fixing that - FIFO is worked fine for me (iOS 9.2.1).

gheni4
  • 273
  • 3
  • 16
  • What path did you set here? I thought in iOS the filesystem is sandboxed away. – gran_profaci Jan 07 '17 at 02:19
  • @gran_profaci I had to pass data to a static library that was part of my project that expects a filePath yet I didn't want to write all that data to a file. Everything happened locally in the same sandbox so there was no problem. I don't remember exact path but it was regulal file name in the 'Documents' – gheni4 Jan 08 '17 at 06:54