[42 Seoul] pipex
open
#include <fcntl.h>
int open (const char *FILENAME, int FLAGS[, mode_t MODE];
OPTION
FLAGS | Desciption |
---|---|
O_RDONLY | 읽기전용 |
O_WRONLY | 쓰기전용 |
O_RDWR | 읽기쓰기전용 |
O_CREAT | 해당파일이 없다면 생성 |
O_EXCL O_CREAT | 없는 파일이면 만들고 존재하면 error |
O_TRUNC | 파일내용 삭제 |
This System call returns the value of file descriptor.
#include <unistd.h>
int execve(const char *pathname, char *const argv[], char *const envp[]);
pathname : 파일이름 argv : 파일인자 포인터 envp : 환경변수 포인터
perror
#include <stdio.h>
void perror(const char *string);
stderr로 오류메시지를 출력.
access
#include <unistd.h>
int access(const char *pathname, int mode);
This checks whether the calling process can access the file pathname.
returns 0, faliure returns -1
mode(Macro valuable) | description |
---|---|
R_OK | 읽기 권한 확인 |
W_OK | 쓰기 권한 확인 |
X_OK | 실행 권한 확인 |
F_OK | 파일 존재 확인 |
fork
PID : Process ID;
PPID : Parent Process ID;
PGID : Process Group ID;
SID : Session ID;
실행중인 프로세스 확인
ps -A -o pid,ppid,pgid,sid,command
#include <unistd.h>
pid_t fork(void);
자식프로세스를 생성한다. 부모프로세스는 자식 pid값을 반환하고, 자식프로세스는 0을 반환. 성공시 pid 실패시 -1
pid_t pid; // pid 변수선언
pid = fork(); // 실패시 -1, 부모에게 ppid,
waitpid
pid_t waitpid(pid_t pid, int *status, int options);
성공시 pid, 실패시 -1 리턴
|pid|description|
|:-|:-|
|-1|임의의 자식 프로세스를 기다린다|
|>1|해당 pid의 자식프로세스를 기다린다|
|==0|프로세스 그룹 id가 호출프로세스의 pid와 같은 자식프로세스를 기다린다|
|<-1|프로세스 그룹 id가 pid의 절댓값과 같은 자식프로세스를 기다린다 |
options | description |
---|---|
WCONTINUED | 중단되었다가 |
WNOHANG | 기다리는 PID가 종료되지않아 종료된 상태를 회수할 수 없을 때 반환값으로 0을 받음. |
WUNTRACED | 중단된 자식 프로세스의 상태를 받음. |
dup2
#include <unistd.h>
int dup2(int oldfd, int newfd);
oldfd를 복제하여 newfd에 할당한다.
newfd be allocated the lowest fd that was unused in calling process.
they share file offset, file status flags.
kill
프로세스에 signal을 보내는 command.
kill option
특정 프로세스 종료
kill `ps -ef | grep 프로세스이름 | grep -v grep | awk '{print $2}'`
pipe
#include <unistd.h>
int pipe(int fd[2]);
성공시
fd[0] | process -> pipe |
fd[1] | pipe -> process |
과제에서의 redirection 개요도
child process에서 infile을 인풋으로한 cmd1을 실행시켜 pipe에 아웃풋. pipe를 인풋으로 한 parent process에서 cmd2를 실행시켜 outfile로 아웃풋.
댓글남기기