상세 컨텐츠

본문 제목

UNIX system call - create

Computer Science/UNIX

by 2021. 9. 16. 18:02

본문

반응형

create(2)

create system call은 

#include <fcntl.h> 

int creat(const char *pathname, mode_t mode);
// Returns: file descriptor if OK, -1 on error

pathname 파일을 mode에 해당하는 permission을 주고 생성하는 명령어이다. mode_t에 들어가는 permission '0 + 3자리 십진수' 형태로 입력 가능한데 3자리 십진수가 9비트의 permission bit를 의미하여 0644일 경우 '-rw-r--r--'을 의미한다. 

 

create 말고도 파일을 생성하는 방법이 있는데, 다음과 같다.

fd = creat(“/tmp/newfile”, 0644); // 0644는 permission

fd = open(“/tmp/newfile”, O_WRONLY|O_CREAT|O_TRUNC, 0644);

open의 경우 파일이 있다면 truncate하여 빈 파일로 시작하고, 없을 경우 새로 생성하므로 create와 같이 동작한다.

create는 항상 truncate로 동작하며, 항상 write only로 동작한다.

 

Owner and permission of a new file

새로운 파일을 open이나 create로 생성하는 경우 parent directory에 그 이름이 쓰여지므로 해당 디렉토리에 대한 write permission이 있어야 한다. (parent뿐 아니라 root경로에 대한 권한까지 있어야 함)

 

Owner

파일을 생성할 때 owner는 effective user-ID로부터 설정된다. (user-ID에는 read user-ID와 effective user-ID가 있다.)

group의 경우 effective group-ID로부터 설정된다.(ID와 마찬가지로 read group-ID와 effective user-ID가 존재)

반응형

'Computer Science > UNIX' 카테고리의 다른 글

UNIX system call - write  (0) 2021.09.18
UNIX system call - close, read  (0) 2021.09.17
UNIX - File permissions 파일 권한  (0) 2021.09.15
UNIX system call - open  (0) 2021.09.14
UNIX의 파일체계  (0) 2021.09.13

관련글 더보기