Vatish Sharma
Vatish Sharma

Reputation: 1576

How to verify that the `mkfifo` call succeed?

I am new to C++ programming and I am trying to use mkfifo command to create a named pipe that I can read from my java program. Here is my code:

int main(int, char **)
{
    std::cout << "START" << std::endl;
    int fd;

    // FIFO file path
    char *myfifo = "/sdcard/tempFifo";

    // Creating the named file(FIFO)
    // mkfifo(<pathname>, <permission>)
    int ret = mkfifo(myfifo, 0666);
    std::cout << "mkfifo ret " << ret << std::endl;
    std::cout << "errno " << errno << std::endl;
    std::cout << "errno str::" << std::strerror(errno) << std::endl;

    char arr1[80];
    int startCount = 0;
    while (1)
    {

        // Open FIFO for Read only
        std::cout << "Opening FIFO.." << std::endl;
        fd = open(myfifo, O_RDONLY);

        // Read from FIFO
        std::cout << "Start reading.." << std::endl;
        read(fd, arr1, sizeof(arr1));

        if (strcmp(arr1, "start") == 0)
        {
            if (startCount == 1)
            {
                std::cout << "Start count is greater than one so exit." << std::endl;
                return 0;
            }
            startCount++;
            std::cout << "Start received" << std::endl;
        }

        sleep(5000);
        close(fd);
    }
    std::cout << "STOP" << std::endl;
    return 0;
}

To write "start" on the target pipe, I am using a java code as:

            File file = new File("/sdcard/tempFifo");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            PrintWriter printWriter = new PrintWriter(fileOutputStream);
            printWriter.print("start");
            printWriter.flush();
            printWriter.close();

I am getting Exception: /sdcard/tempFifo: open failed: ENOENT (No such file or directory) in java applicatoin and when I have executed ls -l /sdcard/tempFifo on adb shell then I am not able to see any tempFifo file on my sdcard of rooted phone.

Does anyone know what is the problem in my code?

Update on errno

mkfifo ret -1
errno 22
errno str::Invalid argument

Upvotes: 0

Views: 2235

Answers (1)

Vatish Sharma
Vatish Sharma

Reputation: 1576

Android doesn't allow creating pipes on /sdcard so, I have used /system/tempFifo instead of /sdcard/tempFifo to make it work.

Upvotes: 1

Related Questions