Skip to content

Instantly share code, notes, and snippets.

@m-renaud
Last active April 5, 2018 04:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-renaud/6096163 to your computer and use it in GitHub Desktop.
Save m-renaud/6096163 to your computer and use it in GitHub Desktop.
EINTR handling.
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment