﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-syhd142</title><link>http://www.cppblog.com/syhd142/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 07 Apr 2026 11:51:11 GMT</lastBuildDate><pubDate>Tue, 07 Apr 2026 11:51:11 GMT</pubDate><ttl>60</ttl><item><title>c语言中有关void，sizeof，结构体的一些问题</title><link>http://www.cppblog.com/syhd142/archive/2011/08/24/154254.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Wed, 24 Aug 2011 14:48:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2011/08/24/154254.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/154254.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2011/08/24/154254.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/154254.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/154254.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt; "><strong>void[1]：</strong></span><span style="font-size: 8pt; "><br />void是C语言中的空类型，void的用途有二。<br />1、对函数返回的限定；<br /><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果函数没有返回值，则默认返回整数类型，而不是void类型。c++有很严格的类型，不允许函数不加类型声明，而编译器则不这么认为检查这一点在VC6.0中可以验证。所以在编写代码的时候，每个函数都应该加上返回类型。</div>2、对函数参数的限定；<br />&nbsp; &nbsp; &nbsp; 在c++中，函数参数为void意味着不接受参数，但是在c语言中可以给无参数的函数传递任意类型的参数，这点在turbo c中可以验证。<br /><br /><div>指针的大小和机器的位数有关，在32位机器上任何类型指针的大小都是4字节，在64位机器上为8。<strong>所以指针大小和类型无关</strong>。</div>void*就是空类型指针，所谓空类型指针就是通用指针类型。它有以下特点。</span><span class="Apple-style-span" style="font-size: 12px; ">1、按照ASC码标准void指针不能做算术操作，因为不确定其指向数据类型大小；</span><span class="Apple-style-span" style="font-size: 12px; "><br /></span><span class="Apple-style-span" style="font-size: 12px; ">2、c++允许将任何类型的指针赋给void*，但是不允许void指针赋值给其它类型。必须显示的强制转换。<br /><br />其它类型指针相互之间是否也可以强制装换？<br />&nbsp; &nbsp;强制转换后编译能通过，大部分情况下运行也是没有问题的，但是根据参考文献[4]的说法是容易出问题的，因为某些CPU对某些数据类型有对其限制，这样在做指针强制转换的时候就容易出现问题。<br />&nbsp; &nbsp;其实，其它类型指针之间的转换完全可以通过使用void*类型来避免。<br /><strong><br />sizeof[2]:</strong><br />前面说在同一台机器上指针大小是固定的，通过sizeof测试int*,char*,bool*,double*都出结果都是4(32位机器测试)。<br />今天在程序中memset一个T*类型的指针，发现没有初始化成功，原来在memset的第三个参数中填的是sizeof(T*类型的指针)。特此总结了一下sizeof的一些问题。<br />1、char* s="0123456789"；<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizeof(s)=4，s是一个指针。<br />&nbsp; &nbsp; &nbsp; sizeof(*s)=1，*s是第一个元素。<br />&nbsp; &nbsp; &nbsp; strlen(s)=10，s是字符串首地址。<br />2、char s[]="0123456789"；<br />&nbsp; &nbsp; &nbsp; sizeof(s)=11，s是字符串数组，包括'\0'。<br />&nbsp; &nbsp; &nbsp; sizeof(*s)=1，*s是第一个元素。<br />&nbsp; &nbsp; &nbsp; strlen(s)=10，s是字符串首地址。<br />3、char s[20]="0123456789"；<br />&nbsp; &nbsp; &nbsp; sizeof(s)=20，s是在内存中静态分配的大小。<br />&nbsp; &nbsp; &nbsp; sizeof(*s)=1，*s是第一个元素。<br />&nbsp; &nbsp; &nbsp; strlen(s)=10，s是字符串首地址。<br /><strong><br />结构体[3]：</strong><br />结构体在字节对齐的时候有三个原则，有了这三个原则就很容易的可以计算出任意一个结构体的sizeof大小了。<br />1、结构体变量的首地址能够被其最宽基本类型的成员大小所整除；<br />2、结构体每个成员相对于结构体首地址的偏移量(offset)都是成员大小的整数倍，如有需要编译器会在成员之间加上填充字节(internal adding)；<br />3、结构体的总大小为结构体最宽基本类型成员大小的整数倍，如有需要编译器会在最末一个成员之后加上填充字节(trailing padding)。<br /><hr />参考文献：<br /></span><span class="Apple-style-span" style="font-size: 10pt; ">[1]、</span><a href="http://blog.sina.com.cn/s/blog_625cce080100kip3.html"><span style="font-size: 10pt; ">http://blog.sina.com.cn/s/blog_625cce080100kip3.html</span><br /></a><span style="font-size: 10pt; ">[</span><span style="font-size: 10pt; ">2</span><span style="font-size: 10pt; ">]、</span><a href="http://shansun123.iteye.com/blog/398601"><span style="font-size: 10pt; ">http://shansun123.iteye.com/blog/398601</span></a>&nbsp;<br /><span style="font-size: 10pt; ">[3]、</span><a href="http://blog.csdn.net/Linux_Gao/article/details/2612885"><span style="font-size: 10pt; ">http://blog.csdn.net/Linux_Gao/article/details/2612885&nbsp;<br /></span></a><span style="font-size: 10pt; ">[4</span><span style="font-size: 10pt; ">]、</span><a href="http://blog.csdn.net/Linux_Gao/article/details/2612885"><span style="font-size: 10pt; ">http://blog.csdn.net/Linux_Gao/article/details/2612885</span></a><img src ="http://www.cppblog.com/syhd142/aggbug/154254.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2011-08-24 22:48 <a href="http://www.cppblog.com/syhd142/archive/2011/08/24/154254.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>device network eth0 does not seem to be present</title><link>http://www.cppblog.com/syhd142/archive/2011/07/02/149968.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Sat, 02 Jul 2011 03:25:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2011/07/02/149968.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/149968.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2011/07/02/149968.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/149968.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/149968.html</trackback:ping><description><![CDATA[<div>今天虚拟机启动后网卡eth0启动不了，ifconfig -a时有一个eth10，而在/etc/sysconfig/network-scripts里面只有ifcfg-eth0，这样网卡启动时就找不到对应的配置文件，所以将eth10改成eth0就好了。命令如下：<br />ifconfig eth10 down<br />ip link set eth10 name eth0<br />ifconfig eth0 up</div><img src ="http://www.cppblog.com/syhd142/aggbug/149968.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2011-07-02 11:25 <a href="http://www.cppblog.com/syhd142/archive/2011/07/02/149968.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转] warning: deprecated conversion from string constant to "char *"</title><link>http://www.cppblog.com/syhd142/archive/2011/02/13/139993.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Sun, 13 Feb 2011 13:48:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2011/02/13/139993.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/139993.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2011/02/13/139993.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/139993.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/139993.html</trackback:ping><description><![CDATA[<meta http-equiv="content-type" content="text/html; charset=utf-8"><span  style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">今天碰到了这个警告，老老实实的google了一下，以下是收获：</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">问题是这样产生的，先看这个函数原型：</p><blockquote style="border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: rgb(152, 164, 127); border-right-color: rgb(152, 164, 127); border-bottom-color: rgb(152, 164, 127); border-left-color: rgb(152, 164, 127); background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(245, 245, 245); font: normal normal normal 12px/normal simsun; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; background-position: initial initial; background-repeat: initial initial; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">void someFunc(char *someStr);</p></blockquote><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">再看这个函数调用：</p><blockquote style="border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: rgb(152, 164, 127); border-right-color: rgb(152, 164, 127); border-bottom-color: rgb(152, 164, 127); border-left-color: rgb(152, 164, 127); background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(245, 245, 245); font: normal normal normal 12px/normal simsun; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; background-position: initial initial; background-repeat: initial initial; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">someFunc("I'm a string!");</p></blockquote><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">把这两个东西组合起来，用最新的g++编译一下就会得到标题中的警告。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">为什么呢？原来char *背后的含义是：<em style="font-style: italic; ">给我个字符串，我要修改它。</em><br>而理论上，我们传给函数的字面常量是没法被修改的。<br>所以说，比较和理的办法是把参数类型修改为const char *。<br>这个类型说背后的含义是：<em style="font-style: italic; ">给我个字符串，我只要读取它。</em></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">很自然的延伸一下。 如果我既要传字面常量又要传字符串变量怎么办呢？......重载</p></span>
<img src ="http://www.cppblog.com/syhd142/aggbug/139993.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2011-02-13 21:48 <a href="http://www.cppblog.com/syhd142/archive/2011/02/13/139993.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]string,string.h,cstring.h的区别</title><link>http://www.cppblog.com/syhd142/archive/2011/02/13/139957.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Sat, 12 Feb 2011 16:49:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2011/02/13/139957.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/139957.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2011/02/13/139957.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/139957.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/139957.html</trackback:ping><description><![CDATA[<meta http-equiv="content-type" content="text/html; charset=utf-8"><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">原文地址;</span><meta http-equiv="content-type" content="text/html; charset=utf-8"><a href="http://topic.csdn.net/u/20070402/18/7111613b-c5e4-485e-a5d0-33de9aebbaea.html">http://topic.csdn.net/u/20070402/18/7111613b-c5e4-485e-a5d0-33de9aebbaea.html</a><div><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">c++中 string与string.h 的作用和区别</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">#include &lt;string.h&gt; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">void main() &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">{ &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp; string aaa= "abcsd d"; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp; printf("looking for abc from abcdecd %s\n", &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp; (strcmp(aaa,"abc")) ? "Found" : "Not Found"); &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">} &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">不能正确执行，提示说是string类型没有定义 &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">而下面： &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">#include &lt;string&gt; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">using namespace std; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">void main() &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">{ &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp; string aaa= "abcsd d"; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp; printf("looking for abc from abcdecd %s\n", &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp; (strcmp(aaa,"abc")) ? "Found" : "Not Found"); &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">} &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">这里的string编译器就认识了，但是strcmp就不认识了呢？ &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">一般一个C++的老的带&#8220;.h&#8221;扩展名的库文件，比如iostream.h，在新标准后的标准库中都有一个不带&#8220;.h&#8221;扩展名的相对应，区别除了后者的好多改进之外，还有一点就是后者的东东都塞进了&#8220;std&#8221;名字空间中。 &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">但唯独string特别。 &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">问题在于C++要兼容C的标准库，而C的标准库里碰巧也已经有一个名字叫做&#8220;string.h&#8221;的头文件，包含一些常用的C字符串处理函数，比如楼主提到的strcmp。 &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">这个头文件跟C++的string类半点关系也没有，所以&lt;string&gt;并非&lt;string.h&gt;的&#8220;升级版本&#8221;，他们是毫无关系的两个头文件。 &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">要达到楼主的目的，比如同时： &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">#include &lt;string.h&gt; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">#include &lt;string&gt; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">using namespace std; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">或者 &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">#include &lt;cstring&gt; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">#include &lt;string&gt; &nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">&nbsp;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">其中&lt;cstring&gt;是与C标准库的&lt;string.h&gt;相对应，但裹有std名字空间的版本。</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">笑谈（来自高质量++）</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">C++标准库很大。非常大。难以置信的大。怎么个大法？这么说吧：在C++</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">标准中，关于标准库的规格说明占了密密麻麻300 多页，这还不包括标准C 库，</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">后者只是"作为参考"（老实说，原文就是用的这个词）包含在C++库中。</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">当然，并非总是越大越好，但在现在的情况下，确实越大越好，因为大的</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">库会包含大量的功能。标准库中的功能越多，开发自己的应用程序时能借助的</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">功能就越多。C++库并非提供了一切（很明显的是，没有提供并发和图形用户</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">接口的支持），但确实提供了很多。几乎任何事你都可以求助于它。</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">在归纳标准库中有些什么之前，需要介绍一下它是如何组织的。因为标准</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">库中东西如此之多，你（或象你一样的其他什么人）所选择的类名或函数名就</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">很有可能和标准库中的某个名字相同。为了避免这种情况所造成的名字冲突，</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">实际上标准库中的一切都被放在名字空间std 中（参见条款28）。但这带来了</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">一个新问题。无数现有的C++代码都依赖于使用了多年的伪标准库中的功能，</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">例如，声明在&lt;iostream.h&gt;，&lt;complex.h&gt;，&lt;limits.h&gt;等头文件中的功能。现</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">有软件没有针对使用名字空间而进行设计，如果用std 来包装标准库导致现有</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">代码不能用，将是一种可耻行为。（这种釜底抽薪的做法会让现有代码的程序员</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">说出比"可耻" 更难听的话）</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">慑于被激怒的程序员会产生的破坏力，标准委员会决定为包装了std 的那</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">部分标准库构件创建新的头文件名。生成新头文件的方法仅仅是将现有C++头</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">文件名中的.h 去掉，方法本身不重要，正如最后产生的结果不一致也并不重</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">要一样。所以&lt;iostream.h&gt;变成了&lt;iostream&gt;，&lt;complex.h&gt;变成了&lt;complex&gt;，</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">等等。对于C 头文件，采用同样的方法，但在每个名字前还要添加一个c。所</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">以C 的&lt;string.h&gt;变成了&lt;cstring&gt;，&lt;stdio.h&gt;变成了&lt;cstdio&gt;，等等。最后一</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">点是，旧的C++头文件是官方所反对使用的（即，明确列出不再支持），但旧</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">的C 头文件则没有（以保持对C 的兼容性）。实际上，编译器制造商不会停止</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">对客户现有软件提供支持，所以可以预计，旧的C++头文件在未来几年内还是</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">会被支持。</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">所以，实际来说，下面是C++头文件的现状：</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">旧的C++头文件名如&lt;iostream.h&gt;将会继续被支持，尽管它们不在官方标</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">准中。这些头文件的内容不在名字空间std 中。</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">新的C++头文件如&lt;iostream&gt;包含的基本功能和对应的旧头文件相同，但</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">头文件的内容在名字空间std 中。（在标准化的过程中，库中有些部分的细节被</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">修改了，所以旧头文件和新头文件中的实体不一定完全对应。）</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">标准C 头文件如&lt;stdio.h&gt;继续被支持。头文件的内容不在std 中。</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; "><br></span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">具有C 库功能的新C++头文件具有如&lt;cstdio&gt;这样的名字。它们提供的内</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">容和相应的旧C 头文件相同，只是内容在std 中。</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">所有这些初看有点怪，但不难习惯它。最大的挑战是把字符串头文件理清</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">楚：&lt;string.h&gt;是旧的C 头文件，对应的是基于char*的字符串处理函数；&lt;string&gt;</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">是包装了std 的C++头文件，对应的是新的string 类（看下文）；&lt;cstring&gt;是对</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">应于旧C 头文件的std 版本。如果能掌握这些（我相信你能），其余的也就容易</span><span  style="font-family: simsun; font-size: 14px; line-height: 23px; ">了。</span>

