关于字节对齐的问题

不闻窗外事 2020-03-25 03:15:06
/*
* Structures for I/O stats.
* These are now dynamically allocated.
*/
struct io_stats {
/* # of sectors read */
unsigned long rd_sectors __attribute__ ((aligned (8)));
/* # of sectors written */
unsigned long wr_sectors __attribute__ ((packed));
/* # of sectors discarded */
unsigned long dc_sectors __attribute__ ((packed));
/* # of read operations issued to the device */
unsigned long rd_ios __attribute__ ((packed));
/* # of read requests merged */
unsigned long rd_merges __attribute__ ((packed));
/* # of write operations issued to the device */
unsigned long wr_ios __attribute__ ((packed));
/* # of write requests merged */
unsigned long wr_merges __attribute__ ((packed));
/* # of discard operations issued to the device */
unsigned long dc_ios __attribute__ ((packed));
/* # of discard requests merged */
unsigned long dc_merges __attribute__ ((packed));
/* # of flush requests issued to the device */
unsigned long fl_ios __attribute__ ((packed));
/* Time of read requests in queue */
unsigned int rd_ticks __attribute__ ((packed));
/* Time of write requests in queue */
unsigned int wr_ticks __attribute__ ((packed));
/* Time of discard requests in queue */
unsigned int dc_ticks __attribute__ ((packed));
/* Time of flush requests in queue */
unsigned int fl_ticks __attribute__ ((packed));
/* # of I/Os in progress */
unsigned int ios_pgr __attribute__ ((packed));
/* # of ticks total (for this device) for I/O */
unsigned int tot_ticks __attribute__ ((packed));
/* # of ticks requests spent in queue */
unsigned int rq_ticks __attribute__ ((packed));
};

看代码的时候看到这种结构体,我不太明白这能起到什么作用,为什么要在这里设置字节对齐,以及在这个结构体中这个设置起到的作用是什么?? 有什么特殊的意义吗。。。 平时写程序使用结构体基本上没有用过这种字节对齐,不太理解
...全文
341 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
moquan1100 2020-05-21
  • 打赏
  • 举报
回复
你这个是IO结构体,request在文件系统中,在page cache中都是4KB, 8个sectors。
帅得不敢出门 2020-03-25
  • 打赏
  • 举报
回复
多平台数据交互, 比如windows上一个程序用这个结构与另一linux上的程序交互此结构,如果不指定,双方结构位置有可能不是一一对应。
注视着 2020-03-25
  • 打赏
  • 举报
回复
应该是为了方便移植的吧。

15,447

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