1 / 12

예제 4-2

예제 4-2. 2007242039 김창현. F_RDLCK & F_WRLCK (1). F_RDLCK 이 된 파일을 다시 F_RDLCK 하려는 경우. F_RDLCK 이 된 파일을 F_WRLCK 하려는 경우. Block 되지 않고 F_RDLCK 상태로 된다. Block 된다. 사용자 1. 사용자 1. DATA F_RDLCK 상 태. DATA F_RDLCK 상 태. 사용자 2. 사용자 2. F_RDLCK & F_WRLCK (2).

Download Presentation

예제 4-2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 예제 4-2 2007242039 김창현

  2. F_RDLCK & F_WRLCK (1) F_RDLCK이 된 파일을 다시 F_RDLCK 하려는 경우 F_RDLCK이 된 파일을 F_WRLCK 하려는 경우 Block 되지 않고 F_RDLCK 상태로 된다. Block 된다. 사용자 1 사용자 1 DATA F_RDLCK 상태 DATA F_RDLCK 상태 사용자 2 사용자 2

  3. F_RDLCK & F_WRLCK (2) F_WRLCK이 된 파일을 F_RDLCK 하려는 경우 F_WRLCK이 된 파일을 다시 F_WRLCK 하려는 경우 Block 된다. Block 된다. 사용자 1 사용자 1 DATA F_WRLCK 상태 DATA F_WRLCK 상태 사용자 2 사용자 2

  4. 예제4-2 • LINUX의 화면 편집기(vi)로 파일을 편집하는동안 다른 사용자가 편집중인 파일에 접근할 수 없도록 lock시킬 수 있는 프로그램을 작성하라

  5. 예제 4-2 간단한 예 사용자 1 사용자2 DATA vi 편집기 vi 편집기 편집 LOCK 불가능 F_WRLCK

  6. 예제 4-2 Code (1) #include <fcntl.h>#include <errno.h>main(argc, argv)intargc ; char *argv[] ; { struct flock lock ; intfd, open(), fcntl() ; char command[100] ; if((fd= open(argv[1], O_RDWR)) == -1) { perror(argv[1]) ; exit(1) ; } 레코드 lock 구조체 선언 argv[1] open (읽기/쓰기)

  7. 예제 4-2 Code(2) lock.l_type = F_WRLCK ; lock.l_whence = 0 ; lock.l_start = 0L ; lock.l_len = 0L ; if(fcntl(fd, f_SETLK, &lock) == -1) { if(errno == EACEES) { printf(“%s busy –- try later\n”, argv[1]) ; exit(2) ; } perror(argv[1]) ; exit(3) ; } sprintf(command, “vi %s\n”, argv[1]) ; system(command) ; lock의 형태 : ‘쓰기’ 잠금 파일포인터 위치 : 처음부터 파일포인터 위치에 대한 시작점 구역의 길이(byte 단위) = 전체 열었던 파일에 대한 제어 : lock 을 참조하여 잠금상태 설정 argv[1] 이름으로 된 파일을 vi 에디터로 연다

  8. 예제 4-2 Code (3) • lock.l_type = F_UNLCK ; • fcntl(fd, F_SETLK, &lock) ; • close(fd) ; • } 잠금형태: 잠금해제 열었던 파일에 대한 제어 : lock 을 참조하여 잠금상태 설정

  9. 예제 4-2 사용 함수 및 상수 • 함수 • fcntl(intfildes, intcmd ,…/*arg*/) • 개방된 파일을 제어한다. • arg인수는 cmd에 종속되며, 각 인수는 파일의 잠금에 사용되며, 잠금은 파일 전체 및 파일의 세트먼트에 적용될 수 있다.

  10. 예제 4-2 사용 함수 및 상수 • 상수(fcntl() ) • F_SETLK • arg가 가르키는 구조체에 대하여 잠금을 설정 혹은해제한다. 설정 할 수 없을 경우 -1을 반환한다. • F_SETLKW • 다른 잠금에 의해 막힐 경우, fcntl이 잠금을 수행 할 수 있을 때 까지 막히는 것을 빼면 F_SETLK 와 같다. • F_GETLK • 잠금을 설정하지 않고 특정 잠금의 설정 여부를 검사한다.

  11. 예제 4-2 사용 함수 및 상수 • l_type의 종류 ( flock 구조체) • F_RDLCK : 잠금 형태가 읽기이다. • F_WRLCK : 잠금 형태가 쓰기이다. • F_UNLCK : 잠금을 해제한다 • ㅣ_whence 의 종류 ( flock 구조체) • SEEK_SET (=0) : 처음위치로 설정 • SEEK_CUR(=1) : 현재 위치에 offset을 더한 값 • SEEK_END(=2) : 파일 크기에 offset을 더한 값

  12. 예제 4-2 실행사진 & 실행

More Related