Thread: timeout on write() for linux pipe

  1. #1
    Registered User
    Join Date
    Feb 2020

    Question timeout on write() for linux pipe

    How can I set timeout for write() on linux pipe ?

    example code:

    int fd_pipe = open("/run/some/pipe", O_RDWR);
    // here i need to set timeout for 3 seconds somehow, if can't write, code will continue...
    write(fd_pipe, something, strlen(something));
    // continue executing..

  2. #2
    Registered User
    Join Date
    Feb 2019
    Something like this?
    static void do_nothing(int sig) {}
    signal( SIGALRM, do_nothing );
    errno = 0;
    write( ... );
    if ( errno == EINTR ) puts( "timedout" );

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Note that you only get EINTR if the timeout happens before any data is written at all.

    If write() manages to write just one character, you need to check the return result to determine how much data has actually been written to the pipe.

    Open the file with O_NONBLOCK mode, then using select() with a timeout to handle the case where not all characters are written.

    I'm not aware of an API that will tell you in advance whether a pipe has space for N characters, such that you know in advance that the whole message can be written.

    cross-posted for reference -> timeout on write() for linux pipe - C++ Forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with pipe and exec in linux
    By matkuz in forum C Programming
    Replies: 2
    Last Post: 01-04-2016, 07:34 AM
  2. write to a pipe and so on
    By sharonch in forum Windows Programming
    Replies: 0
    Last Post: 12-07-2012, 01:41 PM
  3. pipe() read(), write() - more then 128Kb of data
    By chihwahli in forum C Programming
    Replies: 3
    Last Post: 11-01-2011, 04:32 AM
  4. Replies: 2
    Last Post: 12-06-2008, 02:17 PM
  5. write() with pipe()
    By und3rdog in forum C Programming
    Replies: 11
    Last Post: 05-03-2003, 10:57 PM

Tags for this Thread