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 |
31 |
Tags
- 입력 버퍼
- 바샤
- QA
- Zero That Out
- 5622
- 백준
- 10773
- LJESNJAK
- file IO
- 시프
- File 조작
- system programming
- 전자책
- 균형잡힌 세상
- For Beginners
- The Balance of the World
- IT
- Parenthesis
- 1874
- process control
- c++
- Process Communication
- c
- 해바
- Baekjoon
- 브런치
- BAKA
- 시스템 프로그래밍
- 4949
- 2941
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