컴퓨터 공부/Advanced UNIX System Programming

[4월 28일 1교시] 파일 관련 시스템 콜2

려리군 2009. 4. 28. 09:13

추천사이트 : http://www.kldp.org


file descriptor 복제

#include<unistd.h>

int dup(int filedes);                       // 가장 작은 번호

int dup2(int filedes, int filedes2);    // filedes2 번호

기능 : 사용 중인 file descriptor 복사, dup의 의미는 duplicate

리턴 : 성공시 할당 받은 descriptor번호, 실패시 -1


dup()호출 예시


dup2() 사용

STDIN_FILENO, STDOUT_FILENO를 특정 파일로 입출력할 때 사용.

STDERR는 redirection 해도 STDERR로 간다.

STDOUT은 redirection 하면 파일로 저장 될 가능성이 있다.

표준 출력을 파일로 썼다고 다시 돌려놓는 방법 : http://kldp.org/node/59008


fcntl()

#include<unistd.h>

int fcntl(int filedes, int cmd, int arg);

기능 : 파일의 속성을 알아내거나 변경

cmd : 명령들.

arg : 명령에 따른 파라미터.


stat(), fstat(), lstat()

#include<sys/types.h>

#include<sys/stat.h>

int stat(const char *pathname, struct stat *buf);

int fstat(int filedes, struct stat *buf);

기능 : 주어진 파일에 대한 정보(struct stat)를 얻는다.

리턴 : 성공시 0, 실패시 -1

int lstat(const char *pathname, struct stat *buf);

기능 : Symbolic link file에 대한 자체 정보를 얻는다.


파일 타입

파일 타입설명매크로 함수
정규파일데이터로 구성된 text 또는 binary 파일S_ISREG()
디렉터리파일파일 이름과 포인터로 구성된 파일S_ISDIR()
문자특수파일Device파일. I/O시 임의 크기의 바이트 열 전송.S_ISCHR()
블록특수파일Device파일. I/O시 일정 크기의 데이터 전송.S_ISBLK()
FIFO프로세스간 통신에 사용되는 파일.(named pipe)S_ISFIFO()
소켓네트워크를 통한 프로세스 간 통신에 사용되는 파일.S_ISSOCK()
심볼릭 링크다른 파일을 가리키는 포인터 역할을 하는 파일S_ISLNK()