티스토리 뷰
현재 운영체제 수업을 듣고 있는데, 과제로 리눅스를 사용하는 과제가 주어졌습니다.
내용은 즉슨 "두개의 프로세스를 실행하고, 서로 네임드 파이프라인 통신을 하게 하라. 단 각 프로세스는 fork를 통해 자식프로세스를 만들고, 자식은 전송을 부모는 읽고 출력하는 형식으로 제작하라. 그리고 Ctrl + C 가 입력되면 좀비 프로세스를 만들지 않고 종료하라" 라는 과제가 주어졌습니다. 그래도 교수님게서 사용할 기본 함수들은 알려주셔서 그나마 쉽게 풀어나갈 수 있었습니다.
통신 기본 코드
char *chatTtoJ = "./chatTtoJ";
char *chatJtoT = "./chatJtoT";
mkfifo(chatTtoJ, 0666);
mkfifo(chatJtoT, 0666);
fdr = open(chatTtoJ, ?);
fdw = open(chatJtoT, ?);
부모 코드
while(read( ? )>0 ) {
// 입출력은 본인이 편한 방식으로 사용할 것
printf( ? );
sleep(1);
}
시그널 코드
signal(SIGCHILD, ? );
signal(SIGINT, ? );
위 코드들을 토대로 아래 프로그램을 제작하였습니다.
Tom.c
더보기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>
pid_t pid;
int P_pid;
int end=0;
static void signal_handler(int signo){
if(signo == SIGINT){
end = 1;
}
else if(signo == SIGCHLD){
int state;
int zompid = wait(&state);
if(zompid)
if(state != -1)
printf("부모프로세스 : 자식 PID=%d, 종료 코드=%d\n", zompid, WEXITSTATUS(state));
end = 2;
}
}
int main(){
char input[80], output[80];
int fdw, fdr;
char *chatTtoJ = "./chatTtoJ";
char *chatJtoT = "./chatJtoT";
mkfifo(chatTtoJ, 0666);
mkfifo(chatJtoT, 0666);
fdw = open(chatTtoJ, O_WRONLY);
fdr = open(chatJtoT, O_RDONLY);
signal(SIGINT, signal_handler);
signal(SIGCHLD, signal_handler);
pid = fork();
if(pid == 0){//자식 프로세스에 의해 실행되는 코드
while(1){
if(end == 1){
printf("Child Break : %d \n", getpid());
exit(100);
}
printf(">>>");
fgets(output, 80, stdin);
write(fdw, output, strlen(output)+1);
sleep(0);
}
}
else if(pid > 0){ //부모 프로세스
while(1){
int i = 0, n = 0;
while((n = read(fdr, &input[i], 1))>0){
if(input[i] == '\0') break;
if(i == 78){
input[++i] = '\0';
break;
}
i++;
}
if(end == 2){
close(fdr);
close(fdw);
return 0;
}
else if(n<=0){
end = 1;
}
printf("\nInput : %s\n", input);
sleep(1);
}
}
else{
printf("fork 오류");
return 0;
}
}
Jerry.c
더보기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>
pid_t pid;
int P_pid;
int end=0;
static void signal_handler(int signo){
if(signo == SIGINT){
end = 1;
}
else if(signo == SIGCHLD){
int state;
int zompid = wait(&state);
if(zompid)
if(state != -1)
printf("부모프로세스 : 자식 PID=%d, 종료 코드=%d\n", zompid, WEXITSTATUS(state));
end = 2;
}
}
int main(){
char input[80], output[80];
int fdw, fdr;
char *chatTtoJ = "./chatTtoJ";
char *chatJtoT = "./chatJtoT";
mkfifo(chatTtoJ, 0666);
mkfifo(chatJtoT, 0666);
fdr = open(chatTtoJ, O_RDONLY);
fdw = open(chatJtoT, O_WRONLY);
signal(SIGINT, signal_handler);
signal(SIGCHLD, signal_handler);
pid = fork();
if(pid == 0){//자식 프로세스에 의해 실행되는 코드
while(1){
if(end == 1){
printf("Child Break : %d \n", getpid());
exit(100);
}
printf(">>>");
fgets(output, 80, stdin);
write(fdw, output, strlen(output)+1);
sleep(0);
}
}
else if(pid > 0){ //부모 프로세스
while(1){
int i = 0, n = 0;
while((n = read(fdr, &input[i], 1))>0){
if(input[i] == '\0') break;
if(i == 78){
input[++i] = '\0';
break;
}
i++;
}
if(end == 2){
close(fdr);
close(fdw);
return 0;
}
else if(n<=0){
end = 1;
}
printf("\nInput : %s\n", input);
sleep(1);
}
}
else{
printf("fork 오류");
return 0;
}
}
'서버 공부 > 시스템 프로그래밍' 카테고리의 다른 글
메모리 관리 - 힙(Heap) 컨트롤 (0) | 2022.07.29 |
---|---|
쓰레드 동기화 기법 - NamedMutex와 WAIT_ABANDONED (0) | 2022.07.29 |
쓰레드 동기화 기법 - 커널 모드 동기화 (0) | 2022.07.28 |
쓰레드 동기화 기법 - 유저 모드 동기화 (0) | 2022.07.28 |
쓰레드 구현 모델 (0) | 2022.07.28 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 지뢰찾기
- 멀티쓰레드
- 보안
- 고양이
- 컨퍼런스
- 링크드 리스트
- 인제대학교
- 백준
- 시스템보안
- Select모델
- 자료구조
- c++
- Dreamhack
- 개발
- queue
- 학교
- 야경
- 알고리즘
- BFS
- STL
- 스레드풀
- 정보보안
- 워셜알고리즘
- 더블버퍼링
- 드림핵
- 레지스터
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함