??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美高清视频一区,亚洲欧美一区二区在线观看,欧美高清视频手机在在线http://www.aygfsteel.com/lukewange-hit1983/category/35923.htmlpresentzh-cnThu, 13 Nov 2008 23:48:02 GMTThu, 13 Nov 2008 23:48:02 GMT60linux讑֤模型http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240185.htmlLukeWLukeWWed, 12 Nov 2008 15:14:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240185.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/240185.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240185.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240185.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240185.html Q. Sysfs文gpȝ
Sysfs文gpȝ是一个类gproc文gpȝ的特D文件系l,用于系l中的设备组l成层次l构Qƈ向用h式程序提供详l的内核数据l构信息。其层目录主要有:
Block目录Q包含所有的块设?br /> Devices目录Q包含系l所有的讑֤QƈҎ讑֤挂接的ȝcdl织成层ơ结?br /> Bus目录Q包含系l中所有的ȝcd
Drivers目录Q包括内怸所有已注册的设备驱动程?br /> Class目录Q系l中的设备类型(如网卡设备,声卡讑֤{)
Q. 内核对象机制关键数据l构
2.1 kobject内核对象
Kobject 是Linux 2.6引入的新的设备管理机Ӟ在内怸由struct kobject表示。通过q个数据l构使所有设备在底层都具有统一的接口,kobject提供基本的对象管理,是构成Linux 2.6讑֤模型的核心结构,它与sysfs文gpȝ紧密兌Q每个在内核中注册的kobject对象都对应于sysfs文gpȝ中的一个目录?br /> Kobjectl构定义为:
struct kobject {
char * k_name;    // 指向讑֤名称的指?/span>
char name[KOBJ_NAME_LEN];   // 讑֤名称
struct kref kref;    // 对象引用计数
struct list_head entry;   // 挂接到所在kset中去的单?/span>
struct kobject * parent; // 指向父对象的指针
struct kset * kset;    // 所属kset的指?/span>
struct kobj_type * ktype;   // 指向其对象类型描q符的指?/span>
struct dentry * dentry; // sysfs文gpȝ中与该对象对应的文g节点路径指针
};

其中的kref域表C对象引用的计敎ͼ内核通过kref实现对象引用计数理Q内核提供两个函数kobject_get()、kobject_put()分别用于增加和减引用计敎ͼ当引用计Cؓ0Ӟ所有该对象使用的资源将被释放?br /> Ktype 域是一个指向kobj_typel构的指针,表示该对象的cd。Kobj_type数据l构包含三个域:一个releaseҎ用于释放kobject? 用的资源Q一个sysfs_ops指针指向sysfs操作表和一个sysfs文gpȝ~省属性列表。Sysfs操作表包括两个函数store()? show()。当用户态读取属性时Qshow()函数被调用,该函数编码指定属性值存入buffer中返回给用户态;而store()函数用于存储用户? 传入的属性倹{?br /> 2.2 kset内核对象集合
Kobject通常通过ksetl织成层ơ化的结构,kset是具有相同类型的kobject的集合,在内怸用kset数据l构表示Q定义ؓQ?br />
struct kset {
struct subsystem * subsys;   // 所在的subsystem的指?/span>
struct kobj_type * ktype;   // 指向该kset对象cd描述W的指针
struct list_head list;      // 用于q接该kset中所有kobject的链表头
struct kobject kobj;    // 嵌入的kobject
struct kset_hotplug_ops * hotplug_ops; // 指向热插拔操作表的指?/span>
};

?含在kset中的所有kobject被组l成一个双向@环链表,list域正是该链表的头。Ktype域指向一个kobj_typel构Q被? kset中的所有kobject׃nQ表C些对象的cd。Kset数据l构q内嵌了一个kobject对象Q由kobj域表C)Q所有属于这个kset 的kobject对象的parent域均指向q个内嵌的对象。此外,ksetq依赖于kobjl护引用计数Qkset的引用计数实际上是内嵌? kobject对象的引用计数?br /> 2.3 subsystem内核对象子系l?br /> Subsystem是一pdkset的集合,描述pȝ中某一 c设备子pȝQ如block_subsys表示所有的块设备,对应于sysfs文gpȝ中的block目录。类似的Qdevices_subsys对应? sysfs中的devices目录Q描q系l中所有的讑֤。Subsystem由struct subsystem数据l构描述Q定义ؓQ?br />
struct subsystem {
struct kset kset;       // 内嵌的kset对象
struct rw_semaphore rwsem; // 互斥讉K信号?/span>
};

?个kset必须属于某个subsystemQ通过讄ksetl构中的subsys域指向指定的subsystem可以一个kset加入到该 subsystem。所有挂接到同一subsystem的kset׃n同一个rwsem信号量,用于同步讉Kkset中的链表?br />
Q. 内核对象机制主要相关函数
针对内核对象不同层次的数据结构,linux 2.6内核定义了一pd操作函数Q定义于lib/kobject.c文g中?br /> 3.1 kobject相关函数
void kobject_init(struct kobject * kobj)Q?/span>// kobject初始化函数。设|kobject引用计数?Qentry域指向自w,其所属kset引用计数加1

int kobject_set_name(struct kobject *kobj, const char *format, )Q?/span>// 讄指定kobject的名U?/span>

void kobject_cleanup(struct kobject * kobj);
void kobject_release(struct kref *kref)Q?/span>// kobject清除函数。当其引用计CؓQ时Q释攑֯象占用的资源?/span>

struct kobject *kobject_get(struct kobject *kobj)Q?/span>// kobj 对象的引用计数加1Q同时返回该对象的指针?/span>

void kobject_put(struct kobject * kobj)Q?/span>// kobj对象的引用计数减1Q如果引用计数降?Q则调用kobject_release()释放该kobject对象?/span>

int kobject_add(struct kobject * kobj)Q?/span>// kobj对象加入Linux讑֤层次。挂接该kobject对象到kset的list链中Q增加父目录各kobject的引用计敎ͼ在其parent指向的目录下创徏文g节点Qƈ启动该类型内核对象的hotplug函数?/span>

int kobject_register(struct kobject * kobj)Q?/span>// kobject注册函数。通过调用kobject_init()初始化kobjQ再调用kobject_add()完成该内核对象的注册?/span>

void kobject_del(struct kobject * kobj)Q?/span>// 从Linux讑֤层次(hierarchy)中删除kobj对象?/span>

void kobject_unregister(struct kobject * kobj)Q?/span>// kobject注销函数。与kobject_register()相反Q它首先调用kobject_del从设备层ơ中删除该对象,再调用kobject_put()减少该对象的引用计数Q如果引用计数降?Q则释放该kobject对象?/span>

3.2 kset相关函数
与kobject 怼Qkset_init()完成指定kset的初始化Qkset_get()和kset_put()分别增加和减kset对象的引用计数? Kset_add()和kset_del()函数分别实现指定keset对象加入讑֤层次和从其中删除Qkset_register()函数完成 kset的注册而kset_unregister()函数则完成kset的注销?br /> 3.3 subsystem相关函数
subsystem有一l完成类似的函数Q分别是Q?br />
void subsystem_init(struct subsystem *subsys);
int subsystem_register(struct subsystem *subsys);
void subsystem_unregister(struct subsystem *subsys);
struct subsystem *subsys_get(struct subsystem *subsys)
void subsys_put(struct subsystem *subsys);

Q. 讑֤模型lg
在上q内核对象机制的基础上,Linux的设备模型徏立在几个关键lg的基上,下面我们详细阐述q些lg?br /> 4.1 devices
pȝ中的M讑֤在设备模型中都由一个device对象描述Q其对应的数据结构struct device定义为:
struct device {
struct list_head g_list;
struct list_head node;
    
struct list_head bus_list;
    
struct list_head driver_list;
    
struct list_head children;
    
struct device *parent;
    
struct kobject kobj;
    
char bus_id[BUS_ID_SIZE];
    
struct bus_type *bus;
    
struct device_driver *driver;
    
void *driver_data;
    
/* Several fields omitted */
};

g_list 该device对象挂接到全局讑֤链表中,所有的device对象都包含在devices_subsys中,q组l成层次l构。Node域将该对象挂? 到其兄弟对象的链表中Q而bus_list则用于将q接到相同ȝ上的讑֤l织成链表,driver_list则将同一驱动E序理的所有设备组lؓ? 表。此外,children域指向该device对象子对象链表头Qparent域则指向父对象。Device对象q内嵌一个kobject对象Q用于引 用计数管理ƈ通过它实现设备层ơ结构。Driver域指向管理该讑֤的驱动程序对象,而driver_data则是提供l驱动程序的数据。Bus域描q设 备所q接的ȝcd?br /> 内核提供了相应的函数用于操作device对象。其中Device_register()函数一个新的device对象? 入设备模型,q自动在/sys/devices下创Z个对应的目录。Device_unregister()完成相反的操作,注销讑֤对象? Get_device()和put_device()分别增加与减设备对象的引用计数。通常devicel构不单独用,而是包含在更大的l构中作Z 个子l构使用Q比如描qPCI讑֤的struct pci_devQ其中的dev域就是一个device对象?br /> 4.2 drivers
pȝ中的每个驱动E序׃个device_driver对象描述Q对应的数据l构定义为:
struct device_driver {
    
char *name;   // 讑֤驱动E序的名U?/span>
    struct bus_type *bus; // 该驱动所理的设备挂接的ȝcd
    struct kobject kobj;    // 内嵌kobject对象
    struct list_head devices;  // 该驱动所理的设备链表头
    int (*probe)(struct device *dev); // 指向讑֤探测函数Q用于探设备是否可以被该驱动程序管?/span>
int (*remove)(struct device *dev); // 用于删除讑֤的函?/span>
/*
 some fields omitted*/
}Q?/span>

与device l构cMQdevice_driver对象依靠内嵌的kobject对象实现引用计数理和层ơ结构组l。内核提供类似的函数用于操作 device_driver对象Q如get_driver()增加引用计数Qdriver_register()用于向设备模型插入新的driver? 象,同时在sysfs文gpȝ中创建对应的目录。Device_driver()l构q包括几个函敎ͼ用于处理热拔插、即插即用和甉|理事g?br /> 4.3   buses
pȝ中ȝ由struct bus_type描述Q定义ؓQ?br />
struct bus_type {
char   * name; // ȝcd的名U?/span>
struct subsystem subsys; // 与该ȝ相关的subsystem
struct kset drivers; // 所有与该ȝ相关的驱动程序集?/span>
struct kset devices; // 所有挂接在该ȝ上的讑֤集合
struct bus_attribute * bus_attrs; // ȝ属?/span>
struct device_attribute * dev_attrs; // 讑֤属?/span>
struct driver_attribute * drv_attrs;   // 驱动E序属?/span>
int (*match)(struct device * dev, struct device_driver * drv);
int (*hotplug) (struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
int (*suspend)(struct device * dev, u32 state);
int (*resume)(struct device * dev);
}Q?/span>

?个bus_type对象都内嵌一个subsystem对象Qbus_subsys对象理pȝ中所有ȝcd的subsystem对象。每? bus_type对象都对?sys/bus目录下的一个子目录Q如PCIȝcd对应?sys/bus/pci。在每个q样的目录下都存在两个子? 录:devices和driversQ分别对应于bus_typel构中的devices和drivers域)。其中devices子目录描q连接在该? U上的所有设备,而drivers目录则描qC该ȝ兌的所有驱动程序。与device_driver对象cMQbus_typel构q包含几个函? Qmatch()、hotplug(){)处理相应的热插拔、即插即拔和甉|理事g?br /> 4.4 classes
pȝ中的讑֤cȝ struct class描述Q表C某一c设备。所有的class对象都属于class_subsys子系l,对应于sysfs文gpȝ中的/sys/class目录? 每个class对象包括一个class_device链表Q每个class_device对象表示一个逻辑讑֤Qƈ通过struct class_device中的dev域(一个指向struct device的指针)兌一个物理设备。这P一个逻辑讑֤L对应于一个物理设备,但是一个物理设备却可能对应于多个逻辑讑֤。此外,classl构? q包括用于处理热插拔、即插即拔和甉|理事g的函敎ͼq与device对象和driver对象怼?br />

LukeW 2008-11-12 23:14 发表评论
]]>
位运?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Wed, 12 Nov 2008 05:53:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/240082.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240082.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240082.html</trackback:ping><description><![CDATA[<strong>C中的位运?/strong><br /> 能够q用CQ何整形的数据cd?包括char, int), 无论有没有short, long, unsignedq样的限定词.<br /> <br /> <br /> 位运的应用<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 交换指针变量x,y所指向的存储位|处存放的?br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 优势是不需要第三个位置来时存储另一个?br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 但是q个Ҏq没有明昄性能优势,只是一个智力上的消?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> inplace_swap(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y)<br /> {<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br /> }<br /> </span></div> <br /> <strong>位运常见用?</strong><br /> 实现掩码q算<br /> <br /> <br /> <br /> -----------------------------------<br /> <strong>Java中的位运?/strong><br /> <br /> <br /> <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/240082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-11-12 13:53 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大端端 -- 各系l及机器的信息表C?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Wed, 12 Nov 2008 03:58:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/240053.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240053.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240053.html</trackback:ping><description><![CDATA[因ؓ现行的计机都是以八位一个字节ؓ存储单位,那么一?6位的整数,也就是C语言中的short,在内存中可能有两U存储顺序big- <br /> <br /> endian和litte-endian.考虑一个short整数0x3132(0x32是低?0x31是高?,把它赋值给一个short变量,那么它在内存中的存储?br /> <br /> 能有如下两种情况:<br /> 大端字节(Big-endian):<br /> <br /> short变量地址<br />        0x1000                  0x1001<br /> ___________________________________<br /> |                 |<br /> |         0x31    |       0x32<br /> |________________ | ________________<br /> 高位字节在低位字节的前面,也就是高位在内存地址低的一?可以q样C(大端->高位->在前->正常的逻辑序)<br />  <br /> 端字节(little-endian):<br /> <br /> short变量地址<br />        0x1000                  0x1001<br /> _____________________________________<br /> |                 |<br /> |         0x32    |       0x31<br /> |________________ | __________________<br /> 低位字节在高位字节的前面,也就是低位在内存地址低的一?可以q样C(端->低位->在前->与正帔R辑序相反)<br />  <br /> 可以做个实验<br /> 在windows上下如下E序<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> #include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">assert.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main( </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> )<br /> {<br />         </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test;<br />         FILE</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> fp;<br />         <br />         test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x3132</span><span style="color: rgb(0, 0, 0);">;  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">(31ASIIC码的’1’,32ASIIC码的’2’)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((fp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c:</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">test.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">wb</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br />               assert(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />         fwrite(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, fp);<br />         fclose(fp);<br /> }</span></div> <br />     然后在C盘下打开test.txt文g,可以看见内容?1,而test{于0x3132,可以明显的看出来x86的字节顺序是低位在前.如果我们<br /> 把这D同L代码攑ֈ(big-endian)的机器上执行,那么打出来的文g是12.q在本机中用是没有问题?但当你把q个文g从一<br /> 个big- endian机器复制C个little-endian机器上时出现问题了.<br /> <br />     如上qC?我们在big-endian的机器上创徏了这个test文g,把其复制到little-endian的机器上再用freadd一?short?br /> ?我们得到的就不再?x3132而是0x3231?q样d的数据就是错误的,所以在两个字节序不一L机器上传输数据时需要特?br /> 心字节序,理解了字节顺序在可以帮助我们写出UL行更高的代码.<br /> <br /> 正因为有字节序的差?所以在|络传输的时候定义了所有字节顺序相关的数据都用big-endian,BSD的代码中定义了四个宏来处<br /> ?<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> ntohs(n)     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">|络字节序C机字节顺?nbsp;n代表net, h代表host, s代表short</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htons(n)     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">L字节序到网l字节顺?nbsp;n代表net, h代表host, s代表short</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> ntohl(n)      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">|络字节序C机字节顺?nbsp;n代表net, h代表host, s代表 long</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htonl(n)      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">L字节序到网l字节顺?nbsp;n代表net, h代表host, s代表 long</span></div> <br /> 举例说明下这其中一个宏的实?<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> sw16(x) "</span><span style="color: rgb(0, 0, 0);"><br />     ((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)( </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0x00ffU</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0xff00U</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) ))</span></div> <br /> q里实现的是一个交换两个字节顺?其他几个宏类?<br /> <br /> 我们改写一下上面的E序<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> #include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">assert.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> sw16(x) "</span><span style="color: rgb(0, 0, 0);"><br />     ((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)( </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0x00ffU</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0xff00U</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) ))<br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 因ؓx86下面是低位在?需要交换一下变成网l字节顺?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htons(x) sw16(x)</span><span style="color: rgb(0, 0, 0);"><br />  <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main( </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> )<br /> {<br />         </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test;<br />         FILE</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> fp;<br />         <br />         test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> htons(</span><span style="color: rgb(0, 0, 0);">0x3132</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">(31ASIIC码的’1’,32ASIIC码的’2’)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((fp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c:</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">test.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">wb</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br />               assert(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />         fwrite(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, fp);<br />         fclose(fp);<br /> }</span></div> <br />  <br />     如果在高字节在前的机器上,׃与网l字节顺序一?所以我们什么都不干可以了,只需要把#define htons(x) sw16(x)宏替<br /> <br /> 换ؓ #define htons(x) (x).<br />     一开始我在理解这个问题时,d想ؓ什么其他数据不用交换字节顺?比如说我们write一块buffer到文?最后终于想明白?<br /> <br /> 因ؓ都是unsigned charcd一个字节一个字节的写进?q个序是固定的,不存在字节顺序的问题.<br /> <p><strong><font color="#0000ff">【用函数判断pȝ是Big Endianq是Little Endian?/font></strong><br /> </p> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> IsBig_Endian()<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">如果字节序ؓbig-endianQ返回true;<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">反之?nbsp;  little-endianQ返回false</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">{<br />     unsigned </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x1122</span><span style="color: rgb(0, 0, 0);">;<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">( (unsigned </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test ) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x11</span><span style="color: rgb(0, 0, 0);">)<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> TRUE;<br /> </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> FALSE;<br /> <br /> }</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">IsBig_Endian()<br /> </span></div> <br /> <strong><font color="#0000ff">【打印程序对象的字节表示?/font></strong><font color="#0000ff"><br /> </font> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 可在不同q_与硬件架构的机器中测试运行这D代?理解大端表示和小端表C的不同.<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> q段代码使用强制cd转换规避cdpȝ</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">#incluede </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 假设每个字节都是非负整数</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">typedef unsigned </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">byte_pointer;<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_bytes(byte_pointer start, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br /> {<br />  </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; i </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);"> len; i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> %.2x</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, start[i]);<br />  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_int(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_float(</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 在用相同编?如ASCII~码)的系l中,字符串字节表C得到的l果一般是相同?所以文本数据比二进制数据具有更强的q_无关?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_string(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x)<br /> {<br />  show_bytes((byte_pointer) x, strlen(x));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_pointer(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> test_show_bytes(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> val)<br /> {<br />  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> ival </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> val;<br />  </span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> fval </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);">)ival;<br />  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">pval </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">ival;<br />  <br />  show_int(ival); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 各个机器因ؓ大端表示和小端表C的不同,从而只是字节顺序不?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"> show_float(fval); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 各个机器因ؓ大端表示和小端表C的不同,从而只是字节顺序不?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"> show_pointer(pval); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 指针值是与机器相关的(linux,sun使用4字节地址, 而alpha使用八字节地址)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">}</span></div> <br /> ---------------------------------------------<br /> 对于如数?2345在int型和float型时的编码表C?br /> <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/240053.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-11-12 11:58 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ຣʡ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ϸ</a>| <a href="http://" target="_blank">³</a>| <a href="http://" target="_blank">μԴ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">»</a>| <a href="http://" target="_blank">¡Ң</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ͷ</a>| <a href="http://" target="_blank">ݸ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ͨ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">â</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ī</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ǿ</a>| <a href="http://" target="_blank">ʡ</a>| <a href="http://" target="_blank">Ǩ</a>| <a href="http://" target="_blank">۽</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">ɼ</a>| <a href="http://" target="_blank"></a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>