Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- The Balance of the World
- File 조작
- 백준
- BAKA
- 전자책
- QA
- Zero That Out
- c
- 바샤
- 1874
- system programming
- 입력 버퍼
- For Beginners
- 2941
- c++
- file IO
- 균형잡힌 세상
- 브런치
- Process Communication
- 해바
- process control
- Parenthesis
- 4949
- 10773
- 5622
- LJESNJAK
- 시프
- 시스템 프로그래밍
- Baekjoon
- IT
Archives
- Today
- Total
해바
Lab #12 본문
pipe(), fork(), execlp(), system call 들을 사용하여 "ps -ef | grep telnet"의 동작을 구현한다.
- 부모 프로세스는 파이프를 통해서 "ps -ef" 명령 결과를 출력한다.
- 자식 프로세스는 부모 프로세스가 출력한 결과를 받아 "grep telnet" 명령을 수행한다.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
int fd[2];
pid_t pid;
if(pipe(fd) != 0) perror("pipe");
if((pid = fork()) == -1) perror("fork");
else if(pid == 0) {
close(fd[1]);
if(fd[0] != 0) {
dup2(fd[0], 0);
close(fd[0]);
}
execlp("grep", "grep", "telnet", (char*) 0);
}
else {
close(fd[0]);
if(fd[1] != 1) {
dup2(fd[1], 1);
close(fd[1]);
}
execlp("ps", "ps", "-ef", (char*) 0);
}
return 0;
}
컴파일 :
%gcc pipe.c
실행 :
%./a.out
Comments