_locking

파일의 바이트를 잠그거나 잠금 해제합니다.

구문

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

매개 변수

fd
파일 설명자입니다.

mode
수행할 잠금 작업입니다.

nbytes
잠글 바이트 수입니다.

반환 값

성공하면 _locking은 0을 반환합니다. 반환 값 -1은 실패를 나타내며, 이 경우 errno 다음 값 중 하나로 설정됩니다.

errno Condition
EACCES 잠금 위반(파일이 이미 잠겨 있거나 잠금 해제됨)입니다.
EBADF 잘못된 파일 설명자입니다.
EDEADLOCK 잠금 위반입니다. 또는 _LK_RLCK 플래그가 _LK_LOCK 지정되고 10번의 시도 후에 파일을 잠글 수 없는 경우 반환됩니다.
EINVAL _locking에 잘못된 인수가 지정되었습니다.

잘못된 파일 설명자와 같은 잘못된 매개 변수로 인해 오류가 발생하는 경우 매개 변수 유효성 검사에 설명된 대로 잘못된 매개 변수 처리기가 호출됩니다.

설명

함수는 _locking 로 지정된 fd파일의 바이트를 잠그거나 잠금 해제 nbytes 합니다. 파일의 바이트를 잠그면 다른 프로세스에서 해당 바이트에 액세스할 수 없습니다. 모든 잠금 또는 잠금 해제는 파일 포인터의 현재 위치에서 시작하여 다음 nbytes 바이트에 대해 진행됩니다. 파일의 끝을 지나 바이트를 잠글 수 있습니다.

mode 는 Locking.h에 정의된 다음 매니페스트 상수 중 하나여야 합니다.

mode 효과
_LK_LOCK 지정된 바이트를 잠급니다. 바이트를 잠글 수 없는 경우 프로그램은 1초 후에 즉시 다시 시도합니다. 10번의 시도 후에 바이트를 잠글 수 없는 경우 상수는 오류를 반환합니다.
_LK_NBLCK 지정된 바이트를 잠급니다. 바이트를 잠글 수 없는 경우 상수는 오류를 반환합니다.
_LK_NBRLCK _LK_NBLCK와 동일합니다.
_LK_RLCK _LK_LOCK와 동일합니다.
_LK_UNLCK 지정된 바이트를 잠금 해제합니다. 이러한 바이트는 이미 잠겨 있어야 합니다.

겹치지 않는 파일의 여러 영역을 잠글 수 있습니다. 잠금 해제할 영역은 이미 잠겨 있어야 합니다. _locking 인접한 지역을 병합하지 않습니다. 두 개의 잠긴 영역이 인접하면 각 영역의 잠금을 별도로 해제해야 합니다. 영역은 일시적으로만 잠가야 하며 파일을 닫거나 프로그램을 종료하기 전에 잠금을 해제해야 합니다.

기본적으로 이 함수의 전역 상태는 애플리케이션으로 범위가 지정됩니다. 이 동작을 변경하려면 CRT의 전역 상태를 참조하세요.

요구 사항

루틴에서 반환된 값 필수 헤더 선택적 헤더
_locking <io.h> 및 <sys/locking.h> <errno.h>

호환성에 대한 자세한 내용은 호환성을 참조하세요.

라이브러리

모든 버전의 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

참고 항목

파일 처리
_creat, _wcreat
_open, _wopen