﻿<?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++博客-good good code,day day up-文章分类-Linux/Unix</title><link>http://www.cppblog.com/dawnbreak/category/8987.html</link><description>http://blog.csdn.net/dawnbreak/</description><language>zh-cn</language><lastBuildDate>Tue, 17 Mar 2009 16:58:13 GMT</lastBuildDate><pubDate>Tue, 17 Mar 2009 16:58:13 GMT</pubDate><ttl>60</ttl><item><title>Windows下 POSIX 线程编程</title><link>http://www.cppblog.com/dawnbreak/articles/76946.html</link><dc:creator>pear_li</dc:creator><author>pear_li</author><pubDate>Tue, 17 Mar 2009 15:58:00 GMT</pubDate><guid>http://www.cppblog.com/dawnbreak/articles/76946.html</guid><wfw:comment>http://www.cppblog.com/dawnbreak/comments/76946.html</wfw:comment><comments>http://www.cppblog.com/dawnbreak/articles/76946.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/dawnbreak/comments/commentRss/76946.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dawnbreak/services/trackbacks/76946.html</trackback:ping><description><![CDATA[<h3><strong>1. POSIX 标准</strong></h3>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; POSIX是Portable Operating System Interface of Unix的缩写。由IEEE（Institute of Electrical and Electronic Engineering）开发，由ANSI和ISO标准化。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; POSIX的诞生和Unix的发展是密不可分的，Unix于70年代诞生于Bell lab，并于80年代向美各大高校分发V7版的源码以做研究。UC Berkeley在V7的基础上开发了BSD Unix。后来很多商业厂家意识到Unix的价值也纷纷以Bell Lab的System V或BSD为基础来开发自己的Unix，较著名的有Sun OS，AIX，VMS。由于各厂家对Unix的开发各自为政，造成了Unix的版本相当混乱，给软件的可移植性带来很大困难，对Unix的发展极为不利。为结束这种局面，IEEE开发了POSIX，POSIX在源代码级别上定义了一组最小的Unix(类Unix)操作系统接口。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; POSIX 表示可移植操作系统接口（Portable Operating System Interface ，缩写为 POSIX 是为了读音更像 UNIX）。电气和电子工程师协会（Institute of Electrical and Electronics Engineers，IEEE）最初开发 POSIX 标准，是为了提高 UNIX 环境下应用程序的可移植性。然而，POSIX 并不局限于 UNIX。许多其它的操作系统，例如 DEC OpenVMS 和 Microsoft Windows NT，都支持 POSIX 标准，尤其是 IEEE Std. 1003.1-1990（1995 年修订）或 POSIX.1，POSIX.1 提供了源代码级别的 C 语言应用编程接口（API）给操作系统的服务程序，例如读写文件。POSIX.1 已经被国际标准化组织（International Standards Organization，ISO）所接受，被命名为 ISO/IEC 9945-1:1990 标准。 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; POSIX 现在已经发展成为一个非常庞大的标准族，某些部分正处在开发过程中。表 1-1 给出了 POSIX 标准的几个重要组成部分。POSIX 与 IEEE 1003 和 2003 家族的标准是可互换的。除 1003.1 之外，1003 和 2003 家族也包括在表中。 </p>
<p><strong>表1：标准的重要组成部分</strong><br>1003.0 <br>管理 POSIX 开放式系统环境（OSE）。IEEE 在 1995 年通过了这项标准。 ISO 的版本是 ISO/IEC 14252:1996。 <br>1003.1 <br>被广泛接受、用于源代码级别的可移植性标准。1003.1 提供一个操作系统的 C 语言应用编程接口（API）。IEEE 和 ISO 已经在 1990 年通过了这个标准，IEEE 在 1995 年重新修订了该标准。 <br>1003.1b <br>一个用于实时编程的标准（以前的 P1003.4 或 POSIX.4）。这个标准在 1993 年被 IEEE 通过，被合并进 ISO/IEC 9945-1。 <br>1003.1c <br>一个用于线程（在一个程序中当前被执行的代码段）的标准。以前是 P1993.4 或 POSIX.4 的一部分，这个标准已经在 1995 年被 IEEE 通过，归入 ISO/IEC 9945-1:1996。 <br>1003.1g <br>一个关于协议独立接口的标准，该接口可以使一个应用程序通过网络与另一个应用程序通讯。 1996 年，IEEE 通过了这个标准。 <br>1003.2 <br>一个应用于 shell 和 工具软件的标准，它们分别是操作系统所必须提供的命令处理器和工具程序。 1992 年 IEEE 通过了这个标准。ISO 也已经通过了这个标准（ISO/IEC 9945-2:1993）。 <br>1003.2d <br>改进的 1003.2 标准。 <br>1003.5 <br>一个相当于 1003.1 的 Ada 语言的 API。在 1992 年，IEEE 通过了这个标准。并在 1997 年对其进行了修订。ISO 也通过了该标准。 <br>1003.5b <br>一个相当于 1003.1b（实时扩展）的 Ada 语言的 API。IEEE 和 ISO 都已经通过了这个标准。ISO 的标准是 ISO/IEC 14519:1999。 <br>1003.5c <br>一个相当于 1003.1q（协议独立接口）的 Ada 语言的 API。在 1998 年， IEEE 通过了这个标准。ISO 也通过了这个标准。 <br>1003.9 <br>一个相当于 1003.1 的 FORTRAN 语言的 API。在 1992 年，IEEE 通过了这个标准，并于 1997 年对其再次确认。ISO 也已经通过了这个标准。 <br>1003.10 <br>一个应用于超级计算应用环境框架（Application Environment Profile，AEP）的标准。在 1995 年，IEEE 通过了这个标准。 <br>1003.13 <br>一个关于应用环境框架的标准，主要针对使用 POSIX 接口的实时应用程序。在 1998 年，IEEE 通过了这个标准。 <br>1003.22 <br>一个针对 POSIX 的关于安全性框架的指南。 <br>1003.23 <br>一个针对用户组织的指南，主要是为了指导用户开发和使用支持操作需求的开放式系统环境（OSE）框架 <br>2003 <br>针对指定和使用是否符合 POSIX 标准的测试方法，有关其定义、一般需求和指导方针的一个标准。在 1997 年，IEEE 通过了这个标准。 <br>2003.1 <br>这个标准规定了针对 1003.1 的 POSIX 测试方法的提供商要提供的一些条件。在 1992 年，IEEE 通过了这个标准。 <br>2003.2 <br>一个定义了被用来检查与 IEEE 1003.2（shell 和 工具 API）是否符合的测试方法的标准。在 1996 年，IEEE 通过了这个标准。 </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 除了 1003 和 2003 家族以外，还有几个其它的 IEEE 标准，例如 1224 和 1228，它们也提供开发可移植应用程序的 API。要想得到关于 IEEE 标准的最新信息，可以访问 IEEE 标准的主页，网址是 <a href="http://standards.ieee.org/"><u><font color=#0066cc>http://standards.ieee.org/</font></u></a>。有关 POSIX 标准的概述信息，请访问 Web 站点 <a href="http://standards.ieee.org/reading/ieee/stad_public/description/posix/"><u><font color=#0066cc>http://standards.ieee.org/reading/ieee/stad_public/description/posix/</font></u></a>。</p>
<h3><strong>2. Liniux下的线程编程</strong></h3>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Linux系统下的多线程遵循POSIX线程接口，称为pthread。从上面的描述不难知道，<strong>POSIX线程接口是POSIX众多标准中的一个（<a href="http://www.unix-systems.org/version3/ieee_std.html"><u><font color=#0066cc>POSIX 1003.1-2001</font></u></a>）</strong>。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 编写Linux下的多线程程序，需要使用头文件pthread.h，连接时需要使用库libpthread.a。顺便说一下，Linux下pthread的实现是通过系统调用 clone() 来实现的。clone() 是Linux所特有的系统调用，它的使用方式类似fork，关于 clone() 的详细情况，有兴趣的读者可以去查看有关文档说明。</p>
<p>下面是一个 POSIX 线程的简单示例程序(thread1.c)： </p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">pthread.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdlib.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">unistd.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img id=_95_198_Open_Image onclick="this.style.display='none'; document.getElementById('_95_198_Open_Text').style.display='none'; document.getElementById('_95_198_Closed_Image').style.display='inline'; document.getElementById('_95_198_Closed_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056210.gif" align=top><img id=_95_198_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_95_198_Closed_Text').style.display='none'; document.getElementById('_95_198_Open_Image').style.display='inline'; document.getElementById('_95_198_Open_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056233.gif" align=top>&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">thread_function(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arg)&nbsp;</span><span id=_95_198_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id=_95_198_Open_Text><span style="COLOR: #000000">{<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i;<br><img id=_130_181_Open_Image onclick="this.style.display='none'; document.getElementById('_130_181_Open_Text').style.display='none'; document.getElementById('_130_181_Closed_Image').style.display='inline'; document.getElementById('_130_181_Closed_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056227.gif" align=top><img id=_130_181_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_130_181_Closed_Text').style.display='none'; document.getElementById('_130_181_Open_Image').style.display='inline'; document.getElementById('_130_181_Open_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056512.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;</span><span id=_130_181_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id=_130_181_Open_Text><span style="COLOR: #000000">{<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Thread&nbsp;says&nbsp;hi! </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;sleep(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056186.gif" align=top>&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;NULL;<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056497.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img id=_215_472_Open_Image onclick="this.style.display='none'; document.getElementById('_215_472_Open_Text').style.display='none'; document.getElementById('_215_472_Closed_Image').style.display='inline'; document.getElementById('_215_472_Closed_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056210.gif" align=top><img id=_215_472_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_215_472_Closed_Text').style.display='none'; document.getElementById('_215_472_Open_Image').style.display='inline'; document.getElementById('_215_472_Open_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056233.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)&nbsp;</span><span id=_215_472_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id=_215_472_Open_Text><span style="COLOR: #000000">{<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;pthread_t&nbsp;mythread;<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;<br><img id=_307_362_Open_Image onclick="this.style.display='none'; document.getElementById('_307_362_Open_Text').style.display='none'; document.getElementById('_307_362_Closed_Image').style.display='inline'; document.getElementById('_307_362_Closed_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056227.gif" align=top><img id=_307_362_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_307_362_Closed_Text').style.display='none'; document.getElementById('_307_362_Open_Image').style.display='inline'; document.getElementById('_307_362_Open_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056512.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;pthread_create(&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">mythread,&nbsp;NULL,&nbsp;thread_function,&nbsp;NULL)&nbsp;)&nbsp;</span><span id=_307_362_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id=_307_362_Open_Text><span style="COLOR: #000000">{<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">error&nbsp;creating&nbsp;thread.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;abort();<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056186.gif" align=top>&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img id=_405_459_Open_Image onclick="this.style.display='none'; document.getElementById('_405_459_Open_Text').style.display='none'; document.getElementById('_405_459_Closed_Image').style.display='inline'; document.getElementById('_405_459_Closed_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056227.gif" align=top><img id=_405_459_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_405_459_Closed_Text').style.display='none'; document.getElementById('_405_459_Open_Image').style.display='inline'; document.getElementById('_405_459_Open_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056512.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;pthread_join&nbsp;(&nbsp;mythread,&nbsp;NULL&nbsp;)&nbsp;)&nbsp;</span><span id=_405_459_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id=_405_459_Open_Text><span style="COLOR: #000000">{<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">error&nbsp;joining&nbsp;thread.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;abort();<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056186.gif" align=top>&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;exit(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056497.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top></span></div>
</div>
<p>&nbsp;要编译这个程序，只需先将程序存为 thread1.c，然后输入：</p>
<div>$ gcc thread1.c -o thread1 -lpthread</div>
<p>运行则输入：</p>
<div>$ ./thread1</div>
<h3><strong>3. Windows下POSIX线程编程</strong></h3>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Windows本身没有提供对POSIX的支持。但有一个叫 <a href="http://sources.redhat.com/pthreads-win32/"><u><font color=#0066cc>POSIX Threads for Win32</font></u></a> 的开源项目给出了一个功能比较完善的Windows下pthreads API的实现。目前的最新版本是Pthreads-w32 release 2.8.0 (2006-12-22)。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我没有测试过这个最新版本，这里只给出2.7.0版的链接：<a title=ftp://sources.redhat.com/pub/pthreads-win32/pthreads-w32-2-7-0-release.exe href="ftp://sources.redhat.com/pub/pthreads-win32/pthreads-w32-2-7-0-release.exe"><u><font color=#0066cc>ftp://sources.redhat.com/pub/pthreads-win32/pthreads-w32-2-7-0-release.exe</font></u></a>。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 关于该开源项目的详细介绍见：<a title=http://sources.redhat.com/pthreads-win32/ href="http://sources.redhat.com/pthreads-win32/"><u><font color=#0066cc>http://sources.redhat.com/pthreads-win32/</font></u></a>。</p>
<h4><strong>3.1 简单使用</strong></h4>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 上面的exe文件是一个自解压文件，解压后，Pre-built.2目录中有编译所需要的头文件（include子目录）和库文件（lib子目录）。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一个简单的测试程序(main.cpp)：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top><span style="COLOR: #000000">#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><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">pthread.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">assert.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;Function_t(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;Param)<br><img id=_91_214_Open_Image onclick="this.style.display='none'; document.getElementById('_91_214_Open_Text').style.display='none'; document.getElementById('_91_214_Closed_Image').style.display='inline'; document.getElementById('_91_214_Closed_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056210.gif" align=top><img id=_91_214_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_91_214_Closed_Text').style.display='none'; document.getElementById('_91_214_Open_Image').style.display='inline'; document.getElementById('_91_214_Open_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056233.gif" align=top></span><span id=_91_214_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id=_91_214_Open_Text><span style="COLOR: #000000">{<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">I&nbsp;am&nbsp;a&nbsp;thread!&nbsp;&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;myid&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;pthread_self();<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">thread&nbsp;ID=%d&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;myid);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;NULL;<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056497.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=_228_596_Open_Image onclick="this.style.display='none'; document.getElementById('_228_596_Open_Text').style.display='none'; document.getElementById('_228_596_Closed_Image').style.display='inline'; document.getElementById('_228_596_Closed_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056210.gif" align=top><img id=_228_596_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_228_596_Closed_Text').style.display='none'; document.getElementById('_228_596_Open_Image').style.display='inline'; document.getElementById('_228_596_Open_Text').style.display='inline';" alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056233.gif" align=top></span><span id=_228_596_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id=_228_596_Open_Text><span style="COLOR: #000000">{<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;pid;<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_attr_t&nbsp;attr;<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_attr_init(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">attr);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_attr_setscope(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">attr,&nbsp;PTHREAD_SCOPE_PROCESS);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_attr_setdetachstate(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">attr,&nbsp;PTHREAD_CREATE_DETACHED);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_create(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">pid,&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">attr,&nbsp;Function_t,&nbsp;NULL);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">========================================&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;pthread_attr_destroy(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">attr);<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056264.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056497.gif" align=top>}</span></span><span style="COLOR: #000000">&nbsp;</span></div>
</div>
<p>使用 cl.exe 编译（不熟悉 cl.exe 的请参考：<a title=http://blog.csdn.net/liuyongjin1984/archive/2008/01/07/2029405.aspx href="http://blog.csdn.net/liuyongjin1984/archive/2008/01/07/2029405.aspx"><u><font color=#800080>http://blog.csdn.net/liuyongjin1984/archive/2008/01/07/2029405.aspx</font></u></a> 或者参见下面3.2部分）：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top>》<span style="COLOR: #000000">rem&nbsp;cl.bat<br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top>》cl.exe&nbsp;main.cpp &nbsp;</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">c&nbsp; </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">I</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">c:pthreads-w32-2-7-0-releasePre-built.2include</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top><br><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056480.gif" align=top>》link.exe&nbsp; </span><span style="COLOR: #000000">/</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">:main_cl.exe&nbsp; main.obj&nbsp; </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">LIBPATH:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">c:pthreads-w32-2-7-0-releasePre-built.2lib</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;&nbsp; pthreadVC2.lib</span></div>
</div>
<p><font size=3>&nbsp;<strong>3.2 使用VC++ 6.0或Visual Studio 2005来运行上面的程序</strong></font></p>
<p>关键有两点：</p>
<p>1. 是将<strong>头文件（include子目录）和库文件（lib子目录）</strong>中的内容添加到VC++ 6.0或Visual Studio 2005开发环境对应的include和lib目录下。</p>
<p>具体来说（<strong>以添加include目录为例，添加lib目录类似</strong>）：</p>
<p><strong>图1：VC++ 6.0（添加include目录：工具--》选项--》目录）</strong></p>
<p><a href="http://p.blog.csdn.net/images/p_blog_csdn_net/liuyongjin1984/WindowsLiveWriter/WindowsPOSIX_14824/1.png"></a><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056899.bmp">&nbsp;</p>
<p>&nbsp;</p>
<p><strong>图2：Visual Studio 2005(添加include目录：tools--》options)</strong></p>
<p><a href="http://p.blog.csdn.net/images/p_blog_csdn_net/liuyongjin1984/WindowsLiveWriter/WindowsPOSIX_14824/2.png"></a><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056941.bmp">&nbsp;</p>
<p>&nbsp;</p>
<p><strong>2. 指定link时要连接的库的名称（pthreadVC2.lib）</strong></p>
<p><strong>图3：VC++ 6.0（工程--》设置--》连接）</strong></p>
<p><strong><a href="http://p.blog.csdn.net/images/p_blog_csdn_net/liuyongjin1984/WindowsLiveWriter/WindowsPOSIX_14824/3.png"></a></strong></p>
<p><strong><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056613.bmp"></strong></p>
<p><strong></strong></p>
<p><strong>图4：Visual Studio 2005(project--&gt;* property pages)</strong></p>
<p><a href="http://p.blog.csdn.net/images/p_blog_csdn_net/liuyongjin1984/WindowsLiveWriter/WindowsPOSIX_14824/4.png"></a><img alt="" src="http://www.newbooks.com.cn/infos/pic/200801/179364/20080109010056694.bmp">&nbsp;</p>
<strong></strong>
<h3>4. 书籍推荐<br><font size=3>&lt;&lt; POSIX Multithread Programming Primer &gt;&gt;:</font> <a href="http://download.csdn.net/source/237125"><u><font color=#0066cc>http://download.csdn.net/source/237125</font></u></a></h3>
<img src ="http://www.cppblog.com/dawnbreak/aggbug/76946.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dawnbreak/" target="_blank">pear_li</a> 2009-03-17 23:58 <a href="http://www.cppblog.com/dawnbreak/articles/76946.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>cygwin 使用</title><link>http://www.cppblog.com/dawnbreak/articles/68393.html</link><dc:creator>pear_li</dc:creator><author>pear_li</author><pubDate>Tue, 02 Dec 2008 07:09:00 GMT</pubDate><guid>http://www.cppblog.com/dawnbreak/articles/68393.html</guid><wfw:comment>http://www.cppblog.com/dawnbreak/comments/68393.html</wfw:comment><comments>http://www.cppblog.com/dawnbreak/articles/68393.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/dawnbreak/comments/commentRss/68393.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dawnbreak/services/trackbacks/68393.html</trackback:ping><description><![CDATA[<p>对于经常在Windows编程的使用者，想体验*nix编程可以选择vw，vpc，双系统，而对于真正的开发者而言一个*nix shell加上<span style="COLOR: red">GCC</span> <span style="COLOR: red">GDB</span> <span style="COLOR: red">MAKE</span>以及相关的函数库文件足以，哦，也许加上DEVDOC才算完整，也就是可以使用<span style="COLOR: red">man</span>，所有这些cygwin完全可以搞定。<br>cygwin简介：<br>这个问题你最好google一下"cygwin的历史",或许能够获得更为详尽的答案。简而言之，cygwin是一个在windows平台上运行的 linux模拟环境，是cygnus solutions公司开发的自由软件（该公司开发了很多好东西，著名的还有eCos，不过现已被Redhat收购）。插一句废话，很多朋友不明白 linux和unix的区别和联系，在此也简要介绍一下。UNIX是一个注册商标，是要满足一大堆条件并且支付可观费用才能够被授权使用的一个操作系统; linux是unix的克隆版本，是由其创始人Linus和诸多世界知名的黑客手工打造的一个操作系统。为什么linux和unix之间有很多软件可以很轻松的移植？因为linux也满足POSIX规范,所以在运行机制上跟unix相近。&nbsp;&nbsp; <br>&nbsp;&nbsp; 以下引用网上的一段话（出处：<a href="http://blog.csdn.net/glock18/archive/2004/07/10/38275.aspx"><u><font color=#e6a306>http://blog.csdn.net/glock18/archive/2004/07/10/38275.aspx</font></u></a>），<br>用于说明cygwin的工作机制：cygnus当初首先把gcc，gdb，gas等开发工具进行了改进，使他们能够生成并解释win32的目标文件。然后，他们要把这些工具移植到windows平台上去。一种方案是基于win32 api对这些工具的源代码进行大幅修改，这样做显然需要大量工作。因此，他们采取了一种不同的方法——他们写了一个共享库(就是cygwin dll)，把win32 api中没有的unix风格的调用（如fork,spawn,signals,select,sockets等）封装在里面，也就是说，他们基于 win32 api写了一个unix系统库的模拟层。这样，只要把这些工具的源代码和这个共享库连接到一起，就可以使用unix主机上的交叉编译器来生成可以在 windows平台上运行的工具集。以这些移植到windows平台上的开发工具为基础，cygnus又逐步把其他的工具（几乎不需要对源代码进行修改，只需要修改他们的配置脚本）软件移植到windows上来。这样，在windows平台上运行bash和开发工具、用户工具，感觉好像在unix上工作。关于cygwin实现的更详细描述，请参考<a href="http://cygwin.com/cygwin-ug-net/highlights.html"><u><font color=#e6a306>http://cygwin.com/cygwin-ug-net/highlights.html</font></u></a><br><br>那么接下来就安装：<br>&nbsp;1. 先去Cygwin网站（www.cygwin.com）下载一个安装文件（setup.exe），这个文件体积很小，只有不到300KB。然后双击运行setup.exe。因为是第一次安装，所以必须选择从Internet在线安装，也可以先从Internet下载安装文件，然后再手动安装。我选择后者，因为这样，以后我可以在不联网的时候也能安装。</p>
<p>2. 接下来点击&#8220;下一步&#8221;，然后从一系列服务器列表中选择一个你认为网速最快的一个作为下载服务器。接着点击&#8220;下一步&#8221;就会出现如下图所示的界面，用来选择你想安装的程序。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 如果你不做任何修改，将默认安装Cygwin运行所需的最少的程序和组件。不过，gcc编译器不在默认安装程序之列，所以你必须选择安装gcc。具体是， 在上述窗口中的列表中展开Devel目录，找到gcc-g++一项，点击&#8220;Default&#8221;，它就变成了&#8220;Install&#8221;，同时由于程序之间的依赖性 gcc-core条目的&#8220;Default&#8221;，也变成了&#8220;Install&#8221;。</p>
<p>&nbsp;&nbsp;&nbsp; 此外，为了使gcc能正确编译源文件，还必须安装win32api库，否则会编译出错。因此，你需要在上述窗口中展开Libs目录，找到win32api一项，将它点成&#8220;Install&#8221;。</p>
3. 接下来就是耐心等待程序去下载所需的安装文件了。一旦下载完毕，你再次运行setup.exe，先选择你将要把Cygwin安装在什么地方，然后再选择你 刚才所下载的那些安装文件所在的硬盘位置。接着你还需要再次选择安装gcc和win32api。这样，你就可以开始安装了。
<p>4. 安装完成后，运行Cygwin。在窗口中敲入gcc就可以直接用gcc来编译了。编译链接生成的可执行文件，系统会自动加上.exe后缀，在Cygwin 环境中可以直接运行。但如果脱离Cygwin环境，而在MS DOS下运行，则会出错。解决办法很简单，那就是将cygwin1.dll文件拷贝到C:\WINDOWS目录下即可。<br><br><span style="COLOR: red">在这里我想强调，如果图省事，完全可以直接将all右边的default设为install。但是这将这样你4个G的空间，所以如果你不是拥有特别大的空间的话，还是建议选择性的安装，需要什么可以随时点击先前的setup.exe加装新的组件除去以上所说的组件外，在devel下的GDB和make相信也是大多数开发者所必须的。<br></span>至此已将解决的了文章开头提及的前三项要件，而第四项man则是cygwin默认安装的，不过不是很全面，所以可以通过<a href="http://tldp.org/manpages/man.php"><u><font style="COLOR: #3366ff" color=#ffffff>The Linux Documentation Project </font></u></a>（很好很强大的文档网站）来解决，不过貌似这个网站大陆打不开，反正我是没打开，还好有香港同胞建立的mirror，比较好用的是<a href="ftp://ftp.hkmirror.org/pub/ldp/manpages/">ftp://ftp.hkmirror.org/pub/ldp/manpages/</a>，在其中找到适合你的man版本，下载，解压到$(cygwin)目录下的/usr/share/man/下面，编辑/usr/share/misc/man.conf，文件末尾加上MANPATH&nbsp;/usr/share/man/man-pages-*.**即可。<br><br>好啦，如下配置后，启动cygwin，来编一个小程序helloworld热热身<br>随便选一个目录建立helloworld.cc文件，记事本打开，写入以下代码：<br>#include "stdio.h"<br>int<br>main(int args,char* argv[])<br>{<br>&nbsp;printf("helloworld");<br>&nbsp;return 0;<br>}<br>保存，启动cygwin cd到helloworld.cc文件目录，执行<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">$&nbsp;gcc&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">o&nbsp;helloworld&nbsp;helloworld.cc</span></div>
然后执行<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">$&nbsp;.</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">helloworld</span></div>
得到结果<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">helloworld</span></div>
<br><span style="COLOR: #0000ff">Appendix<br><br>pwd&nbsp;&nbsp; 显示当前的路径<br>cd&nbsp;&nbsp;&nbsp; 改变当前路径，无参数时进入对应用户的home目录<br>ls&nbsp;&nbsp;&nbsp; 列出当前目录下的文件。此命令有N多参数，比如ls -al<br>ps&nbsp;&nbsp;&nbsp; 列出当前系统进程<br>kill&nbsp; 杀死某个进程<br>mkdir 建立目录<br>rmdir 删除目录<br>rm&nbsp;&nbsp;&nbsp; 删除文件<br>mv&nbsp;&nbsp;&nbsp; 文件改名或目录改名<br>man&nbsp;&nbsp; 联机帮助<br>less&nbsp; 显示文件的最末几行<br>df&nbsp;&nbsp;&nbsp; 显示文件系统<br></span>
<img src ="http://www.cppblog.com/dawnbreak/aggbug/68393.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dawnbreak/" target="_blank">pear_li</a> 2008-12-02 15:09 <a href="http://www.cppblog.com/dawnbreak/articles/68393.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux 中的一些快捷键</title><link>http://www.cppblog.com/dawnbreak/articles/68119.html</link><dc:creator>pear_li</dc:creator><author>pear_li</author><pubDate>Fri, 28 Nov 2008 17:21:00 GMT</pubDate><guid>http://www.cppblog.com/dawnbreak/articles/68119.html</guid><wfw:comment>http://www.cppblog.com/dawnbreak/comments/68119.html</wfw:comment><comments>http://www.cppblog.com/dawnbreak/articles/68119.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/dawnbreak/comments/commentRss/68119.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dawnbreak/services/trackbacks/68119.html</trackback:ping><description><![CDATA[载终端中，复制为ctrl-shift-c，黏贴为ctrl-shift-v<br>而在ubuntu中定义两者可以有鼠标中键完成<br>其他终端快捷键<br>终端快捷键<br>
tab=补全<br>
ctrl+a=开始位置<br>
ctrl+e=最后位置<br>
ctrl+k=删除此处至末尾所有内容<br>
ctrl+u=删除此处至开始所有内容<br>常用<br>
win+n切换背景颜色风格<br>
alt+tab=不用说了。<br>
win+tab若开3D效果了切换<br>
ctrl+alt+backspace=相当于强制注销<br>
ctrl+alt+del=调出关机菜单<br>
ctrl+alt+l=锁定桌面<br>
<br>
ctrl+alt+d=最小化gnome所有窗口<br>
ctrl+alt+f2=linux<span href="http://www.linuxdiyf.com/bbs/tag.php?name=%D6%D5%B6%CB" onclick="tagshow(event)" class="t_tag">终端</span>用户（alt + f7返回x<span href="http://www.linuxdiyf.com/bbs/tag.php?name=windows" onclick="tagshow(event)" class="t_tag">windows</span>，alt+ &lt;- 或-&gt; 进行<span href="http://www.linuxdiyf.com/bbs/tag.php?name=%D6%D5%B6%CB" onclick="tagshow(event)" class="t_tag">终端</span>切换）<br>
ctrl+alt+ &lt;- 或-&gt; =切换桌面<br><br><img src ="http://www.cppblog.com/dawnbreak/aggbug/68119.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dawnbreak/" target="_blank">pear_li</a> 2008-11-29 01:21 <a href="http://www.cppblog.com/dawnbreak/articles/68119.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> ubuntu解压命令全览</title><link>http://www.cppblog.com/dawnbreak/articles/68118.html</link><dc:creator>pear_li</dc:creator><author>pear_li</author><pubDate>Fri, 28 Nov 2008 17:11:00 GMT</pubDate><guid>http://www.cppblog.com/dawnbreak/articles/68118.html</guid><wfw:comment>http://www.cppblog.com/dawnbreak/comments/68118.html</wfw:comment><comments>http://www.cppblog.com/dawnbreak/articles/68118.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/dawnbreak/comments/commentRss/68118.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dawnbreak/services/trackbacks/68118.html</trackback:ping><description><![CDATA[<div id="blog_text" class="cnt">
<div class="cnt">
<h1>&nbsp;<a  href="http://blog.csdn.net/lg_Mp5/archive/2006/12/14/1443280.aspx">ubuntu下使用rar</a><cite class="fav_csdnstylebykimi"></cite></h1>
<p>1  用ubuntu自带的新立得软件包安装ark软件，搜索ark，然后选中就行了，会自动安装附带的10多个库文件。</p>
<p>2 安装完ark，会在菜单的附件中找到，你会发现ark其实已经设计成为支持rar，因为在新建打开等操作中都可以选择rar格式，不过只是写了出来，不是真正的支持。接下来主角要登场了。</p>
<p>3 到这里<a  href="http://www.rarlab.com/download.htm">RARLAB</a>，你可以下载rar for linux的文件，当然不是用rar格式压缩的，用默认打开的归档管理器解压缩，将其中的文件找一个路径存放。</p>
<p>4&nbsp; 将刚才解压缩的rar for linux的文件中的四个可执行文件：default.sfx，rar，rar_static， unrar，复制到 /usr/local/bin 目录下。你可能需要管理员帐号。如果在终端下操作，可以简单的sudo mv一下。</p>
<br><strong><font size="2"><font color="#0000ff"><br>.tar</font></font></strong><br>
解包：tar xvf FileName.tar<br>
打包：tar cvf FileName.tar DirName<br>
<font color="#008000">（注：tar是打包，不是压缩！）</font><br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.gz</font></font></strong><br>
解压1：gunzip FileName.gz<br>
解压2：gzip -d FileName.gz<br>
压缩：gzip FileName<br>
<strong><font size="2"><font color="#0000ff">.tar.gz 和 .tgz</font></font></strong><br>
解压：tar zxvf FileName.tar.gz<br>
压缩：tar zcvf FileName.tar.gz DirName<br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.bz2</font></font></strong><br>
解压1：bzip2 -d FileName.bz2<br>
解压2：bunzip2 FileName.bz2<br>
压缩： bzip2 -z FileName<br>
<strong><font size="2"><font color="#0000ff">.tar.bz2</font></font></strong><br>
解压：tar jxvf FileName.tar.bz2<br>
压缩：tar jcvf FileName.tar.bz2 DirName<br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.bz</font></font></strong><br>
解压1：bzip2 -d FileName.bz<br>
解压2：bunzip2 FileName.bz<br>
压缩：<font color="#ffa500">未知</font><br>
<strong><font size="2"><font color="#0000ff">.tar.bz</font></font></strong><br>
解压：tar jxvf FileName.tar.bz<br>
压缩：<font color="#ffa500">未知</font><br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.Z</font></font></strong><br>
解压：uncompress FileName.Z<br>
压缩：compress FileName<br>
<strong><font size="2"><font color="#0000ff">.tar.Z</font></font></strong><br>
解压：tar Zxvf FileName.tar.Z<br>
压缩：tar Zcvf FileName.tar.Z DirName<br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.zip</font></font></strong><br>
解压：unzip FileName.zip<br>
压缩：zip FileName.zip DirName<br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.rar</font></font></strong><br>
解压：rar x FileName.rar<br>
压缩：rar a FileName.rar DirName<br>
<font color="#008000"><br>
rar请到：<a href="http://www.rarsoft.com/download.htm" style="color: #003793;" target="_blank"><font color="#003793">http://www.rarsoft.com/download.htm</font></a> 下载！<br>
解压后请将rar_static拷贝到/usr/bin目录（其他由$PATH环境变量指定的目录也可以）：<br>
[root@www2 tmp]# cp rar_static /usr/bin/rar</font><br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.lha</font></font></strong><br>
解压：lha -e FileName.lha<br>
压缩：lha -a FileName.lha FileName<br>
<font color="#008000"><br>
lha请到：<a href="http://www.infor.kanazawa-it.ac.jp/%7Eishii/lhaunix/" style="color: #003793;" target="_blank"><font color="#003793">http://www.infor.kanazawa-it.ac.jp/~ishii/lhaunix/</font></a>下载！<br>
&gt;解压后请将lha拷贝到/usr/bin目录（其他由$PATH环境变量指定的目录也可以）：<br>
[root@www2 tmp]# cp lha /usr/bin/</font><br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.rpm</font></font></strong><br>
解包：rpm2cpio FileName.rpm | cpio -div<br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.deb</font></font></strong><br>
解包：ar p FileName.deb data.tar.gz | tar zxf -<br>
---------------------------------------------<br>
<strong><font size="2"><font color="#0000ff">.tar .tgz .tar.gz .tar.Z
.tar.bz .tar.bz2 .zip .cpio .rpm .deb .slp .arj .rar .ace .lha .lzh
.lzx .lzs .arc .sda .sfx .lnx .zoo .cab .kar .cpt .pit .sit .sea</font></font></strong><br>
解压：sEx x FileName.*<br>
压缩：sEx a FileName.* FileName<br>
<font color="#008000"><br>
sEx只是调用相关程序，本身并无压缩、解压功能，请注意！<br>
sEx请到： <a href="http://sourceforge.net/projects/sex" style="color: #003793;" target="_blank"><font color="#003793">http://sourceforge.net/projects/sex</font></a>下载！<br>
解压后请将sEx拷贝到/usr/bin目录（其他由$PATH环境变量指定的目录也可以）：<br>
[root@www2 tmp]# cp sEx /usr/bin/</font></div>
</div><img src ="http://www.cppblog.com/dawnbreak/aggbug/68118.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dawnbreak/" target="_blank">pear_li</a> 2008-11-29 01:11 <a href="http://www.cppblog.com/dawnbreak/articles/68118.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>