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
- 균형잡힌 세상
- 바샤
- process control
- IT
- file IO
- system programming
- c++
- 브런치
- The Balance of the World
- 백준
- 1874
- 시스템 프로그래밍
- Process Communication
- 해바
- Baekjoon
- LJESNJAK
- 5622
- BAKA
- For Beginners
- File 조작
- 시프
- 4949
- 입력 버퍼
- 10773
- Zero That Out
- QA
- c
- 전자책
- 2941
- Parenthesis
Archives
- Today
- Total
해바
Lab #11 본문
두 개의 프로그램 parent.c(실행 파일 이름은 parent)와 child.c(child)를 다음과 같이 작성한다 :
- parent에서는 자식 프로세스(child)를 fork()와 exec() 시스템 호출을 이용하여 시작한다.
- parent에서 "parent: child initiated"라고 출력한다.
- child에서는 30sec 동안 sleep() 한다.
- 30sec가 되기 전에 parent에서 자식 프로세스에게 kill()을 사용하여 SIGTERM을 보낸다.
- child가 terminate 된 후 parent에서 다음과 같이 출력한다 : "Parent: child terminated"
// parent.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int main() {
pid_t pid;
if((pid = fork()) == -1) perror("fork");
else if(pid == 0) execlp("child.out", "child.out", (char*) 0);
else {
printf("Parent: child initiated\n");
sleep(5); // 적당히 지정
if(!kill(pid, SIGTERM)) perror("kill");
printf("Parent: child terminated\n");
}
return 0;
}
// child.c
#include <stdio.h>
int main() {
printf("Waiting 30 seconds...\n");
sleep(30);
/* 1초씩 보여주고 싶으면 이 코드로 변경
for (int i = 1; i <= 30; i++) {
printf("%d...\n", i);
sleep(1);
}*/
return 0;
}
컴파일 :
%gcc parent.c
%gcc child.c -o child.out
실행 :
%./a.out
Comments