TOC
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <errno.h>
typedef struct s_exec {
char **cmd_split;
char *cmd_path;
int fd;
int status;
} t_exec;
typedef struct s_pipe {
int write_fd;
int pipe_fd[2];
pid_t pid[2];
int status;
} t_pipe;
이미 존재하는 파일을 열거나 새로운 파일을 생성하는 system call 함수.
open으로 열린 파일은 read, write, lseek, close의 함수와 사용 가능하다.
#include <fcntl.h>
int open(const char *pathname, int flags);
pathname
: open할 파일에 대한 절대경로의 파일명 혹은 상대경로 파일명
flag
반환값
사용예시
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd_hello;
int fd_test;
fd_hello = open("hello.txt", O_RDONLY);
printf("fd_hello : %d\\n", fd_hello);
//OUTPUT : 3
fd_test = open("test.txt", O_RDONLY);
printf("fd_test : %d\\n", fd_test);
//OUTPUT : 4
return 0;
}
따라서 0, 1, 2는 시스템적으로 건들면 안되는 플래그이므로 정상적으로 open 한 파일의 위치를 가리키는 fd는 3부터 부여된다.
추가적으로, 동일한 파일은 두 번 open하면 3, 4 차례로 fd값이 부여됨.