Мне нужно создать связь между родителем и разветвленным дочерним элементом с помощью каналов. Родитель отправит 0, потомок отправит обратно 1, родитель ответит 2 и т. д., пока счетчик не достигнет 5. Затем оба процесса завершатся.
Я попытался реализовать это с помощью двух каналов:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int fd[2];
int fd2[2];
int val = 0;
// create pipe descriptors
pipe(fd);
pipe(fd2);
// fork() returns 0 for child process, child-pid for parent process.
if (fork() != 0) {
///0. Send: 0
// parent: writing only, so close read-descriptor.
close(fd[0]);
// send the value on the write-descriptor.
val = 0;
resend:
write(fd[1], &val, sizeof(val));
printf("0.Parent(%d) send value: %d\n", getpid(), val);
// close the write descriptor
close(fd[1]);
///3. Receive 1;
close(fd2[1]);
// now read the data
read(fd2[0], &val, sizeof(val));
printf("3.Parent(%d) received value: %d\n", getpid(), val);
// close the read-descriptor
close(fd2[0]);
wait(NULL);
val++;
if (val < 6)
goto resend;
else
return 0;
} else { // child: reading only, so close the write-descriptor
childStart:
wait(NULL);
///1. receive: 0
close(fd[1]);
// now read the data (will block)
read(fd[0], &val, sizeof(val));
printf("1.Child(%d) received value: %d\n", getpid(), val);
// close the read-descriptor
//close(fd[0]);
///2. Send: 1
// parent: writing only, so close read-descriptor.
close(fd2[0]);
// send the value on the write-descriptor.
val = val + 1;
write(fd2[1], &val, sizeof(val));
printf("2.Child(%d) send value: %d\n", getpid(), val);
// close the write descriptor
close(fd2[1]);
if(val <5) {
wait(NULL);
goto childStart;
}
else
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
return 0 ;
}
Я получаю следующий вывод:
0.Parent(120978) send value: 0
1.Child(120979) received value: 0
2.Child(120979) send value: 1
1.Child(120979) received value: 1
2.Child(120979) send value: 2
1.Child(120979) received value: 2
2.Child(120979) send value: 3
1.Child(120979) received value: 3
2.Child(120979) send value: 4
1.Child(120979) received value: 4
2.Child(120979) send value: 5
3.Parent(120978) received value: 1
0.Parent(120978) send value: 2
3.Parent(120978) received value: 2
0.Parent(120978) send value: 3
3.Parent(120978) received value: 3
0.Parent(120978) send value: 4
3.Parent(120978) received value: 4
0.Parent(120978) send value: 5
3.Parent(120978) received value: 5
Как я могу заставить процессы ждать сообщения друг друга?
resend: if (val < 6) goto resend;
Просто используйте циклwhile
... Вы выполняетеclose(fd[1]);
, затемgoto resend
и повторно используетеwrite(fd[1],
. Ну, как только вы закроетеfd[1]
, вы не сможете использовать его позже. - person KamilCuk   schedule 22.05.2020wait
в ребенке? - person William Pursell   schedule 22.05.2020