0

I am trying to code a Named pipe in linux in C

I searched and searched but still could not find how to properly read from a named pipe. though my code looks ok.

The writing to the pipe works.
But reading is stuck in an infinite loop. The behaviour is very strange.

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

struct letter
{
    char name[10];  
    float val;
}temp;
float a,b;
int fd;

main()
{
    int choice; 
    mkfifo("testfifo",0666);
    fd=open("testfifo",O_RDWR);

    printf("Input value of A ");
    scanf("%f",&a);
    printf("Input value of B ");
    scanf("%f",&b);
    write_to_fifo();
    close(fd);

    printf("Evaluate?");
    scanf("%d",&choice);
    fd=open("testfifo",O_RDWR);
    evaluate();
}
write_to_fifo()
{
    temp.val=a*b;
    strcpy(temp.name,"temp1");
    write(fd,temp,sizeof(temp));
}
evaluate()
{
    float prod; 
    read(fd,temp,sizeof(temp));
    prod=temp.val;
    printf("Result is %f",prod);
}
user3041058
  • 1,520
  • 2
  • 12
  • 16
  • 3
    You can't do this in a single thread like this. When you close the pipe, everything you wrote is thrown away. You need to read from the pipe in a separate thread or process from the one that's writing to it. – Barmar Jun 29 '14 at 08:16
  • 1
    How did you check that writing to the pipe worked? `mkfifo` creates blocking pipes. If you called `open` on your `mkfifo` file in read-only or write-only mode, you will notice the blocking. You will see a similar behaviour if you reading from or writing to a bash pipe created from the shell by calling `mkfifo `. – Pradhan Jun 29 '14 at 08:24
  • @Pradhan I see. Is it the problem of the RDWR mode? Or can mkfifo never be used to create a pipe for communication. Btw, my initial intention was to write to pipe from two processes and read in the first process. – user3041058 Jun 29 '14 at 09:37
  • As an aside, enable warnings as errors in your compiler. Like `gcc -Wall -Wextra -Werror`. I can see that you are not writing standards-compliant code (e.g. function return types not declared), and this will help you stop doing that. – John Zwinck Jun 29 '14 at 11:05
  • 1
    You can't write to the same pipe from two processes. You might be better off using message queues or sockets. Refer to http://stackoverflow.com/questions/2443786/is-it-safe-to-pipe-the-output-of-several-parallel-processes-to-one-file-using – cup Jun 29 '14 at 11:49
  • @cup the problem is we must implement this program for our college.And apparently they said its a classic program ! Even http://stackoverflow.com/questions/2784500/how-to-send-a-simple-string-between-two-programs-using-pipes exactly does not seem to work. The terminal just will not run it fully, it stays in an infinite key read state – user3041058 Jun 29 '14 at 17:14
  • Re: the SO reference, add a getchar before the close in the writer. Once the writer closes the pipe, the pipe is destroyed. If you don't start the reader in time, it will loop forever. – cup Jun 29 '14 at 21:26

0 Answers0