在C和Linux中执行IPC管道时,输出行为突然

在C和Linux中执行IPC管道时,输出行为突然,c,linux,pipe,ipc,C,Linux,Pipe,Ipc,完整的代码是 #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

完整的代码是

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
int pc[2];//父对象写入子对象,子对象从父对象读取
int cp[2];//父项从子项读取,子项写入父项
pid\u t ck\u pid;//检查分叉pid
如果(管道(pc)=-1)
{ 
fprintf(stderr,“管道故障-pc\n”);
返回1;
}
如果(管道(cp)=-1)
{
fprintf(stderr,“管道故障-cp\n”);
返回1;
}
ck_pid=fork();
如果(ck_pid=5)
{
winner_avl=真;
}
//等待(空);
}
printf(“完成任务的时间,最终值:%d\n”,最终值);
strcpy(wb,“滚出去”);
关闭(pc[0]);
写入(pc[1],wb,strlen(wb)+1);
等待(空);
关闭(cp[0]);
关闭(cp[1]);
关闭(pc[0]);
关闭(pc[1]);
}
其他的
{
printf(“分叉问题”);
}    
返回0;
}
问题在于第一次迭代后屏幕冻结

转弯ID:0

计数为0的父级(17254)

计数为0的子级(17255)

来自父级的内容是:开始

阅读儿童成功

来自子级的内容为:0

最终值现在为:0

回合ID:1父回合(17254),计数:1

如果我正常地遵循关闭未使用的管道末端,我不会得到任何数据

转弯ID:0

计数为0的父级(17495)

计数为0的子项(17496)

来自父级的内容是:开始

阅读儿童成功

来自子级的内容为:0

最终值现在为:0

转弯ID:1

父级(17495),计数为1

来自子级的内容为:0

最终值现在为:0

转弯ID:2

父级(17495),计数:2

来自子级的内容为:0

最终值现在为:0

转弯ID:3

家长(17495人),人数:3人

来自子级的内容为:0

最终值现在为:0

转弯ID:4

家长(17495人),人数:4

来自子级的内容为:0

最终值现在为:0

完成事情的时间,最终:0


请帮助。

在等待之前关闭文件描述符一次(您尝试关闭一些描述符两次)。非常感谢。我将[close(pc[1]);//在读取pc之前关闭写入端],[close(cp[0]);//在写入cp之前关闭读取端]从子对象的while循环中取出,并将[close(pc[0]);//在写入pc之前关闭读取端],[close(cp[1]);//在读取cp之前关闭写入端]从父对象的while循环中取出。这就成功了。