</div><img src ="http://www.cppblog.com/syhd142/aggbug/139957.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2011-02-13 00:49 <a href="http://www.cppblog.com/syhd142/archive/2011/02/13/139957.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cache Coherence的问题</title><link>http://www.cppblog.com/syhd142/archive/2011/02/05/139739.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Sat, 05 Feb 2011 07:54:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2011/02/05/139739.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/139739.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2011/02/05/139739.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/139739.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/139739.html</trackback:ping><description><![CDATA[<span style="font-size: 12pt; ">自己写论文的时候，引用了一篇SIGCOM的文献，里面有谈到Cache Coherency（缓存一致性）和False Shareing（伪共享）的问题。那么什么是缓存一致性呢？这个问题困扰了我很久，今天在网上看博文的时候发现很详细的解释了这个问题，这也解决了我心中的困惑。<br></span><br><span style="font-size: 12pt; "><strong>缓存一致性（Cache Coherency）：</strong>我们现有的CPU的Cache是有多层结构的，一般每个CPU会有一个私有的L1和L2级Cache，然后多个核心CPU共享一个L3级缓存。但是这样一来有个问题，就是多个CPU的私有Cache之间需要同步。比如说，CPU1上的线程对全局变量global_counter进行了加1操作，这个被写入的新值存到CPU1的私有缓存里，而CPU2上的线程想要读global_counter的时候，而CPU2的私有Cache里的global_counter的值还是旧值，怎么办呢？这个任务就交给Cache Coherency来完成了。Cache Coherency是一种Cache之间的同步协议，它其实就是保证对某一个地址的读操作返回的的值一定是那个地址的最新值，而这个值可能是该线程所处CPU写的，也肯能是另外一个CPU上的线程写的。<br><br>问题到这里看起来似乎得到了圆满的解决，但是当多个CPU对同一内存地址线上的不同数据进行操作时，Cache Coherency机制会将整个地址线上的数据拷贝到各个CPU的私有Cache中去了，这样每个线程在读取自己数据的时候也把别人的数据读进去了，更新的时候Cache Coherency为了保持数据的一致性，不同CPU的Cache之间要进行同步，这个会导致严重的性能问题，即所谓的False Shareing，在维基百科上给出了详细的解释。解决方案很简单，说是通过把每个数据凑齐Cache Line的长度，实现隔离。<br></span><br><span style="font-size: 12pt; ">参考文献：<br></span>[1]&nbsp;<a title="Wiki:Cache Coherency
" href="http://en.wikipedia.org/wiki/Cache_coherence" style="color: rgb(0, 44, 153); text-decoration: none; ">Wiki:Cache Coherency</a><br>[2]&nbsp;<a title="Wiki:False Shareing" href="http://en.wikipedia.org/wiki/False_sharing" target="_blank" style="color: rgb(0, 44, 153); text-decoration: none; ">Wiki:False Shareing</a><br>[3]&nbsp;<a title="False sharing问题及其解决方法" href="http://blog.yufeng.info/archives/783" style="color: rgb(0, 44, 153); text-decoration: none; ">False sharing问题及其解决方法</a><br>[4]&nbsp;<a title="为什么程序员需要关心顺序一致性（Sequential Consistency）而不是Cache一致性（Cache Coherence？）" href="http://www.parallellabs.com/2010/03/06/why-should-programmer-care-about-sequential-consistency-rather-than-cache-coherence/" style="color: rgb(0, 44, 153); text-decoration: none; ">为什么程序员需要关心顺序一致性（Sequential Consistency）而不是Cache一致性（Cache Coherence？）</a><img src ="http://www.cppblog.com/syhd142/aggbug/139739.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2011-02-05 15:54 <a href="http://www.cppblog.com/syhd142/archive/2011/02/05/139739.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转] 文本文件和二进制文件有什么区别?</title><link>http://www.cppblog.com/syhd142/archive/2010/12/29/137695.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Wed, 29 Dec 2010 13:46:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2010/12/29/137695.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/137695.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2010/12/29/137695.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/137695.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/137695.html</trackback:ping><description><![CDATA[
<meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 20px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><div>源地址：<a href="http://hi.baidu.com/obi_wan/blog/item/465db603180af0723812bb3e.html">http://hi.baidu.com/obi_wan/blog/item/465db603180af0723812bb3e.html</a></div><meta http-equiv="content-type" content="text/html; charset=utf-8">二进制文件，这个再基础不过的名词，正因为它的无处不在，或许没有人会关注它背后隐含的内容。其实我也一样，在写下<p style="line-height: normal; ">这些文字之前，我也是认为二进制文件就像空气一样，平常得让人完全忽略了。<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 很偶然的，今天在写代码的时候使用了fopen函数：<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILE * fopen (const char * filename, const char * mode)<br style="line-height: normal; ">大家可以看到第二个参数是mode，而这个参数定义了文件打开的方式，w、a等都可以做为函数的实参。除此之外，还有两个值：t和</p><p style="line-height: normal; ">b。这两个值定义了文件是按照文本(text)还是二进制(binary)方式开发。正是这个两个值引起了我对二进制文件的兴趣，因为它们</p><p style="line-height: normal; ">让我想到了很多问题。<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 首先，出现在脑海的第一个问题是：文本文件和二进制文件有什么区别呢？我想这个问题并不是每个程序员能够马上回答</p><p style="line-height: normal; ">上来的，至少我是不行了。查阅了资料之后，发现答案就在自己的知识范围之内的：将文件看作是由一个一个字节(byte) 组成的，</p><p style="line-height: normal; ">那么文本文件中的每个字节的最高位都是0，也就是说文本文件使用了一个字节中的七位来表示所有的信息，而二进制文件则是将字</p><p style="line-height: normal; ">节中的所有位都用上了。这就是两者的区别；<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 接着，第二个问题就是文件按照文本方式或者二进制方式打开，两者会有什么不同呢？其实不管是二进制文件也好，还是</p><p style="line-height: normal; ">文本文件也好，都是一连串的0和1，但是打开方式不同，对于这些0和1的处理也就不同。如果按照文本方式打开，在打开的时候会</p><p style="line-height: normal; ">进行translate，将每个字节转换成ASCII码，而以按照二进制方式打开的话，则不会进行任何的translate；<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最后就是文本文件和二进制文件在编辑的时候，使用的方式也是不同的。譬如，你在记事本中进行文本编辑的时候，你进</p><p style="line-height: normal; ">行编辑的最小单位是字节(byte)；而对二进制文件进行编辑的话，最小单位则是位(bit)，当然我们都不会直接通过手工的方式对二</p><p style="line-height: normal; ">进制文件进行编辑了。</p><p style="line-height: normal; ">----------------2---------------</p><p style="line-height: normal; "><br style="line-height: normal; ">二进制文件和文本文件都是0,1组成,但文件系统对他们的解释不一样,一般系统调用(或WIN32API)都会分为字符<br style="line-height: normal; ">式和二进制式(或流式).文本或字符文件代表慢速设备,而二进制文件代表可以大块数据操作的快速外设,二进制文件内容基本无意义</p><p style="line-height: normal; ">,系统对它不加解释地传给调用者,解释由调用者负责.而对字符文件,系统把他理解为单字节的ASCII或多字节的UNICODE字符串,并且</p><p style="line-height: normal; ">对其中的特殊字符(如回车等)加以特殊处理.所以同一个文件,可以使用不同类型的系统调用.</p><p style="line-height: normal; "></p><p style="line-height: normal; ">文本文件也叫做ASCII码文件，与以&#8216;文本方式&#8217;打开文件不是同一个概念！文本文件存储的是ASSCII码字符，即存储在磁盘上只占用二进制的0x20--0x7e。另外，还有回车(0x0d),换行(0x0a)，TAB(0x09)等，所以有可压缩的空间。</p><p style="line-height: normal; ">&nbsp;&nbsp;&nbsp; 换行和回车是不同的，而且在不同的操作系统，解释也不相同。&#8216;\n&#8217;一般会操作系统被翻译成"行的结束"，即LF(Line-Feed)；&#8216;\r&#8217;会被翻译成"回车"，即CR(Cariage-Return)</p><p style="line-height: normal; ">&nbsp;&nbsp;&nbsp; 回车(CR)和换行(LF)符都是用来表示&#8220;下一行&#8221;的。而标准没有规定要使用哪一个。于是产生了三种不同的用法：<br style="line-height: normal; ">(1) Dos和windows采用回车+换行(CR+LG)表示下一行<br style="line-height: normal; ">(2) UNIX采用换行符(LF)表示下一行<br style="line-height: normal; ">(3) MAC机采用回车符(CR)表示下一行。<br style="line-height: normal; ">当在不同的系统间传递文件，就要涉及格式的转换。</p><p style="line-height: normal; ">&nbsp;&nbsp;&nbsp; 文本方式和二进制方式的最大区别在于文本方式对于'\n'换行符的理解不同<br style="line-height: normal; ">(1)在DOS平台下，该字符会被展开成&lt;CR&gt;&lt; LF&gt;两个控制字符(相当于"\r\n")，在ASCII字符集下是 0DH,0AH<br style="line-height: normal; ">(2)在UNIX平台下，仅仅是&lt;LF&gt;，不会展开。<br style="line-height: normal; ">(3)在二进制方式下，不管是什么平台，'\n'都是精确的&lt;LF&gt;。</p><p style="line-height: normal; ">&nbsp;&nbsp; 在linux/unix 系统上，只有一种文件类型的系统，带b字母的模式和对应的不带b字母的模式是相同的。（UNIX文本文件通常不包含Ctrl+Z和\r）<br style="line-height: normal; ">关于EOF：<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp; EOF可以作为文本文件的结束标志,但不能作为二进制文件的结束符.feof函数既可以判断二进制文件,又可以判断文本文件.</p><p style="line-height: normal; ">&nbsp;&nbsp;&nbsp; EOF在Windows下是ctrl+z，linux下是ctrl+D.</p></span>
<img src ="http://www.cppblog.com/syhd142/aggbug/137695.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2010-12-29 21:46 <a href="http://www.cppblog.com/syhd142/archive/2010/12/29/137695.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转] sudo apt-get 和dpkg命令大全</title><link>http://www.cppblog.com/syhd142/archive/2010/12/29/137662.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Wed, 29 Dec 2010 05:54:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2010/12/29/137662.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/137662.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2010/12/29/137662.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/137662.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/137662.html</trackback:ping><description><![CDATA[
<meta http-equiv="content-type" content="text/html; charset=utf-8"><div>注：找不到出处了，所以就没表明源地址。</div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="font-size: 12px;"><strong><meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="font-weight: normal; font-size: 16px; ">一、APT的使用（Ubuntu Linux软件包管理工具一）<br>apt-cache search # ------(package 搜索包)<br>apt-cache show #------(package 获取包的相关信息，如说明、大小、版本等)<br>sudo apt-get install # ------(package 安装包)<br>sudo apt-get install # -----(package - - reinstall 重新安装包)<br>sudo apt-get -f install # -----(强制安装?#"-f = --fix-missing"当是修复安<br>装吧...)<br>sudo apt-get remove #-----(package 删除包)<br>sudo apt-get remove - - purge # ------(package 删除包，包括删除配置文件<br>等)<br>sudo apt-get autoremove --purge # ----(package 删除包及其依赖的软件包+配置文件等（只对6.10有效，强烈推荐）)<br>sudo apt-get update #------更新源<br>sudo apt-get upgrade #------更新已安装的包<br>sudo apt-get dist-upgrade # ---------升级系统<br>sudo apt-get dselect-upgrade #------使用 dselect 升级<br>apt-cache depends #-------(package 了解使用依赖)<br>apt-cache rdepends # ------(package 了解某个具体的依赖?#当是查看该包被哪些包依赖吧...)<br>sudo apt-get build-dep # ------(package 安装相关的编译环境)<br>apt-get source #------(package 下载该包的源代码)<br>sudo apt-get clean &amp;&amp; sudo apt-get autoclean # --------清理下载文件的存<br>档 &amp;&amp; 只清理过时的包<br>sudo apt-get check #-------检查是否有损坏的依赖<br><br>apt-get install 的用法<br>apt-get install &lt;package&gt;<br>下载 &lt;package&gt; 以及所有倚赖的包裹,同时进行包裹的安装或升级.如果某个包裹被设置了 hold (停止标志,就会被搁在一边(即不会被升级).更多 hold 细节请看下面.<br><br>apt-get remove [--purge] &lt;package&gt;<br>移除 &lt;package&gt; 以及任何倚赖这个包裹的其它包裹.--purge 指明这个包裹应该被完全清除 (purged) ,更多信息请看 dpkg -P .<br><br>apt-get update<br>升级来自 Debian 镜像的包裹列表,如果你想安装当天的任何软件,至少每天运行一次,而且每次修改了<br>/etc/apt/sources.list 后,必须执行.<br><br>apt-get upgrade [-u]<br>升 级所以已经安装的包裹为最新可用版本.不会安装新的或移除老的包裹.如果一个包改变了倚赖关系而需要安装一个新的包裹,那么它将不会被升级,而是标志为 hold .apt-get update 不会升级被标志为 hold 的包裹 (这个也就是 hold 的意思).请看下文如何手动设置包裹为 hold .我建议同时使用 '-u' 选项,因为这样你就能看到哪些包裹将会被升级.<br><br>apt-get dist-upgrade [-u]<br>和 apt-get upgrade 类似,除了 dist-upgrade 会安装和移除包裹来满足倚赖关系.因此具有一定的危险性.<br><br>apt-cache search &lt;pattern&gt;<br>搜索满足 &lt;pattern&gt; 的包裹和描述.<br><br>apt-cache show &lt;package&gt;<br>显示 &lt;package&gt; 的完整的描述.<br><br>apt-cache showpkg &lt;package&gt;<br>显示 &lt;package&gt; 许多细节,以及和其它包裹的关系.<br><br>dselect, console-apt, aptitude, gnome-apt,是APT 的几个图形前端(其中一些在使用前得先安装).这里 dselect 无疑是最强大的,也是最古老,最难驾驭.<br><br>－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br><br>二、 DPKG的 用法（Ubuntu Linux软件包管理工具二）<br>dpkg -i &lt;package.deb&gt;<br>安装一个 Debian 包裹文件;如你手动下载的文件.<br><br>dpkg -c &lt;package.deb&gt;<br>列出 &lt;package.deb&gt; 的内容.<br><br>dpkg -I &lt;package.deb&gt;<br>从 &lt;package.deb&gt; 中提取包裹信息.<br><br>dpkg -r &lt;package&gt;<br>移除一个已安装的包裹.<br><br>dpkg -P &lt;package&gt;<br>完全清除一个已安装的包裹.和 remove 不同的是, remove 只是删掉数据和可执行<br>文件, purge 另外还删除所有的配制文件.<br><br>dpkg -L &lt;package&gt;<br>列出 &lt;package&gt; 安装的所有文件清单.同时请看 dpkg -c 来检查一个 .deb 文件<br>的内容.<br><br>dpkg -s &lt;package&gt;<br>显示已安装包裹的信息.同时请看 apt-cache 显示 Debian 存档中的包裹信息,以<br>及 dpkg -I 来显示从一个<br>.deb 文件中提取的包裹信息.<br><br>dpkg-reconfigure &lt;package&gt;<br>重新配制一个已经安装的包裹,如果它使用的是 debconf (debconf 为包裹安装提<br>供了一个统一的配制界面).你能够重新配制 debconf 它本身,如你想改变它的前端<br>或提问的优先权.例如,重新配制 debconf ,使用一个 dialog 前端,简单运行:<br><br>dpkg-reconfigure --frontend=dialog debconf (如果你安装时选错了,这里可以<br>改回来哟<br><br>echo "&lt;package&gt; hold" | dpkg --set-selections<br>设置 &lt;package&gt; 的状态为 hlod (命令行方式)<br><br>dpkg --get-selections "&lt;package&gt;"<br>取的 &lt;package&gt; 的当前状态 (命令行方式)<br><br>支持通配符,如:<br>Debian:~# dpkg --get-selections *wine*<br><br>备注：<br><br>－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－例如:<br>大家现在用的都是 gaim-0.58 + QQ-plugin,为了防止 gaim 被升级,我们可以采用<br>如下方法:<br><br>方法一:<br>Debian:~# echo "gaim hold" | dpkg --set-selections<br>然后用下面命令检查一下:<br>Debian:~# dpkg --get-selections "gaim"<br>gaim hold<br>现在的状态标志是 hold,就不能被升级了.<br><br>如果想恢复怎么办呢?<br>Debian:~# echo "gaim install" | dpkg --set-selections<br>Debian:~# dpkg --get-selections "gaim"<br>gaim install<br>这时状态标志又被重置为 install,可以继续升级了.<br><br>同志们会问,哪个这些状态标志都写在哪个文件中呢?<br>在 /var/lib/dpkg/status 里,你也可以通过修改这个文件实现 hold.<br><br>有时你会发现有的软件状态标志是 purge,不要奇怪.<br>如:事先已经安装了 amsn, 然后把它卸了.<br>apt-get remove --purge amsn<br>那么状态标志就从 install 变成 purge.<br><br>方法二:<br>在/etc/apt 下手动建一个 preferences 文件<br>内容：<br>Package: gaim<br>Pin: version 0.58*<br>保存<br>更详细内容请看:<br><a href="http://linuxsir.com/bbs/showthread....&amp;threadid=22601" title="http://linuxsir.com/bbs/showthread....&amp;threadid=22601" target="_blank" style="font-size: 12px; line-height: 1.6em; color: rgb(0, 0, 0); text-decoration: none; ">http://linuxsir.com/bbs/showthread....&amp;threadid=22601</a><br><br>dpkg -S &lt;file&gt;<br>在包裹数据库中查找 &lt;file&gt;,并告诉你哪个包裹包含了这个文件.(注:查找的是事<br>先已经安装的包裹)<br><br>从源码建立deb packages<br><br>apt-get source [-b] &lt;package&gt;<br>下载一个源码的包并解开。<br>你必须在你的/etc/apt/sources.list文件里写入一条 deb-src 的记录才能完成这<br>项工作。<br>如果你输入了一个-b参数，并且是以root的身份，deb包会被自动的创建。<br><br>apt-get build-dep &lt;package&gt;<br>自动下载并安装通过源码创建 &lt;package&gt; 时需要的包。<br>只有apt 0.5以上版本才支持这个功能。<br>现在woody和以上版本包含了这个功能。<br>如果你使有一个旧版本的apt，查找依赖性最简单的方法是查看源码包中<br>debian/control 这个文件，<br>注意这个路径是相对的，是包内的路径。<br><br>普通的用法，结合 apt-get source -b,例子 (as root)：<br><br>apt-get build-dep &lt;package&gt;<br>apt-get source -b &lt;package&gt;<br><br>会下载源码包，建立依赖性，然后尝试编译源码。<br><br>dpkg-source -x &lt;package.dsc&gt;<br>如果你手工下载了一个程序的源码包，其中包含了几个类<br>似 .orig.tar.gz , .dsc ,<br>以及 .diff.gz 之类的文件，<br>那么你就可以对 .dsc 文件使用这个命令来 unpack 源码包。<br><br>dpkg-buildpackage<br>从 Debian 源码树建立一个deb包。你必须在source tree的主目录才能生效。例<br>如：<br><br>dpkg-buildpackage -rfakeroot -uc -b<br><br>这里 '-rfakeroot' 指定命令使用 fakeroot 程序来模仿 root 权限 (来实现所有<br>者(ownership)目的)，<br>'-uc' 表示 "Don't cryptographically sign the changelog", '-b' 代表只建立<br>二进制包.<br><br>debuild<br>一个快速打包脚本类似 dpkg-buildpackage ,能自动的识别是否使用 fakeroot,<br>同时为你运行 lintian 和 gpg<br><br>修正倚赖关系<br><br>dpkg --configure --pending<br>如果dpkg在apt-get install upgrade dist-uptradeing 的时候出错退出，<br>尝试使用此命令来配置已经unpack的包。<br>然后再用 apt-get install ，upgrade, or dist-upgrade -f ，<br>然后再用 apt-get install, upgrade, or dist-upgrade.<br><br>可能会重复多次，这样通常可以解决大多数的依赖性问题。<br>(同时,如果提示由于某种原因需要某个特定的包裹,你可以常识安装或卸载这个包)<br><br>apt-get install -f<br>apt-get upgrade -f<br>apt-get dist-upgrade -f<br>尝试修正上述过程中出现依赖性关系<br>注意 apt-get install -f 不需要 &lt;package&gt; 作为参数。<br><br>－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br>用apt-cdrom -d /media/cdrom0 add 加载光驱，<br><br>/dev/hdc /cdrom iso9660 user,noauto 0 0<br>用编辑器修改一下存盘，重启，应该可以了。<br>然后，apt-cdrom -d /cdrom add<br>apt-get install (你要装的软件名：《例如gcc》)<br>gcc相关的软件会自动装入。<br>昨天，刚试过，很爽。<br>注意：apt不会认你手动挂载的光驱，如果fstab中设置不对apt不会正常工作。<br><br>用apt-cdrom -d /media/cdrom0 add 加载光驱，<br>需要使用#mkdir /media/cdrom0 否则会错误提示说&#8220;无法读取文件系统挂载<br>点/media/cdrom的状态 -stat(2 没有那个文件或目录)&#8221;</span></strong></span></div><img src ="http://www.cppblog.com/syhd142/aggbug/137662.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2010-12-29 13:54 <a href="http://www.cppblog.com/syhd142/archive/2010/12/29/137662.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转] Ubuntu 软件包管理详解</title><link>http://www.cppblog.com/syhd142/archive/2010/12/29/137659.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Wed, 29 Dec 2010 05:09:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2010/12/29/137659.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/137659.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2010/12/29/137659.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/137659.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/137659.html</trackback:ping><description><![CDATA[
<meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="font-family: Arial; font-size: 14px; line-height: 21px; "><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">原地址：<a href="http://www.cppblog.com/jb8164/archive/2009/01/09/71583.html">http://www.cppblog.com/jb8164/archive/2009/01/09/71583.html</a></p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; "><a href="http://www.cppblog.com/jb8164/archive/2009/01/09/71583.html"></a>Ubuntu 方便宜用，最值得让人称道的便是其安装软件的方式, 一条命令: sudo apt-get install xxx&nbsp;就几乎能帮你搞定所有的软件安装难题。但是有时你可能有这样的需求，查看某个软件包是否安装、安装在哪..., 那我们就来介绍一下 Ubuntu 的软件包管理方式。</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">一、Ubuntu 采用 Debian 的软件包管理器 dpkg 来管理软件包, 类似 RPM. 系统中所有 packages 的信息都在 /var/lib/dpkg/<br>目录下, 其子目录 /var/lib/dpkg/info 用于保存各个软件包的配置文件列表:<br>&nbsp;(1).conffiles 记录了软件包的配置文件列表<br>&nbsp;(2).list 保存软件包中的文件列表, 用户可以从 .list 的信息中找到软件包中文件的具体安装位置.<br>&nbsp;(3).md5sums 记录了软件包的md5信息, 这个信息是用来进行包验证的.<br>&nbsp;(4).prerm 脚本在 Debian 包解包之前运行, 主要作用是停止作用于即将升级的软件包的服务, 直到软件包安装或升级完成.<br>&nbsp;(5).postinst 脚本是完成 Debian 包解开之后的配置工作, 通常用于执行所安装软件包相关命令和服务重新启动.</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">/var/lib/dpkg/available 文件的内容是软件包的描述信息, 该软件包括当前系统所使用的 Debian 安装源中的所有软件包,<br>其中包括当前系统中已安装的和未安装的软件包.<br></p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">/var/cache/apt/archives 目录是在用 apt-get install 安装软件时，软件包的临时存放路径<br><br>/etc/apt/sources.list 存放的是软件源站点, 当你执行 sudo apt-get install xxx 时，Ubuntu 就去这些站点下载软件包到本地并执行安装<br><br>二、相关命令使用示例:<br>&nbsp;(1)查看某软件包的安装内容<br>&nbsp;&nbsp;&nbsp; dpkg -L xxx</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(2)查找软件库中的软件包<br>&nbsp;&nbsp;&nbsp; apt-cache search 正则表达式</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(3)显示系统安装包的统计信息<br>&nbsp;&nbsp;&nbsp; apt-cache stats<br>&nbsp;<br>&nbsp;(4)显示系统全部可用软件包的名称<br>&nbsp;&nbsp;&nbsp; apt-cache pkgnames</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(5)显示某软件包的详细信息<br>&nbsp;&nbsp;&nbsp; apt-cache show xxx</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(6)查找某文件属于哪个包<br>&nbsp;&nbsp;&nbsp; apt-file search xxx</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(7)查看已经安装了哪些软件包<br>&nbsp;&nbsp;&nbsp; dpkg -l</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(8)查询某软件依赖哪些软件包<br>&nbsp;&nbsp;&nbsp; apt-cache depends xxx</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(9)查询某软件被哪些软件包依赖<br>&nbsp;&nbsp;&nbsp; apt-cache rdepends xxx</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(10)增加一个光盘源<br>&nbsp;&nbsp;&nbsp; sudo apt-cdrom add<br>&nbsp;&nbsp;&nbsp; 注: 顾名思义, 就是安装更新软件包时让其优先从Ubuntu 光盘上找(如果你不能上网安装/更新, 但有 Ubuntu 的 DVD ISO, 这会对你非常有用)</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(11)系统升级<br>&nbsp;&nbsp;&nbsp; sudo apt-get update</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(12)清除所有已删除软件包的残馀配置文件<br>&nbsp;&nbsp;&nbsp; dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(13)编译时缺少h文件的自动处理<br>&nbsp;&nbsp;&nbsp; sudo auto-apt run ./configure</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(14)查看安装软件时下载软件包的临时存放目录<br>&nbsp;&nbsp;&nbsp; ls /var/cache/apt/archives</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(15)备份当前系统安装的所有软件包的列表<br>&nbsp;&nbsp;&nbsp; dpkg --get-selections | grep -v deinstall &gt; ~/somefile</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(16)从上面备份的安装包的列表文件恢复所有包<br>&nbsp;&nbsp;&nbsp; dpkg --set-selections &lt; ~/somefile<br>&nbsp;&nbsp;&nbsp; sudo dselect</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(17)清理旧版本的软件缓存<br>&nbsp;&nbsp;&nbsp; sudo apt-get autoclean</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(18)清理所有软件缓存<br>&nbsp;&nbsp;&nbsp; sudo apt-get clean</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(19)删除系统不再使用的孤立软件<br>&nbsp;&nbsp;&nbsp; sudo apt-get autoremove</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 14px; ">&nbsp;(20)查看软件包在服务器上面的地址<br>&nbsp;&nbsp;&nbsp; apt-get -qq --print-uris install ssh | cut -d\' -f2</p></span>
<img src ="http://www.cppblog.com/syhd142/aggbug/137659.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2010-12-29 13:09 <a href="http://www.cppblog.com/syhd142/archive/2010/12/29/137659.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何去掉代码中的行号</title><link>http://www.cppblog.com/syhd142/archive/2010/12/21/137141.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Tue, 21 Dec 2010 13:30:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2010/12/21/137141.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/137141.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2010/12/21/137141.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/137141.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/137141.html</trackback:ping><description><![CDATA[<div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000; ">我们在从网上拷贝程序的时候有的时候会带有行号，上次记得在shell命令里面有一个按照列分割的命令，但是忘记了，今天看网页的时候突然看到，于是就试了一下，发现可以通过pipe实现，这个功能很好。<br>例，有如下代码：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">2</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">3</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">4</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">5</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;argc,&nbsp;</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">argv[])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">6</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">7</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">8</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;&nbsp;i&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;argc;&nbsp;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">9</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">10</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d&nbsp;argument&nbsp;is&nbsp;%s\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,&nbsp;i,&nbsp;argv[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">11</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">12</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">13</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br>需要去掉前面的行号，当然我们可以写一个程序实现该功能，但是这样太麻烦，还要编译。我们通过cut命令实现。<br>具体方法:cut&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">cstart_pos</span><span style="color: #000000; ">-</span><span style="color: #000000; ">end_pos&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;input_file<br>或者:cut&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">cstart_pos</span><span style="color: #000000; ">-</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;input_file<br>在上面那个例子中用cat&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">c8</span><span style="color: #000000; ">-</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;input_file就行啦。</span></div><br><img src ="http://www.cppblog.com/syhd142/aggbug/137141.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2010-12-21 21:30 <a href="http://www.cppblog.com/syhd142/archive/2010/12/21/137141.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转] 你知道Linux网络协议栈吗？</title><link>http://www.cppblog.com/syhd142/archive/2010/12/14/136394.html</link><dc:creator>Fucker</dc:creator><author>Fucker</author><pubDate>Tue, 14 Dec 2010 09:21:00 GMT</pubDate><guid>http://www.cppblog.com/syhd142/archive/2010/12/14/136394.html</guid><wfw:comment>http://www.cppblog.com/syhd142/comments/136394.html</wfw:comment><comments>http://www.cppblog.com/syhd142/archive/2010/12/14/136394.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/syhd142/comments/commentRss/136394.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/syhd142/services/trackbacks/136394.html</trackback:ping><description><![CDATA[




