[英]Named Pipe Linux
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--------------------------------------------------------------
Writer: 作家:
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));
}
}
}
reader: //PROCESS 2------------------------------------------------------------ 读者://过程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;
}
I think that I don't forget anything. 我认为我不会忘记任何事情。
you probably misuse the read an write: you write read(fdpipe, &estatisticas, sizeof(esta));
您可能会误用读写:您写了
read(fdpipe, &estatisticas, sizeof(esta));
but declared esta* estatisticas;
但声明为
esta* estatisticas;
thus you actually send the address you allocate plus some garbage (undefined behaiour) 因此,您实际上发送了分配的地址以及一些垃圾(未定义的行为)
you probably intended to write: read(fdpipe, estatisticas, sizeof(*estatisticas))
您可能打算写:
read(fdpipe, estatisticas, sizeof(*estatisticas))
BTW you should check the amount of byte that you read as read done not always read all the byte you asked for (may be less) 顺便说一句,您应该检查读取为已读完成的字节数量,而不总是读取您要求的所有字节(可能更少)
Careful, you did the same error on the write side, don't forget to fix it there too. 小心一点,您在写入方面也犯了同样的错误,请不要忘记也将其修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.