c - 命名管道 Linux

标签 c linux named-pipes

我尝试运行这段代码,但是当我从管道读取时,没有任何信息:

Statitics:
Hora de inicio:(null)
Total pedidos:0
Pedidos recusados:0
Dominios locais:0
Dominios externos:0
Hora atual:17:5:14

我不知道出了什么问题...我需要帮助。

typedef struct{
   int pedidos;
   int loc;
   int ext;
   int rej;
   char* start;
} esta;
int fdpipe;
fd_set write_set;

//PROCESS 1--------------------------------------------------------------

作者:

esta* estatisticas;

estatisticas=(esta* )malloc(sizeof(esta));
estatisticas->start=convertTime(time(NULL));

 estatisticas->ext=1;
 estatisticas->loc=1;
 estatisticas->rej=1;
estatisticas->pedidos=1;

if (mkfifo(mpconfs->pipe_stats,0600)<0)
{
        perror("Cannot create pipe: ");
        exit(0);
}
//escrever no pipe
if ((fdpipe=open(mpconfs->pipe_stats, O_WRONLY)) < 0)
{
        perror("Cannot open pipe for writing: ");
        exit(0);
}

while(1) {
    //escrever no pipe
    FD_ZERO(&write_set);

    FD_SET(fdpipe, &write_set);



    if (select(fdpipe+1, NULL, &write_set, NULL, NULL)>0) {
        if(FD_ISSET(fdpipe,&write_set)){
            write(fdpipe, &estatisticas, sizeof(esta));
        }
    }
}

读者: //进程2-------------------------------------------- --------------

fd_set read_set;
esta* estatisticas;
signal(SIGINT,catch_ctrlcProcEsts);

sleep(2);
if ((fdpipe=open(mpconfs->pipe_stats, O_RDWR)) < 0)
{
    perror("Cannot open pipe for reading: ");
    exit(0);
}

while(1){

    FD_ZERO(&read_set);
    // prepares read set to "listen" to the following FD
    FD_SET(fdpipe, &read_set);
    if (select(fdpipe+1, &read_set, NULL, NULL, NULL)>0) {
        if(FD_ISSET(fdpipe,&read_set)){

            read(fdpipe, &estatisticas, sizeof(esta));
            imprimeStats(estatisticas);
        }
    }

}
}

void imprimeStats(esta* estatisticas){
   char *horaAtual=convertTime(time(NULL));
   printf("\nStatitics:\n");
   printf("Hora de inicio:%s\n",estatisticas->start);
   printf("Total pedidos:%d\n",estatisticas->pedidos);
   printf("Pedidos recusados:%d\n",estatisticas->rej);
   printf("Dominios locais:%d\n",estatisticas->loc);
   printf("Dominios externos:%d\n",estatisticas->ext);
   printf("Hora atual:%s\n\n",horaAtual);
}

   char *convertTime(time_t time){
   char *res;
   int h, min, sec;
   res=(char*)malloc(9*sizeof(char));
   res[0]='\0';

   h=(time/3600);
   min=(time-3600*h)/60;
   sec = time%60;
   h=h%24;
   sprintf(res,"%d:%d:%d", h, min, sec);
   return res;
}

我想我什么都不会忘记。

最佳答案

你可能误用了读写: 你写了 read(fdpipe, &estatisticas, sizeof(esta)); 但声明了 esta* estatisticas; 因此你实际上发送了你分配的地址加上一些垃圾(undefined behaiour)

你可能打算写:read(fdpipe, estatisticas, sizeof(*estatisticas))

顺便说一句,你应该检查你读取的字节数,因为读取并不总是读取你要求的所有字节(可能更少)

小心,你在写入端犯了同样的错误,不要忘记在那里修复它。

关于c - 命名管道 Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33962958/

相关文章:

C fork 和 pipe 多进程

c - 处理/避免 C 中的堆栈溢出

c - 为什么 xQueueSendToBack 调用会阻塞?

iphone - 在 Objective-C 类之间共享 C 函数

mysql - 使用 MySQL C API - 使用准备好的语句检查插入行是否成功

python - 从列表中的元素增加字符串时循环失败

windows - 命名管道名称可以有反斜杠吗?

c - 读取中间值时双向 fifo 通信阻塞

ruby - 在 Ruby 中创建命名管道

c - 我该如何打印这个?