Home > Articles, Operating Systems > Useful functions for enabling and disabling the non-blocking I/O mode of file descriptors.

Useful functions for enabling and disabling the non-blocking I/O mode of file descriptors.

October 18, 2014 Leave a comment Go to comments

In this article I will present to you two useful functions that can be used whenever you want to enable or disable the non-blocking I/O mode of a file descriptor. By using the enable_io_blocking_for_file_descriptor and disable_io_blocking_for_file_descriptor functions you can enable and disable the I/O blocking of a file descriptor.

Here is the header of the source code unit providing the functions:

/*
 *  Copyright (C) 2014  Efstathios Chatzikyriakidis (contact@efxa.org).
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _FILE_DESCRIPTOR_UTILITY_H_
#define _FILE_DESCRIPTOR_UTILITY_H_

#include "shared-types.h"

/*
 * function prototypes.
 */

bool_t enable_io_blocking_for_file_descriptor (const int file_descriptor);

bool_t disable_io_blocking_for_file_descriptor (const int file_descriptor);

#endif // _FILE_DESCRIPTOR_UTILITY_H_

Here is the implementation of the source code unit providing the functions:

/*
 *  Copyright (C) 2014  Efstathios Chatzikyriakidis (contact@efxa.org).
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <fcntl.h>

#include "file-descriptor-utility.h"

/*
 * functions.
 */

bool_t
enable_io_blocking_for_file_descriptor (const int file_descriptor)
{
  int file_descriptor_flags;

  if ((file_descriptor_flags = fcntl (file_descriptor, F_GETFL, 0)) < 0) 
  {
    return false;
  }

  if (fcntl (file_descriptor,
             F_SETFL,
             file_descriptor_flags & ~O_NONBLOCK) < 0) 
  {
    return false;
  }

  return true;
}

bool_t
disable_io_blocking_for_file_descriptor (const int file_descriptor)
{
  int file_descriptor_flags;

  if ((file_descriptor_flags = fcntl (file_descriptor, F_GETFL, 0)) < 0) 
  {
    return false;
  }

  if (fcntl (file_descriptor,
             F_SETFL,
             file_descriptor_flags | O_NONBLOCK) < 0) 
  {
    return false;
  }

  return true;
}

Happy Hacking!

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: