Device Drivers/General

From Linux Drivers
Jump to: navigation, search

Blocking and Non-blocking

When an user application open a file by open() system call, it may set O_NONBLOCK or O_NDELAY flag which requests that the file is to be opened in nonblocking mode if supported.

int fd;

fd = open("/dev/my_driver", O_RDWR | O_NONBLOCK);

Also the user application can turns non blocking mode on or off by calling ioctl() system call with FIONBIO.

int flag;
/* non blocking mode */
flag = 1;
ioctl(fd, FIONBIO, &flag);
/* blocking mode */
flag = 0;
ioctl(fd, FIONBIO, &flag);

If the file was open by fopen() call, fcntl() can be used to enable or disable non-blocking mode

int flag;

flag = fcntl(fp, F_GETFL, 0);
/* non blocking mode */
fcntl(fp, F_SETFL, flag | O_NONBLOCK);
/* blocking mode */
fcntl(fp, F_SETFL, flag & ~O_NONBLOCK);

Kernel code can test whether O_NONBLOCK bit is set in f_flags field in struct file structure. like

int driver_open(struct inode *inode, struct file *file)
{
      if (file->f_flags & O_NONBLOCK) {
              /* non blocking mode */
      } else {
              /* blocking mode */
      }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox