파이프를 사용하여 두 프로세스를 동기화하는 방법이 있다고 생각하지만이를 구현하는 방법을 잘 모르겠습니다. 내 코드에서 자식 프로세스와 아버지 프로세스가 동시에 코드를 실행하므로 하나의 프로세스가 다른 프로세스를 기다리고 싶습니다. 예를 들어 파이프를 사용하여 다른 프로세스가 완료 될 때까지 한 프로세스를 차단합니다.파이프를 사용하여 아버지와 자식 프로세스를 어떻게 동기화합니까?
내 코드 : 내가 원하는처럼
int main()
{
int one[2];
int two[2];
int x=0;
char messageRead[256], messageRead2[256], messageWrite[256], messageWrite2[256];
pipe(one);
pipe(two);
pid_t pid = fork();
if(pid == 0) //child process
{
while (x==0) //loop until condition is met (didn't write the condition yet, just testing)
{
std::cout << "Child process.\n\n";
std::cin >> messageWrite;
write(one[1], messageWrite, 256);
read(two[0], messageRead2, 256);
}
}
else if(pid>0) //father process
{
while(x==0)
{
std::cout << "Father process.\n\n";
std::cin >> messageWrite;
write(two[1], messageWrite2, 256);
read(one[0], messageRead, 256);
}
}
}
는 지금, 두 프로세스가왔다 갔다하지만, 둘 다 같이 동시에 실행 : 작업 할 수
Father process.
Child process.
user input here
user input here
Father process.
Child process.
user input here
user input here
Child process.
Father process.
user input here
user input here
etc...
[poll (2)] (http://man7.org/linux/man-pages/man2/poll.2.html)과 같이 멀티플렉싱 시스템 콜을 사용하는 것을 고려해 볼 수 있으며 [Advanced Linux Programming] http://advancedlinuxprogramming.com/ –