_locking

ファイルのロックまたはロック解除のバイト数。

int _locking(
   int fd,
   int mode,
   long nbytes 
);

パラメーター

  • fd
    ファイル記述子。

  • モード
    実行するアクションをロックします。

  • nbytes
    ロックするバイト数。

戻り値

_locking が正常終了した場合は 0 を返します。1 を返しerrno が次の値の 1 に設定すると失敗を示します。

  • EACCES
    違反のロック (既にロックされているファイルまたはロックを解除します)。

  • EBADF
    無効なファイル記述子。

  • EDEADLOCK
    違反をロックします。返される _LK_LOCK または _LK_RLCK フラグを指定するとファイルは10 の場合ロックすることはできません。

  • EINVAL
    無効な引数で _locking して指定されています。

エラーが パラメーターの検証 に説明されているように無効なパラメーターによって無効なファイル記述子など無効なパラメーター ハンドラーの場合に呼び出されます。

解説

_locking の関数は fd で指定されたファイルの nbytes バイトをロックしたりロックを解除します。ファイルのバイト列をロックすると他のプロセスによるバイトにアクセスできません。完全にロックまたはロック解除するファイル ポインターの現在位置から開始し次の nbytes バイトに移動します。EOF が見つかりました。バイトの過去のロックすることはできます。

モードは Locking.h で定義されているマニフェスト定数の 1 種類があります。

  • _LK_LOCK
    指定されたバイトをロックします。バイトがロックした場合プログラムは 12 番目の後に直ちにもう一度試します。は10 バイトの例ではロックすることはできないと定数はエラーを返します。

  • _LK_NBLCK
    指定されたバイトをロックします。バイトがロックした場合定数はエラーを返します。

  • _LK_NBRLCK
    _LK_NBLCK と同じ。

  • _LK_RLCK
    _LK_LOCK と同じ。

  • _LK_UNLCK
    前にロックされている) を指定されたバイトのロックを解除します。

重複しないファイルに複数の領域はロックできます。ロックが解除された領域は既にロックされている必要があります。_locking は隣接領域をマージできません ; 2 種類のロックされた領域が隣接している場合は各領域は別のロックを解除する必要があります。領域が短時間がロックされファイルを閉じるかまたはプログラムが終了する前にロックを解放する必要があります。

必要条件

ルーチン

必須ヘッダー

オプション ヘッダー

_locking

<io.h> および <sys/locking.h>

<errno.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

ライブラリ

C ランタイム ライブラリのすべてのバージョン。

使用例

// crt_locking.c
/* This program opens a file with sharing. It locks
 * some bytes before reading them, then unlocks them. Note that the
 * program works correctly only if the file exists.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>

int main( void )
{
   int  fh, numread;
   char buffer[40];

   /* Quit if can't open file or system doesn't 
    * support sharing. 
    */
   errno_t err = _sopen_s( &fh, "crt_locking.txt", _O_RDONLY, _SH_DENYNO, 
                          _S_IREAD | _S_IWRITE );
   printf( "%d %d\n", err, fh );
   if( err != 0 )
      exit( 1 );

   /* Lock some bytes and read them. Then unlock. */
   if( _locking( fh, LK_NBLCK, 30L ) != -1 )
   {
      long lseek_ret;
      printf( "No one can change these bytes while I'm reading them\n" );
      numread = _read( fh, buffer, 30 );
      buffer[30] = '\0';
      printf( "%d bytes read: %.30s\n", numread, buffer );
      lseek_ret = _lseek( fh, 0L, SEEK_SET );
      _locking( fh, LK_UNLCK, 30L );
      printf( "Now I'm done. Do what you will with them\n" );
   }
   else
      perror( "Locking failed\n" );

   _close( fh );
}

型 : crt_locking.txt

The first thirty bytes of this file will be locked.

出力例

No one can change these bytes while I'm reading them
30 bytes read: The first thirty bytes of this
Now I'm done. Do what you will with them

同等の .NET Framework 関数

System::IO::FileStream::Lock

参照

関連項目

ファイル処理

_creat、_wcreat

_open、_wopen