Skip to content

Instantly share code, notes, and snippets.

@m-renaud
Last active April 5, 2018 04:49

Revisions

  1. m-renaud revised this gist Jul 29, 2013. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions eintr-handling.c
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,3 @@
    //===========================================================================
    // Copyright (c) 2012 Matt Renaud. All Rights Reserved.
    //
    //===========================================================================

    // Examples of EINTR handling for reentrant functions.

    #include <errno.h>
    @@ -21,7 +16,8 @@ int myopen(char const* pathname, int flags)
    {
    errno = 0;
    ret_val = open(pathname, flags);
    } while(ret_val == -1 && errno == EINTR);
    }
    while(ret_val == -1 && errno == EINTR);

    return ret_val;
    }
    @@ -39,6 +35,7 @@ ssize_t myread(int fildes, void* buf, size_t nbyte)
    numbytes = read(fildes, buf, nbyte);
    }
    while (numbytes == -1 && errno == EINTR);

    return numbytes;
    }

    @@ -56,6 +53,7 @@ ssize_t mywrite(int fildes, void const* buf, size_t nbyte)
    numbytes = write(fildes, buf, nbyte);
    }
    while (numbytes == -1 && errno == EINTR);

    return numbytes;
    }

    @@ -69,7 +67,8 @@ int myclose(int fd)
    {
    errno = 0;
    ret_val = close(fd);
    } while(ret_val == -1 && errno == EINTR);
    }
    while(ret_val == -1 && errno == EINTR);

    return ret_val;
    }
  2. m-renaud created this gist Jul 27, 2013.
    75 changes: 75 additions & 0 deletions eintr-handling.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    //===========================================================================
    // Copyright (c) 2012 Matt Renaud. All Rights Reserved.
    //
    //===========================================================================

    // Examples of EINTR handling for reentrant functions.

    #include <errno.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>

    //m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Open can be interupted when blocked, waiting for a slow device such as a
    // FIFO to open.
    int myopen(char const* pathname, int flags)
    {
    int ret_val;
    do
    {
    errno = 0;
    ret_val = open(pathname, flags);
    } while(ret_val == -1 && errno == EINTR);

    return ret_val;
    }

    //m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    ssize_t myread(int fildes, void* buf, size_t nbyte)
    {
    ssize_t numbytes = 0;

    // Loop while the read function is interupted. Set errno in the loop
    // to make sure that another system call has not set it.
    do
    {
    errno = 0;
    numbytes = read(fildes, buf, nbyte);
    }
    while (numbytes == -1 && errno == EINTR);
    return numbytes;
    }



    //m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    ssize_t mywrite(int fildes, void const* buf, size_t nbyte)
    {
    // Loop while the write function is interupted. Set errno in the loop
    // to make sure that another system call has not set it.
    ssize_t numbytes = 0;
    do
    {
    errno = 0;
    numbytes = write(fildes, buf, nbyte);
    }
    while (numbytes == -1 && errno == EINTR);
    return numbytes;
    }



    //m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    int myclose(int fd)
    {
    int ret_val;
    do
    {
    errno = 0;
    ret_val = close(fd);
    } while(ret_val == -1 && errno == EINTR);

    return ret_val;
    }