- 论坛徽章:
- 0
|
写了一个mkfifo程序,总是出现莫明其妙的问题,现在贴出来,请大家帮指点一下。
这是write 函数
int main(int argc, char **argv)
{
int r_value = 0;
ix_uint32 i_rbyte = 0;
ix_uint8 i_type = 0;
ix_uint8 r_log_buf[2048] = {0};
ix_uint8 r_dbg_buf[2048] = {0};
ix_uint8 fd = 0;
ix_uint8 i_count = 0;
ix_uint32 read_wbyte = 0;
ix_uint8 i_stat = 0;
if (argc < 4)
{
fprintf(stderr, "the argc is error \n" ;
exit(0);
}
/*r_value = daemon_init();
if (r_value != 0)
{
exit(0);
}
*/
i_type = atoi(*(argv + 1));
i_count = atoi(*(argv + 2));
mkfifo_create(MKFIFO_LOG_PATH);
mkfifo_create(MKFIFO_DBG_PATH);
if (i_type == 1)
{
fd = fifo_open(MKFIFO_LOG_PATH);
}
else
{
fd = fifo_open(MKFIFO_DBG_PATH);
}
while(i_count--)
{
/* i_rbyte = ntb_app_read(&i_type, r_buf);
if (i_rbyte < 0)
{
fprintf(stderr, "daemon fun i_rbyte is error\n" ;
continue;
}
if (i_rbyte != 1024)
{
fprintf(stderr, "recv byte not 1024\n" ;
}
*/
if (i_type == PACKET_TYPE_LOG)
{
/* if(i_stat == 0)
mkfifo_create(MKFIFO_LOG_PATH);
fd = fifo_open(MKFIFO_LOG_PATH);
*/
memcpy(r_log_buf, *(argv + 3), strlen(*(argv + 3)));
//read_wbyte = write(i_log_fd, r_buf, 204 ;
read_wbyte = write(fd, r_log_buf, 204 ;
if (read_wbyte == -1)
{
fprintf(stderr, "write log is error\n" ;
return -1;
}
}
if (i_type == PACKET_TYPE_DEBUG)
{
/* if(i_stat == 0)
mkfifo_create(MKFIFO_DBG_PATH);
fd = fifo_open(MKFIFO_DBG_PATH);
*/
memcpy(r_dbg_buf, *(argv + 3), strlen(*(argv + 3)));
//read_wbyte = write(i_dbg_fd, r_buf, 204 ;
read_wbyte = write(fd, r_dbg_buf, 204 ;
if (read_wbyte == -1)
{
fprintf(stderr, "write debug is error\n" ;
return -1;
}
}
// i_stat = 1;
fifo_close(fd);
}
//fifo_close(fd);
return 0;
}
××××××××××××××××××××分隔线××××××××××××××××××××××××
这是read 函数
int main(int argc, char** argv)
{
char r_buf[2050] = {0};
int fd = 0;
int r_size = 0;
int r_type = 0;
int ret_size = 0;
if (argc < 3)
{
perror("argc is error\n" ;
}
r_type=atoi(argv[1]);
r_size=atoi(argv[2]);
//fd = open(FIFO_SERVER, O_RDONLY, 0);
if (r_type == 1)
{
fd = open(FIFO_LOG_SERVER, O_RDONLY, 0);
}
else
{
fd = open(FIFO_DBG_SERVER, O_RDONLY, 0);
}
if (fd == -1)
{
printf("open is error\n" ;
return -1;
}
printf("the fd is %d\n", fd);
r_buf[2050 - 1] = '\0';
while(1)
{
if (r_type == 1)
{
fd = open(FIFO_LOG_SERVER, O_RDONLY, 0);
}
else
{
fd = open(FIFO_DBG_SERVER, O_RDONLY, 0);
}
ret_size = read(fd, r_buf, r_size);
if (ret_size == -1)
{
printf("no data \n" ;
}
printf("the ret_size is %s\n", r_buf);
close(fd);
}
return 0;
}
由于考虑到循环中反复open文件,可能会降低效率,因此希望一直是open状态。但是总是出现奇怪的现象,第一次可以正常,但是后边总是错误。请大家帮看一下。 |
|