Полный код:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdbool.h>
#include<string.h>
#include<sys/wait.h>
#include<sys/types.h>
int main()
{
int pc[2]; // parent writes to child, child reads from parent
int cp[2]; // parent reads from child, childs writes to parent
pid_t ck_pid; // checking fork pid
if (pipe(pc)==-1)
{
fprintf(stderr, "Pipe Failed - pc \n" );
return 1;
}
if (pipe(cp)==-1)
{
fprintf(stderr, "Pipe Failed - cp \n" );
return 1;
}
ck_pid=fork();
if (ck_pid<0)
{
perror("Failure : Child Process Creation \n");
return 1;
}
else if (ck_pid==0)
{
// code for child
//int lc1=0;
char rb[100];
char wb[100];
int str_cpr_r;
int str_cpr_r_1;
char t_buff_1[100];
char t_buff_2[100];
int seq_cnt_1=0;
//for (lc1=0;lc1<5;lc1++)
//{
while(true)
{
pid_t c_tid;
c_tid=getpid();
printf("Child (%d) with count : %d \n", c_tid, seq_cnt_1);
// 0 is the reading end, 1 is the writing end
// pc is child reads from parent
//close(pc[1]); // closing writing end before reading pc
read(pc[0], rb, sizeof(rb));
//close(pc[0]); // closing after reading pc
printf("Content from Parent is : %s \n", rb);
//sprintf(t_buff_1, "get-started-%d", seq_cnt_1);
strcpy(t_buff_1, "get-started");
str_cpr_r=strcmp(rb, t_buff_1);
strcpy(t_buff_2, "get-out");
str_cpr_r_1=strcmp(rb, t_buff_1);
if (str_cpr_r==0)
{
printf("Read in child successful \n");
//strcpy(wb, "OK");
sprintf(wb, "%d", (seq_cnt_1*5));
// cp is childs writes to parent
//close(cp[0]); // closing reading end before writing cp
write(cp[1], wb, strlen(wb)+1);
//close(cp[1]); // closing after writing cp
}
if (str_cpr_r_1==0)
{
close(cp[0]);
close(cp[1]);
close(pc[0]);
close(pc[1]);
exit(0);
}
}
//exit(0);
seq_cnt_1++;
}
else if (ck_pid>0)
{
// code for parent
//int lc2;
//for (lc2=0;lc2<5;lc2++)
//{
//pipe(pc);
//pipe(cp);
bool winner_avl=false;
int seq_cnt=0;
char wb[100];
char rb[100];
pid_t p_tid;
int final_value=0;
while (winner_avl==false)
{
printf("Turn ID : %d \n", seq_cnt);
p_tid=getpid();
printf("Parent (%d) with count : %d \n", p_tid, seq_cnt);
// 0 is the reading end, 1 is the writing end
strcpy(wb, "get-started");
//sprintf(wb, "get-started-%d", seq_cnt);
// pc is parent writes to child
//close(pc[0]); // closing reading end before writing pc
write(pc[1], wb, strlen(wb)+1);
//close(pc[1]); // closing after writing pc
sleep(10);
// cp is parent reads from child
//close(cp[1]); // closing writing end before reading cp
read(cp[0], rb, sizeof(rb));
//close(cp[0]); // closing after reading cp
printf("Content from Child is : %s \n", rb);
final_value=final_value+atoi(rb);
printf("Final value is now : %d \n\n\n", final_value);
//int str_cpr_r;
/*str_cpr_r=strcmp(rb, "OK");
if (str_cpr_r==0)
{
printf("Read in parent successful \n");
}
*/
seq_cnt++;
if (final_value>=10||seq_cnt>=5)
{
winner_avl=true;
}
//wait(NULL);
}
printf("Time to finish things, Final : %d \n", final_value);
strcpy(wb, "get-out");
close(pc[0]);
write(pc[1], wb, strlen(wb)+1);
wait(NULL);
close(cp[0]);
close(cp[1]);
close(pc[0]);
close(pc[1]);
}
else
{
printf("Problem in forking");
}
return 0;
}
Проблема в том, что после первой итерации экран зависает
Идентификатор поворота: 0
Родитель (17254) со счетом: 0
Ребенок (17255) со счетом: 0
Содержание от родителя: начало работы
Чтение у ребенка успешно
Содержимое от ребенка: 0
Окончательное значение теперь: 0
Идентификатор поворота: 1 родитель (17254) со счетом: 1
Если я обычно следую за закрытием неиспользованных концов труб, я не получаю никаких данных
Идентификатор поворота: 0
Родитель (17495) со счетчиком: 0
Ребенок (17496) с количеством: 0
Содержимое от Родителя: начало работы
Чтение на ребенке успешно
Содержимое от Ребенка: 0
Окончательное значение теперь: 0
Идентификатор поворота: 1
Родитель (17495) со счетом: 1
Содержимое от ребенка: 0
Окончательное значение теперь: 0
Идентификатор поворота: 2
Родитель (17495) с количеством: 2
Содержимое от ребенка: 0
Окончательное значение теперь: 0
Идентификатор поворота: 3
Родитель (17495) с количеством: 3
Содержимое от ребенка: 0
Окончательное значение теперь: 0
Идентификатор поворота: 4
Parent (17495) с количеством: 4
Содержание от Child: 0
Окончательное значение теперь: 0
Время до конца sh вещей, Final: 0
Пожалуйста, помогите.