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