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 6803903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:24:04+00:00 2026-05-26T19:24:04+00:00

The following code reads messages from other processes through a pipe. All processes correctly

  • 0

The following code reads messages from other processes through a pipe. All processes correctly print out all the messages, but then they will never proceed past the while loop. Tried debugging in Eclipse, after reading reading all the messages, it will just stop at that while loop.

The index is a number assigned to each process. The first process would have index == 0.
The message itself is simply the index of the process sending the message.

while((n = read(fd[index][0], &mymsg, sizeof(int))) == sizeof(int))
        printf("process%d  has received a message from process%d\n", index, mymsg);

Any ideas why this would happen?

Here is how each process writes to another:

// Write to other process
if(write(fd[index2][1], &index, sizeof(int)) != sizeof(int))
    sys_error(2);

This is done five times. fd is a table of read-and-write ends for each process.

  • 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-05-26T19:24:04+00:00Added an answer on May 26, 2026 at 7:24 pm

    The call to read() is blocking until more data shows up. From the man page for pipe

    If a process attempts to read from an empty pipe, then read(2) will
    block until data is available. If a process attempts to write to a
    full pipe (see below), then write(2) blocks until sufficient data has
    been read from the pipe to allow the write to complete. Nonblocking
    I/O is possible by using the fcntl(2) F_SETFL operation to enable the
    O_NONBLOCK open file status flag.

    After you open each file descriptor before you enter that while loop do this to each one:

    fcntl(fd, F_SETFL, O_NONBLOCK);
    

    However, you really should read up on blocking vs. non-blocking I/O, including reading the man pages for pipe, read, fcntl, etc.

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

Sidebar

Related Questions

The following code reads a text file one character at the time and print
The following code does not work correctly on Windows (but does on Linux): sock
I have the following code to read messages sent from a server to the
I have the following code which reads in the follow file, append a \r\n
I have the following code that reads the content of a url public static
I'm following a book on c, and I come to some code that reads
Why does the following code print ‘read(): Resource temporarily unavailable’ 80% of the time?
I am working on a code which I need to send sms messages from
I'm trying to write code that spawns two child processes that send each other
I am trying to extract all the images from a pdf using itextsharp but

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.