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

时间:2012-10-01 14:11:08

标签: c

我为多个孩子创建管道。代码从键盘上捕获一个信号,然后孩子应该向父亲发送一条消息及其结果。信号的处理工作正常,但我不能将结果发送给父亲。我在这里做错了什么?

父进程的代码是:

#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");

}

儿童的代码是:

#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");

}

1 个答案:

答案 0 :(得分:0)

您的管道存在许多问题:

  • 在致电pipe后拨打fork,最终会得到两个独立的管道(一个在孩子身上,一个在父母身上)。父母听取它创建的那个(没有人写信),所以它永远不会看到任何东西。

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

  • 您不会在它们存在的各种过程中关闭不需要的管道末端,因此您无法可靠地获取EOF

如果您在stackoverflow上搜索pipe+child,您会看到大量问题,示例代码尝试执行与您正在执行的操作类似的操作 - 您可能会发现阅读这些问题很有用和答案。