Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:57:34+00:00 2026-06-17T17:57:34+00:00

I am trying to send information back and forth between two processes using a

  • 0

I am trying to send information back and forth between two processes using a fifo. It works up to a point, but then a read blocks. I suspect Process2 is where the bug is.

Process1:

#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
main()
{
 char oprtr;
 int fd1,fd0;
 float oprnd1,oprnd2,result;

 mkfifo("fifo1",0777);
 fd1=open("fifo1",O_RDWR);

 printf("fd1:%d\n",fd1);
 printf("Add(+)\n");
 printf("subtract(-)\n");
 printf("multiply(*)\n");
 printf("division(/)\n");

 printf("Enter operator\n");
 scanf("%c",&oprtr);
 getchar();
 write(fd1,&oprtr,sizeof(oprtr));

 printf("Enter oprnd1\n");
 scanf("%f",&oprnd1);
 getchar();
 write(fd1,&oprnd1,sizeof(oprnd1));

 fd0=dup(fd1);
 printf("Enter oprnd2\n");
 scanf("%f",&oprnd2);
 getchar();

 if(write(fd0,&oprnd2,sizeof(oprnd2))==0)
  perror("write : oprnd2:");
 else
  printf("writing oprnd2 done\n");

 read(fd1,&result,sizeof(result));
 printf("Result:%f\n",result);
}

Process2:

#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
main()
{
 int fd2,fd3;
 char oprtr;
 float oprnd1,oprnd2,result;
 fd2=open("fifo1",O_RDWR);
 printf("fd2:%d\n",fd2);

 read(fd2,&oprtr,sizeof(oprtr));
 printf("oprtr:%c\n",oprtr);

 read(fd2,&oprnd1,sizeof(oprnd1));
 printf("oprnd1:%f\n",oprnd1);

 fd3=dup(fd2);

This is where the read function is getting blocked

The above two read() calls appear to be working fine, but the following read() call is getting blocked. Why?

 if(read(fd3,&oprnd2,sizeof(oprnd2))==0) ////This is the problem
  perror("read : oprnd2:");
 else
  printf("oprnd2:%f\n",oprnd2);

switch(oprtr)
 {
 case '+':result=oprnd1+oprnd2;
          write(fd2,&result,sizeof(result));break;
 case '-':result=oprnd1-oprnd2;
          write(fd2,&result,sizeof(result));break;
 case '*':result=oprnd1*oprnd2;
          write(fd2,&result,sizeof(result));break;
 case '/':result=oprnd1/oprnd2;
          write(fd2,&result,sizeof(result));break;
 default: printf("Wrong Choice\n");break;
 }
}

Terminal 1:

Add(+)
subtract(-)
multiply(*)
division(/)
Enter operator
+
Enter oprnd1
14.56
Enter oprnd2
16.44
writing oprnd2 done
Result:16.440089

Terminal 2:

fd2:3
oprtr:+
oprnd1:14.560000

Then it just gets blocked

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:57:35+00:00Added an answer on June 17, 2026 at 5:57 pm

    The short and simple answer is that you shouldn’t use a single fifo for bi-directional communication without some external synchronization mechanism.

    To elaborate: a Unix pipe, or fifo, is not best visualized as a plumbing-type pipe. 
    It’s more like a storage tank, with pipes leading to and from it.  The standard usage is that one process writes, thus filling the tank, and another process reads, thus draining the tank.  In your program, everything that gets written to the fifo sits in the tank until somebody comes along and reads it, first-come-first-served.  And so your Process1 program reads the 16.44 value, that it wrote from oprnd2, back into result.  This leaves the tank empty, so there is nothing for Process2 to read.  It comes down to a race condition.  I suspect that, if you ran these two commands a few hundred times, they would work the way you want a few times. 

    If you just want proof-of-concept, add a sleep to Process1 before the read from the fifo.  A better solution would be to use two fifos, one for each direction.  Or you could devise some way for Process2 to let Process1 know that it (Process2) has read the operator and both operands and has written the result –– e.g., a signal or a semaphore –– but that would probably be more trouble than it’s worth.

    On another matter, I strongly recommend against the use of scanf in anything but a toy, demonstration, or throw-away prototype type of program.  If the user hits just Enter, the program will just sit there forever.  I recommend reading a line (being sure to address buffer-overflow) and then call sscanf on it.

    Oh, and also, please indent your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send the Title information in the Facebook but on clikcing
Trying to send a complex type between two systems that have the same code
I'm trying to send information from a form and a hidden email variable (from
I'm trying to send some information to our CRM from a form on our
am trying send an array to php file using $_POST ,still always getting an
I am able to send data to the server using JSON and get back
I am trying to send information to a server from an android phone. When
I am trying to send/receive some data across 2 computers (mac, ubuntu) using a
I was pointed to the service smtp2web a while back, but I've been trying
I've trying to send some infromation to a php file and display the result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.