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

  • Home
  • SEARCH
  • 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 6847081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:42:05+00:00 2026-05-27T00:42:05+00:00

I use a pipe to read the output of a function by passing it

  • 0

I use a pipe to read the output of a function by passing it a file descriptor. To read the output asynchronously I setup the “read” end of the pipe in ASYNC NON BLOCKING mode and I configure an IO handler using SIGACTION. My code looks like the following:

struct sigaction io_act;
struct sigaction io_act_old;
sigset_t block_mask;

/* create the pipe */    
pipe (pipe_fd);

/* set the reading end of pipe in ASYNC mode */
fcntl (pipe_fd[0], F_SETOWN, getpid());
fcntl (pipe_fd[0], F_SETFL, O_ASYNC | O_NONBLOCK);

/* add an I/O handler for SIGIO */
sigemptyset (&block_mask);
io_act.sa_handler = sigio_handler;
io_act.sa_mask = block_mask;
io_act.sa_flags = SA_RESTART;
sigaction (SIGIO, &io_act, &io_act_old);

/* executing the function that generate output in given FILEDES */
my_generate_output (pipe_fd[1]);

close(pipe_fd[1]);

/* this sleep should not be needed (subject of question) */
sleep(1);
close (pipe_fd[0]);

/* restore previous signal handler */
sigaction (SIGIO, &io_act_old, NULL);

The problem is that if I omit the sleep function call I can close the pipe and disable the SIGIO handler before the output is completed. The consequence is that the application abort due to an unhandled SIGIO signal (message “I/O possible”).

How can I make sure that all the output have been read from the pipe before disabling the SIGIO handler ?

  • 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-27T00:42:05+00:00Added an answer on May 27, 2026 at 12:42 am

    To get the number of readable bytes, you might use the ioctl FIONREAD, which is not very portable. See this question as a contrived example.

    You can also test availability of things to read with poll, select and friends.

    And the end-of-file condition is reported with read returning 0 as count.

    ADDENDA

    according to read(2) syscall man page, read fails with errno ==:

       EAGAIN or EWOULDBLOCK
              The  file  descriptor  fd refers to a socket and has been marked
              nonblocking   (O_NONBLOCK),   and   the   read   would    block.
              POSIX.1-2001  allows  either error to be returned for this case,
              and does not require these constants to have the same value,  so
              a portable application should check for both possibilities.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When creating a pipe with os.pipe() it returns 2 file numbers; a read end
I'm trying to use the bash builtin read to read data from a file
For just some fun i am trying to use Flat File databases to read
Suppose both parent and child use one pipe for writing and reading means when
Is it possible to use overlapped I/O with an anonymous pipe? CreatePipe() does not
Use case: 3rd party application wants to programatically monitor a text file being generated
Use case: I've just entered insert mode, and typed some text. Now I want
I'm trying to write a function, that would use native openssl to do some
Calling fclose() here after dup() ing its file descriptor blocks until the child process
How can I, in Windows, use the output of a script/command as an argument

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.