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
- IT
- BAKA
- Baekjoon
- c++
- Process Communication
- system programming
- For Beginners
- QA
- 시프
- Parenthesis
- 균형잡힌 세상
- The Balance of the World
- 4949
- 5622
- 전자책
- 해바
- 브런치
- 바샤
- 입력 버퍼
- Zero That Out
- c
- file IO
- 시스템 프로그래밍
- File 조작
- 1874
- 2941
- 백준
- LJESNJAK
- 10773
- process control
Archives
- Today
- Total
해바
Lab #10 본문
lab #9의 프로그램을 수정하여 다음의 프로그램을 작성하라 :
- 부모 프로세스는 자식 프로세스들의 종료를 기다리면서 종료하는 각 프로세스를 식별하여 메시지("Parent: First(또는 Second) Child: ")와 함께 종료 상태(status)를 출력하는 프로그램을 작성하라.
// status.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
int i = 0, status;
pid_t pids[2];
while(pids[i] = fork()) {
if(i + 1 == 2) break;
++i;
}
switch (pids[i]) {
case -1:
perror("fork");
case 0:
switch (i) {
case 0:
execl("/bin/echo", "echo", "This is Child 1", (char*) 0);
break;
case 1:
execl("/bin/echo", "echo", "This is Child 2", (char*) 0);
break;
}
break;
default:
printf("Parent: Waiting for children\n");
wait(&status);
if(WIFEXITED(status)) printf("Parent: First Child: %d\n", WEXITSTATUS(status));
else printf("Sig No.: %d\n", WTERMSIG(status));
wait(&status);
if(WIFEXITED(status)) printf("Parent: Second Child: %d\n", WEXITSTATUS(status));
else printf("Sig No.: %d\n", WTERMSIG(status));
printf("Parent: All Children terminated\n");
break;
}
return 0;
}
컴파일 :
%gcc status.c
실행 :
%./a.out
Comments