리눅스 프로그래밍을 하다가 문제가 생겼습니다.

kcycj의 이미지

대학교에서 운영체제를 배우고 있는 학생입니다.

우분투에서 Operating system concepts 9판 교재 141~142 페이지에 있는 코드를 그대로 작성해 실행해봤는데

원하는 결과가 나오지 않아 질문드립니다.

간단한 파이프통신 문제같은데 터미널 화면에서 글씨가 깨져서 나온 것 같습니다.

소스코드는 다음과 같으며 결과화면은 캡쳐하여 첨부하였습니다.

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
#define BUFFER_SIZE 25
#define READ_END 0
#define WRITE_END 1
 
 
 
int main(void)
{
char write_msg[BUFFER_SIZE]="Greetings";
char read_msg[BUFFER_SIZE];
int fd[2]={0,1};
pid_t pid;
 
	// create the pipe
	if(pipe(fd)==-1)
	{
		fprintf(stderr,"Pipe failed");
		return 1;
	}
 
	//fork a child process
	pid=fork();
 
	//parent process
	if(pid<0)
	{
		//close the unused end of the pipe
		close(fd[READ_END]);
 
		//write to the pipe
		write(fd[WRITE_END],write_msg,strlen(write_msg)+1);
 
		//close the read end of the pipe
		close(fd[WRITE_END]);
	}
	//child process
	else
	{
		//close the unused end of the pipe
		close(fd[WRITE_END]);
 
		//read from the pipe
		read(fd[READ_END],read_msg,BUFFER_SIZE);
		printf("read %s",read_msg);
 
		//close the write end of the pipe
		close(fd[READ_END]);
	}
	return 0;	
}

File attachments: 
첨부파일 크기
Image icon aaa.jpg85.55 KB
kcycj의 이미지

글삭제하는법을 모르겠네요..

두번째 if문에서 if(pid>0)을 if(pid<0)이라고 잘못썻습니다ㅠㅠ