1

I have a part of code which is written to write into pipe from native code in Android. However, the program does not seem to block on the write operation. As per my understanding the program should block on write untill there is a process that finally reads from the pipe. Please let me know what is wrong with below code and why it exits without blocking.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>

int main()
{
    const char* PATH = "/data/data/com.endpoint.login/v_pipe8";
    char* line = "Hello Pipe!";
    int pipe;

    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
    pipe = mkfifo(PATH, mode);
    if (pipe == -1)
    {
        printf("mkfifo failure\n");
    }

    sleep(1);
    int err = write(pipe,line,strlen(line));

    if (err == -1)
    {
            printf("write error in the pipe\n");
    }

    close(pipe);
    return 0;
}
comrade
  • 4,590
  • 5
  • 33
  • 48
Sameer Joshi
  • 329
  • 3
  • 16

2 Answers2

2

I found the answer. The problem the program was not getting blocked was an error in the program.

This program did not do open before calling write. Also mkfifo does not return fd for the pipe. Correct answer is following:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
    const char* PATH = "/data/data/com.endpoint.login/v_pipe8";
    char* line = "Hello Pipe!";
    int pipe, status;

    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
    status = mkfifo(PATH, mode);
    if (status == -1)
    {
        printf("mkfifo failure\n");
    }

    pipe = open(PATH,O_WRONLY);
    sleep(1);
    int err = write(pipe,line,strlen(line));

    if (err == -1)
    {
            printf("write error in the pipe\n");
    }

    close(pipe);
    return 0;
}
Sameer Joshi
  • 329
  • 3
  • 16
1

You cannot access directly to a path of memory in Android (Maybe is the device is rooted). So, you need a path provided by Android SDK. In Java create a pipe following its reference, use NDK to send the object to C, and use it as a BufferedReader.

If you have no Java part because is a pure native app, you can create it directly in C but it is a little bit complicated. You will need JNI reference, and JNI Functions.

vgonisanz
  • 11,831
  • 13
  • 78
  • 130
  • I need to share the pipe between Java and native process. I did not understand the JNI part that you mentioned. If the path of the pipe is known already between Java and Native process, cant they share the pipe? – Sameer Joshi Jul 14 '16 at 11:04
  • To share, you need to call C from Java with a function, something like: "jint Java_JAVAPACKAGE_writeToPipe(JNIEnv* env, jobject jObject)". You need learn something about JNI to do it propertly. You have an example in other questions, also hardcoded. http://stackoverflow.com/questions/12844418/android-using-pipe-between-native-and-java-apps – vgonisanz Jul 14 '16 at 11:42