• PHP Video Tutorials

PHP - Direct I/O fcntl() Function



dio_fcntl() function can perform a function fcntl of the c-library on fd descriptor.

Syntax

mixed dio_fcntl( resource fd, int cmd [, mixed args] )

dio_fcntl() function can perform the operations specified by "cmd" parameter with a file descriptor fd, and some commands require additional "args" arguments.

An arg is an associative array when cmd is set to F_SETLK or F_SETLLW with the following keys: "start" is the mix where a lock starts, "length" is the size of a locked area. zero means an end of a file, "wenth" is where it refers to l_start: maybe SEEK_SET, SEEK_END, and SEEK_CUR, and "type" is a type of lock: can be F_RDLCK (read lock), F_WRLCK (write lock), or F_UNLCK (unlock).

The cmd can be one of the following operations −

  • F_SETLK − The lock is set or cleared. If a lock is held by someone else, dio_fcntl() function can return -1.

  • F_SETLKW − It similar to F_SETLK, but in the case when a lock is held by someone else, the dio_fcntl() function can wait until the lock is released.

  • F_GETLK − The dio_fcntl() function can return an associative array if anyone else prevents the lock. If there are no obstacles, the "type" key can be set by F_UNLCK.

  • F_DUPFD − Finds the smallest numbered file descriptor greater than or equal to arg, and can return it.

Example

<?php
   $fd = dio_open("/dev/ttyS0", O_RDWR);

   if(dio_fcntl($fd, F_SETLK, Array("type" => F_WRLCK)) == -1) {
      echo "The lock can not be cleared. It is held by someone else";
   } else {
      echo "Lock succesfully set/cleared";
   }
   dio_close($fd);
?>
php_function_reference.htm
Advertisements