소스 실행 안되는 이유가 궁금합니다.
리눅스에서 짠 소스인데요.. 제가 봤을땐.. 네임드 파이프 문제인것 같은데...
궁금한게 제가 알기론.. 네임드 파이프는 먼저 쓰기전용으로 열지 않으면 읽기용이나 그런게 안된다고 알고 있거든요..
그래서 RDWR로 열어놓으면.. 어떤 프로세스가 WRONLY로 열어 뭘 쓰기 전까지 대기하고 있지 않나요? 아닌가?
제가 짠 소스가 조잡해서.. ;; 간단히 설명하자면
부모프로세스가 자식을3개 만들고요.. 메시지를 보낼 자식과 받을 자식을 입력합니다. (자식1,2,3)
만약 1,2를 입력시 자식1이 자식2에게 메시지를 보내는데 여기서, 자식1이 부모에게 먼저 보내고 부모가 자식2에게 보내는 방식입니다.
너무 주먹구구식이라 알아보기 힘드시겠지만.. ㅡㅡ; (제가 실력이 별로여서.. ㅠ.ㅠ)
제 머리로는 잘 모르겠네요.. ㅡㅡ;
방법을 알고 싶습니다.
#include
#include
#include
#include
#include
#include
#include
void func1(void);
main()
{
pid_t pid1=0, pid2=0, pid3=0;
int p1[2], p2[2], p3[2];
char buffer[100], buffer2[100];
int inpro, outpro;
int nread, filedes;
fd_set initset;
if(pipe(p1) == -1)
{
printf("fail to make pipe1\n");
exit(1);
}
if(pipe(p2) == -1)
{
printf("fail to make pipe2\n");
exit(1);
}
if(pipe(p3) == -1)
{
printf("fail to make pipe3\n");
exit(1);
}
if(mkfifo("./trans1", 0666) == -1)
{
printf("fail to make trans1\n");
exit(1);
}
if(mkfifo("./trans2", 0666) == -1)
{
printf("fail to make trans2\n");
exit(1);
}
if(mkfifo("./trans3", 0666) == -1)
{
printf("fail to make trans3\n");
exit(1);
}
printf("메시지를 받아올 자식프로세스 번호와 전달할 프로세스 번호를 입력하시오 : ");
scanf("%d%d", &inpro, &outpro);
if((pid1=fork()) == -1)
{
printf("fail to call fork() to pid1\n");
exit(1);
}
if(pid1>0)
if((pid2=fork()) == -1)
{
printf("fail to call fork() to pid2\n");
exit(1);
}
if(pid1>0 && pid2>0)
if((pid3=fork()) == -1)
{
printf("fail to call fork() to pid3\n");
exit(1);
}
if(pid1==0 && pid2==0 && pid3==0)
{
close(p1[0]);
close(p2[0]);
close(p2[1]);
close(p3[0]);
close(p3[1]);
if(inpro==1)
{
printf("process1 input : ");
scanf("%s", buffer);
if(write(p1[1], buffer, 100) == -1)
{
printf("fail to write p1[1]\n");
exit(1);
}
}
if(outpro==1)
{
if((filedes=open("./trans1", O_RDWR)) == -1)
{
printf("fail to open trans1\n");
exit(1);
}
if((nread=read(filedes, buffer2, 100)) == -1)
{
printf("fail to read trans1\n");
exit(1);
}
printf("[child1] : 받은 메시지는 %s입니다.\n", buffer2);
}
}
if(pid1>0 && pid2==0 && pid3==0)
{
close(p1[0]);
close(p1[1]);
close(p2[0]);
close(p3[0]);
close(p3[1]);
if(inpro==2)
{
printf("process2 input : ");
scanf("%s", buffer);
if(write(p2[1], buffer, 100) == -1)
{
printf("fail to write p2[1]\n");
exit(1);
}
}
if(outpro==2)
{
if((filedes=open("./trans2", O_RDWR)) == -1)
{
printf("fail to open trans2\n");
exit(1);
}
if((nread=read(filedes, buffer2, 100)) == -1)
{
printf("fail to read trans2\n");
exit(1);
}
printf("[child2] : 받은 메시지는 %s입니다.\n", buffer2);
}
}
if(pid1>0 && pid2>0 && pid3==0)
{
close(p1[0]);
close(p1[1]);
close(p2[0]);
close(p2[1]);
close(p3[0]);
if(inpro==3)
{
printf("process3 input : ");
scanf("%s", buffer);
if(write(p3[1], buffer, 100) == -1)
{
printf("fail to write p3[1]\n");
exit(1);
}
}
if(outpro==3)
{
if((filedes=open("./trans3", O_RDWR)) == -1)
{
printf("fail to open trans3\n");
exit(1);
}
if((nread=read(filedes, buffer2, 100)) == -1)
{
printf("fail to read trans3\n");
exit(1);
}
printf("[child3] : 받은 메시지는 %s입니다.\n", buffer2);
}
}
if(pid1>0 && pid2>0 && pid3>0)
{
close(p1[1]);
close(p2[1]);
close(p3[1]);
FD_ZERO(&initset);
FD_SET(p1[0], &initset);
FD_SET(p2[0], &initset);
FD_SET(p3[0], &initset);
if(inpro==1 && outpro==2)
{
if(select(p3[0]+1, &initset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(p1[0], &initset))
{
if((nread=read(p1[0], buffer, 100)) == -1)
{
printf("fail to read p1[0]\n");
exit(1);
}
printf("[parent] : process1으로부터 받은 메시지는 %s입니다.\n", buffer);
}
}
strcpy(buffer2, buffer);
if((filedes=open("./trans2", O_WRONLY)) == -1)
{
printf("fail to open trans2 in parent\n");
exit(1);
}
if(write(filedes, buffer2, 100) == -1)
{
printf("fail to write trans2 in parent\n");
exit(1);
}
}
if(inpro==1 && outpro==3)
{
if(select(p3[0]+1, &initset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(p1[0], &initset))
{
if((nread=read(p1[0], buffer, 100)) == -1)
{
printf("fail to read p1[0]\n");
exit(1);
}
printf("[parent] : process1으로부터 받은 메시지는 %s입니다.\n", buffer);
}
}
strcpy(buffer2, buffer);
if((filedes=open("./trans3", O_WRONLY)) == -1)
{
printf("fail to open trans3 in parent\n");
exit(1);
}
if(write(filedes, buffer2, 100) == -1)
{
printf("fail to write trans3 in parent\n");
exit(1);
}
}
if(inpro==2 && outpro==1)
{
if(select(p3[0]+1, &initset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(p2[0], &initset))
{
if((nread=read(p2[0], buffer, 100)) == -1)
{
printf("fail to read p2[0]\n");
exit(1);
}
printf("[parent] : process2으로부터 받은 메시지는 %s입니다.\n", buffer);
}
}
strcpy(buffer2, buffer);
if((filedes=open("./trans1", O_WRONLY)) == -1)
{
printf("fail to open trans1 in parent\n");
exit(1);
}
if(write(filedes, buffer2, 100) == -1)
{
printf("fail to write trans1 in parent\n");
exit(1);
}
}
if(inpro==2 && outpro==3)
{
if(select(p3[0]+1, &initset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(p2[0], &initset))
{
if((nread=read(p2[0], buffer, 100)) == -1)
{
printf("fail to read p2[0]\n");
exit(1);
}
printf("[parent] : process2으로부터 받은 메시지는 %s입니다.\n", buffer);
}
}
strcpy(buffer2, buffer);
if((filedes=open("./trans3", O_WRONLY)) == -1)
{
printf("fail to open trans3 in parent\n");
exit(1);
}
if(write(filedes, buffer2, 100) == -1)
{
printf("fail to write trans3 in parent\n");
exit(1);
}
}
if(inpro==3 && outpro==1)
{
if(select(p3[0]+1, &initset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(p3[0], &initset))
{
if((nread=read(p3[0], buffer, 100)) == -1)
{
printf("fail to read p3[0]\n");
exit(1);
}
printf("[parent] : process3으로부터 받은 메시지는 %s입니다.\n", buffer);
}
}
strcpy(buffer2, buffer);
if((filedes=open("./trans1", O_WRONLY)) == -1)
{
printf("fail to open trans1 in parent\n");
exit(1);
}
if(write(filedes, buffer2, 100) == -1)
{
printf("fail to write trans1 in parent\n");
exit(1);
}
}
if(inpro==3 && outpro==2)
{
if(select(p3[0]+1, &initset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(p3[0], &initset))
{
if((nread=read(p3[0], buffer, 100)) == -1)
{
printf("fail to read p3[0]\n");
exit(1);
}
printf("[parent] : process3으로부터 받은 메시지는 %s입니다.\n", buffer);
}
}
strcpy(buffer2, buffer);
if((filedes=open("./trans2", O_WRONLY)) == -1)
{
printf("fail to open trans2 in parent\n");
exit(1);
}
if(write(filedes, buffer2, 100) == -1)
{
printf("fail to write trans2 in parent\n");
exit(1);
}
}
}
atexit(func1);
exit(0);
}
void func1(void)
{
unlink("./trans1");
unlink("./trans2");
unlink("./trans3");
}
댓글 달기