Ashish K
Ashish K

Reputation: 935

Named PIPE is stuck on open

I am trying to implement a named PIPE IPC method in which I am sending float values (10) each time the sendToPipe function is being called. Here is the sendToPipe function -

int fd;
char *fifoPipe = "/tmp/fifoPipe";

void sendToPipe(paTestData *data){

    int readCount   = 10;
    fd      = open(fifoPipe, O_WRONLY);   // Getting stuck here

    // Reading 10 sample float values from a buffer from readFromCB pointer as the initial address on each call to sendToPipe function.
    while(readCount > 0){

        write(fd, &data->sampleValues[data->readFromCB], sizeof(SAMPLE));  // SAMPLE is of float datatype
        data->readFromCB++;
        readCount--;
    }

    close(fd);

    //  Some code here
}

And I have initialized the named PIPE in my main here :

int main(){

    // Some code
    mkfifo(fifoPipe, S_IWUSR | S_IRUSR);

    // Other code
}

I am not getting where am I going wrong here. Any help is appreciated. Also let me know if any other information is required.

Upvotes: 2

Views: 3048

Answers (2)

LPs
LPs

Reputation: 16213

Summing all the commented points:

  1. Program is "freezing" because of there is no reader on the other side of pipe.
  2. After the first program launch the pipe was created. Next program launches will return FILE_EXIST error.
  3. To write 10 values in a shot to the pipe, making able the reader to receive all them in a shot, you should prepare a buffer and then open the pipe and write to it (blocking mode). As a side note take care of reader side: read function does not grants to retrieve the whole buffer in a shot, so you must check the returned read data number.

Upvotes: 3

Ashish K
Ashish K

Reputation: 935

Thanks to @LPs for pointing out what was going wrong. My each sample after entering the PIPE was waiting to be read. I however wanted an implementation in which my reader thread could read all the 10 samples in one go. Here is my implementation. -

void pipeData(paTestData *data){

    SAMPLE *tempBuff = (SAMPLE*)malloc(sizeof(SAMPLE)*FRAME_SIZE);
    int readCount   = FRAME_SIZE;

    while(readCount > 0){

        tempBuff[FRAME_SIZE - readCount] = data->sampleValues[data->readFromCB];        
        data->readFromCB++;
        readCount--;
    }

    fd = open(fifoPipe, O_WRONLY);

    write(fd, tempBuff, sizeof(tempBuff));
    // Reader thread called here

    close(fd);
}

Upvotes: 1

Related Questions