<div>原文地址：<a href="http://network.51cto.com/art/201009/227036.htm">http://network.51cto.com/art/201009/227036.htm</a></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div class="sum" style="padding-top: 0px; padding-right: 24px; padding-bottom: 0px; padding-left: 24px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; line-height: 26px; "><ul class="summary" style="padding-top: 8px; padding-right: 16px; padding-bottom: 4px; padding-left: 16px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; line-height: 24px; width: 554px; font-size: 14px; color: rgb(0, 81, 151); background-color: rgb(254, 253, 232); border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(221, 230, 241); border-right-color: rgb(221, 230, 241); border-bottom-color: rgb(221, 230, 241); border-left-color: rgb(221, 230, 241); ">Linux网络协议栈是我们接下来要具体分析的内容。通过文章解析，我们能够掌握这个概念，功能以及简单的应用。</ul></div><ul class="con" style="padding-top: 10px; padding-right: 24px; padding-bottom: 0px; padding-left: 24px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; line-height: 26px; font-size: 14px; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">Linux网络协议栈跟Windows系统中的网络协议有什么不一样呢？这个还需要我们来看看具体的内容。下面就来简单看看它的概念，结构以及相应的一些解说吧。望对大家有所帮助。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; "><strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Linux网络协议栈</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">Linux的协议栈其实是源于BSD的协议栈，它向上以及向下的接口以及协议栈本身的软件分层组织的非常好。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">Linux的协议栈基于分层的设计思想，总共分为四层，从下往上依次是 ：物理层，链路层，网络层，应用层。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">物理层主要提供各种连接的物理设备，如各种网卡，串口卡等；链路层主要指的是提供对物理层进行访问的各种接口卡的驱动程序，如网卡驱动等；网路层的作用是负责将网络数据包传输到正确的位置，最重要的网络层协议当然就是IP协议了，其实网络层还有其他的协议如ICMP，ARP，RARP等，只不过不像IP那样被多数人所熟悉；传输层的作用主要是提供端到端，说白一点就是提供应用程序之间的通信，传输层最著名的协议非TCP与UDP协议末属了；应用层，顾名思义，当然就是由应用程序提供的，用来对传输数据进行语义解释的&#8220;人机界面&#8221;层了，比如HTTP，SMTP，FTP等等，其实应用层还不是人们最终所看到的那一层，最上面的一层应该是&#8220;解释层&#8221;，负责将数据以各种不同的表项形式最终呈献到人们眼前。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; "><strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Linux网络核心架构</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">Linux的网络架构从上往下可以分为三层，分别是 ：</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">用户空间的应用层。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">内核空间的网络协议栈层。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">物理硬件层。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">其中最重要最核心的当然是内核空间的协议栈层了。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; "><strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Linux网络协议栈结构</strong></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">Linux的整个网络协议栈都构建与Linux Kernel中，整个栈也是严格按照分层的思想来设计的，整个栈共分为五层，分别是 ：</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">1． 系统调用接口层，实质是一个面向用户空间应用程序的接口调用库，向用户空间应用程序提供使用网络服务的接口。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">2． 协议无关的接口层，就是SOCKET层，这一层的目的是屏蔽底层的不同协议（更准确的来说主要是TCP与UDP，当然还包括RAW IP， SCTP等），以便与系统调用层之间的接口可以简单，统一。简单的说，不管我们应用层使用什么协议，都要通过系统调用接口来建立一个SOCKET，这个SOCKET其实是一个巨大的sock结构，它和下面一层的网络协议层联系起来，屏蔽了不同的网络协议的不同，只吧数据部分呈献给应用层（通过系统调用接口来呈献）。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">3． 网络协议实现层，毫无疑问，这是整个协议栈的核心。这一层主要实现各种网络协议，最主要的当然是IP，ICMP，ARP，RARP，TCP，UDP等。这一层包含了很多设计的技巧与算法，相当的不错。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">4． 与具体设备无关的驱动接口层，这一层的目的主要是为了统一不同的接口卡的驱动程序与网络协议层的接口，它将各种不同的驱动程序的功能统一抽象为几个特殊的动作，如open，close，init等，这一层可以屏蔽底层不同的驱动程序。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">5． 驱动程序层，这一层的目的就很简单了，就是建立与硬件的接口层。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">可以看到，Linux网络协议栈是一个严格分层的结构，其中的每一层都执行相对独立的功能，结构非常清晰。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; text-indent: 28px; ">其中的两个&#8220;无关&#8221;层的设计非常棒，通过这两个&#8220;无关&#8221;层，其协议栈可以非常轻松的进行扩展。在我们自己的软件设计中，可以吸收这种设计方法。</p></ul></div><img src ="http://www.cppblog.com/syhd142/aggbug/136394.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/syhd142/" target="_blank">Fucker</a> 2010-12-14 17:21 <a href="http://www.cppblog.com/syhd142/archive/2010/12/14/136394.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>