免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1997 | 回复: 5
打印 上一主题 下一主题

[C] 请教c达人 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-17 02:26 |只看该作者 |倒序浏览
请问:在linux下面读取"/proc/net/dev", 对bytes的取值应该采用什么数据类型保存?unsigned int 还是unsigned long long?

论坛徽章:
0
2 [报告]
发表于 2008-06-17 11:21 |只看该作者
char

论坛徽章:
0
3 [报告]
发表于 2008-06-17 13:28 |只看该作者
char?能说得更清楚点吗?我需要对bytes的值进行计算统计,如果保存的类型超出了范围就出问题了。

论坛徽章:
0
4 [报告]
发表于 2008-06-17 13:44 |只看该作者
我现在是这样取值的:
struct if_stat
{
char if_name[8];
// receive
unsigned long long if_rbytes;
unsigned long if_rpackets;
unsigned long if_rerrs;
unsigned long if_rdrop;
unsigned long if_rfifo;
unsigned long if_rframe;
unsigned long if_rcompressed;
unsigned long if_rmulticast;
// tramsmit
unsigned long long if_tbytes;
unsigned long if_tpackets;
unsigned long if_terrs;
unsigned long if_tdrop;
unsigned long if_tfifo;
unsigned long if_tcolls;
unsigned long if_tcarrier;
unsigned long if_tcompressed;
};

struct if_stat if_eth0_stat;
sscanf(line, "%6[^:]: %I64u %u %u %u %u %u %u %u %I64u %u %u %u %u %u %u %u",
       &if_eth0_stat.if_name, &if_eth0_stat.if_rbytes, &if_eth0_stat.if_rpackets,
       &if_eth0_stat.if_rerrs, &if_eth0_stat.if_rdrop, &if_eth0_stat.if_rfifo,
       &if_eth0_stat.if_rframe, &if_eth0_stat.if_rcompressed, &if_eth0_stat.if_rmulticast,
       &if_eth0_stat.if_tbytes, &if_eth0_stat.if_tpackets, &if_eth0_stat.if_terrs,
       &if_eth0_stat.if_tdrop, &if_eth0_stat.if_tfifo, &if_eth0_stat.if_tcolls,
       &if_eth0_stat.if_tcarrier, &if_eth0_stat.if_tcompressed)

可是当
printf("bytes:[%I64u] packets:[%u] %u %u %u %u %u %u %u %u %u %u %u %u %u %u\n",
if_eth0_stat.if_rbytes, if_eth0_stat.if_rpackets, if_eth0_stat.if_rerrs,
if_eth0_stat.if_rdrop, if_eth0_stat.if_rfifo, if_eth0_stat.if_rframe, if_eth0_stat.if_rcompressed,
if_eth0_stat.if_rmulticast, if_eth0_stat.if_tbytes, if_eth0_stat.if_tpackets, if_eth0_stat.if_terrs,
if_eth0_stat.if_tdrop, if_eth0_stat.if_tfifo, if_eth0_stat.if_tcolls, if_eth0_stat.if_tcarrier,
if_eth0_stat.if_tcompressed);进行打印的时候,发现if_eth0_stat.if_rbytes可以打出正确值,而if_eth0_stat.if_rpackets却始终为0,其正确的值后移变成了 if_eth0_stat.if_rerrs的取值了。不知到问题出在哪里?

论坛徽章:
0
5 [报告]
发表于 2008-06-17 13:54 |只看该作者

回复 #4 yxluocn 的帖子

seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
                                "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
                           dev->name, stats->rx_bytes, stats->rx_packets,
                           stats->rx_errors,
                           stats->rx_dropped + stats->rx_missed_errors,
                           stats->rx_fifo_errors,
                           stats->rx_length_errors + stats->rx_over_errors +
                             stats->rx_crc_errors + stats->rx_frame_errors,
                           stats->rx_compressed, stats->multicast,
                           stats->tx_bytes, stats->tx_packets,
                           stats->tx_errors, stats->tx_dropped,
                           stats->tx_fifo_errors, stats->collisions,
                           stats->tx_carrier_errors +
                             stats->tx_aborted_errors +
                             stats->tx_window_errors +
                             stats->tx_heartbeat_errors,
                           stats->tx_compressed)

这是内核打印的,你可以参考

论坛徽章:
0
6 [报告]
发表于 2008-06-17 15:08 |只看该作者
请问stats的结构是什么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP