1 / 5

리눅스 프로그래밍

리눅스 프로그래밍. 예제 2-8. 2006242038 방슬기. 문제 ex)2-8. 터미널 장치 파일로부터 5 초 이내에 응답이 들어오면 프로그램을 계속 수행하고 그렇지 않으면 종료하는 프로그램을 fcntl 함수와 O_NDELAY 플래그를 이용하여 프로그램을 작성하라 . 단 , 5 초간 입력을 기다릴 때는 sleep(5) 함수를 이용한다. fcntl 함수. 파일제어함수 사용법 int fcntl ( int fd , int cmd , struct flock * arg );

Download Presentation

리눅스 프로그래밍

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. 리눅스 프로그래밍 예제2-8 2006242038 방슬기

  2. 문제 ex)2-8 • 터미널장치 파일로부터 5초 이내에 응답이 들어오면 프로그램을 계속 수행하고 그렇지 않으면 종료하는 프로그램을 fcntl함수와 O_NDELAY 플래그를 이용하여 프로그램을 작성하라. 단, 5초간 입력을 기다릴 때는 sleep(5)함수를 이용한다.

  3. fcntl함수 • 파일제어함수 • 사용법 • intfcntl(intfd, intcmd, struct flock *arg); • fd: 열려있는 파일 디스크립터 • cmd: fcntl이 실행하는 동작을 지정 • arg : 파일 제어 인수

  4. fcntl함수명령어 기능 • F_GETFL : fd의 상태 플래그를 가져옴 • 반환값: 파일 상태의 플래그 값(양의 정수) • F_SETFL : fd의 상태 플래그를 세번째 인수로 주어진 정수 값으로 설정함. • 반환값: -1이 아닌 값

  5. 소스설명 5초간 입력이 없을 경우 print문을 출력 • #include <stdio.h> • #include <string.h> • #include <fcntl.h> • int main() • { • intfd,flags; • int open(), fcntl(); • inti,n; • char line[BUFSIZ]; • if((fd = open(“/dev/tty”,ORDONY|O_NDELAY)) == -1) • { • perror(“dev/tty”); • exit(2); • } • printf(“Enter your PIN within five seconds : \n”); • sleep(5); • if(read(fd,line,BUFSIZ) == -1) • { • printf(“\n ===>sorry.\n”); • exit(1); • } • flags = fcntl(fd,F_GETFL,0); • flags &= ~ONDELAY; • fcntl(fd,F_SETFL,flags); • printf(“Enter your bank account number : \n”); • read(fd,line,BUFSIZ); • i = strlen(line); • line[i++] = ‘\0’; • printf(“%s\n”,line); • } 예외처리 시스템오류검출함수 사용자가 5초내에 입력을 하였다면 은행계좌를 입력 받아 출력

More Related