在c中无法从使用多个管道的进程读取消息

在c中无法从使用多个管道的进程读取消息,c,C,我正在为多个子对象创建管道。该代码捕捉到来自键盘的信号,然后孩子应该向父亲发送一条消息,并给出结果。信号处理得很好,但我无法将结果发送给父亲。我做错什么了吗 父进程的代码是: #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <sys/types.h> pid_t *childs; //array for st

我正在为多个子对象创建管道。该代码捕捉到来自键盘的信号,然后孩子应该向父亲发送一条消息,并给出结果。信号处理得很好,但我无法将结果发送给父亲。我做错什么了吗

父进程的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>

pid_t *childs; //array for storing childs pids
int number_of_childs;//variable for the number of childs
int exit_count=0;
int *fd;

/*Handler for the SIGINT signal*/

void control_handler(int sig)
{
if (sig==SIGINT){

    int j,bytes;
    char message[512];
    /*Sending SIGUSR1*/

    for (j=0;j<number_of_childs;j++)
    {
    kill(childs[j],SIGUSR1);
    }

    /*Reading from PIPES*/

    for (j=0;j<number_of_childs;j++)
    {   
    close(fd[(2*j)+1]);  
    bytes = read(fd[(2*j)], message, sizeof(message));
    printf("Read from: %s\n",message);
    close(fd[2*j]);
    }
}

if (sig==SIGUSR2)
{
exit_count++;
}

}

main (int argc,char *argv[]){

int i,child_status;
char cast[512];
char cast2[512];
char cast3[512];
int pid;
number_of_childs=atoi(argv[1]);

signal(SIGINT,control_handler);
signal(SIGUSR2,control_handler);

/*Creating array for children pipes*/
fd=(int*)malloc((2*number_of_childs)*sizeof(int));


/*array that holds the pids for every child used in sending signals*/
childs=malloc(number_of_childs*sizeof (pid_t));



for (i=0;i<number_of_childs;i++){
pid=fork();
    /*Create pipes to communicate with all children*/

    if(pipe(fd+(2*i))==-1)
    {
    perror("pipe");exit(1);
    }

    /*Fathers code goes here*/

    if(pid!=0)
        {
        printf("Parent process: PID= %d,PPID=%d, CPID=%d \n",getpid(),getppid(),pid);
        childs[i]=pid; // Keep all your childs in an array
        printf("Child:%d\n",childs[i]); 
        }

    /*If you are a child*/

        else 
        {
        /*Change the code for the childs and set the time of execution*/
        sprintf(cast,"%d",i+1); // make the time char
        sprintf(cast2,"%d",(2*i)+1);    //make the pipe char
        sprintf(cast3,"%d",number_of_childs);   
        execl("./Child.out","",cast,cast2,cast3,NULL);
        }
    }   
        /*Father should never terminate*/               
        while (exit_count!=number_of_childs);
        printf("Father pospastex!!\n");

}
#包括
#包括
#包括
#包括
#包括
孩子们//用于存储childs PID的数组
孩子的整数//用于孩子数量的变量
int exit_count=0;
int*fd;
/*SIGINT信号的处理程序*/
无效控制处理器(int sig)
{
if(sig==SIGINT){
int j,字节;
字符消息[512];
/*发送SIGUSR1*/

对于(j=0;j而言,存在许多与管道相关的问题:

  • 您在调用
    fork
    之后调用
    pipe
    ,因此最终会得到两个独立的管道(一个在子管道中,一个在父管道中)。父管道侦听它创建的管道(没有人向其写入),因此它不会看到任何东西

  • 您将全局
    fd
    数组中的索引传递给子级,而不是管道的文件描述符。子级有自己的包含随机垃圾的全局
    fd
    数组,因此您实际上是在向随机文件描述符而不是管道写入

  • 在存在的各种过程中,您不会关闭不需要的管端,因此无法可靠地获得EOFs


如果您在stackoverflow上搜索此处,您将看到大量问题,其中示例代码试图执行与您正在执行的操作类似的操作--您可能会发现通读这些问题和答案很有用。

Thx很多。.在论坛中搜索了类似的问题,并在最后找到了正确的答案!效果很好!
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h> 
#include <string.h>
#define WRITE 0
#define READ 1


/*Global declerations*/

int alarmflag=0;
double result=0;
int count=0;
int global_pipe;
int *fd;

/*Handler for the alarm and SIGUSR1 signal*/
void signal_handler (int sig)
{

if(sig==SIGALRM)
{
printf("Im child with pid:%d im going to die my value is %lf \n",getpid(),result);
alarmflag=1;
}

if(sig==SIGUSR1)
{
count++;
char message[512];

    if(count==1)
    {
    close(fd[global_pipe-1]);
    sprintf(message,"%d,%lf",getpid(),result);
    write(fd[global_pipe],message,strlen(message)+1);
    close(fd[global_pipe]); 
    //printf("PID:%d report: %lf\n",getpid(),result);
    }
    if(count==2)
    {
    close(fd[global_pipe-1]);
    sprintf(message,"%d,%lf",getpid(),result);
    write(fd[global_pipe],message,strlen(message)+1);
    close(fd[global_pipe]); 
    //printf("PID:%d report2 : %lf\n",getpid(),result);
    //kill(getppid(),SIGUSR2);
    //exit(0);
    }
}
if(sig==SIGINT)
{
/*Do nothing*/ 
}

}


double p_calculation ()
{
 int i=2;
 result=3;
 double prosimo=-1;

 while(!alarmflag)
    {
 prosimo=prosimo*(-1);
 result=result+(prosimo*(4/((double)i*((double)i+1)*((double)i+2))));
 i=i+2;
    }

}

main(int argc,char *argv[])
{
int pipe;
int size_fd;

size_fd=(atoi(argv[3]));
pipe=(atoi(argv[2]));
global_pipe=pipe;

fd=(int*)malloc(size_fd*sizeof(int));

/*handling signals*/
signal(SIGALRM,signal_handler);
signal(SIGUSR1,signal_handler);
signal(SIGINT,signal_handler);

/*Notify for execution time*/
printf("PID : %d with PPID : %d executing for %d seconds \n",getpid(),getppid(),atoi(argv[1]));
//printf("pipe:%d\n",pipe);
/*end this after the value passed as argument*/
alarm(atoi(argv[1]));
p_calculation();

/*Notify for finish*/

printf("Done!!!\n");

}