| Libin's profile绿色家园PhotosBlogLists | Help |
|
September 23 Chromium 快捷键列表窗口和标签页快捷键
地址栏快捷键在地址栏,进行下列操作之一:
打开谷歌浏览器各功能的快捷键
网页快捷键
文字快捷键
查看更多快捷键
July 17 将linux-2.6.4内核移植到S3C2410摘自:http://www.cublog.cn/u2/70445/showart_1721091.html
1. 构建起环境 要使用内核,首先要编译内核。如果不选择合适的内核和编译器,就会出现错误。典型的就是如下的错误: EG: CC arch/arm/kernel/asm-offsets.s cc1: error : invalid option 'apcs' cc1: error : invalid option 'no-sched-prolog' cc1: error : invalid option 'little-endian' cc1: error : invalid option 'abi=apcs-gnu' arch/arm/kernel/asm-offsets.c:1: error: bad value (armv4) for -march=switch arch/arm/kernel/asm-offsets.c:1: error: bad value (armv4) for -mtune=switch make[1]: *** [arch/arm/kernel/asm-offsets.s] Error 1 make: *** [arch/arm/kernel/asm-offsets.s] Error 2 re:asm-offsets.s 这个文件编译出错一般是编译器问题,不要用2.95.3,用3.4.1 这里主要是编译器版本的问题,2.6内核不能使用以往的2.95.3版本的gcc,需改用3.4.1版本。关于编译器,最好不要自己做,下载网上现成的就可以了。http://www.gnuarm.com/这里有arm的编译器下载。 内核的版本对于编译倒关系不大,一般都能编译成功,而关键还在于配置。为了对s3c2410有更好的支持,建议使用2.6.10以后的版本。网上2.6.11版本用的人也挺多的,相关资料也挺多的,但是新版本对硬件的支持更好更稳定。比如在2.6.11中没有DM9000和CS8900的网卡驱动,在2.6.14版本中就已经对其支持。我选用的是2.6.14版本,现使用三星默认的配置文件arch/arm/configs/s3c2410_defconfig,顺利编译通过。 kernel: http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.11.7.tar.bz2 compiler: ftp://ftp.handhelds.org/projects/toolchain/arm-linux-gcc-3.4.1.tar.bz2
2. 运行内核,显示调试信息 运行内核肯定没问题,但是搞不好你就出现了一下问题: Uncompressing Linux................................................................ done, booting the kernel. 就不动了,以前用2.4.18内核是可以启动的。 re:命令行的console参数错了,应该为console=ttySAC0,不是console=ttyS0。 因为2.6对2410的串口支持已经很好了,使用默认配置的话就不要去怀疑串口驱动了。问题出在命令行上,有人说我将默认的命令行改成了console=ttySAC0也还是不行。这只能说明改动了默认的命令行,只有在bootloader没有传递命令行参数给内核的时候才起作用。如果你的bootloader启动过2.4的内核,命令行参数肯定是不对的,在bootloader中将命令行改了就行了。例如:“console=ttySAC0,115200 root=/dev/ram init=/linuxrc rw initrd=0x30008000,0x320000 ”。 因为烧写bootlader麻烦,时间长,调试阶段不建议修改bootloader。我将内核中的arch/arm/kernel/setup.c文件中的parse_tag_cmdline()函数中的内容注释掉,并且配置正确的CONFIG_CMDLINE参数,即可运行。以后每当改变内核参数只要改变CONFIG_CMDLINE就可以了。(CONFIG_CMDLINE这个值可以在make menuconfig中配置,2.6.11版本和2.6.14版本配置位置有所不同,请注意)。
3. 激动的看到shell 当做完以上工作时看到内核信息哗哗哗的出来已经很兴奋了吧,我也是,但是很快你会看到下面有如下错误: Please append a correct "root=" boot option Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(1,0) re:传递一个合理的root参数,如果已经传递了,请确认内核是否支持 如果使用“console=ttySAC0,115200 root=/dev/ram init=/linuxrc rw initrd=0x30008000,0x320000”这个命令行参数,需要使用ramdisk和initrd。所以确保CONFIG_RAMDISK=y和CONFIG_INITRD=y。在“device driver”=》“Block device”下面可以配置。 这里的ramdisk是把内存块当做一个块设备来使用,相当于一个位于内存中的“磁盘分区”;而initrd就是用于初始化系统的ramdisk,用来mount真正的root。 这里使用initrd是为了调试方便,不需要使用flash和网络。一般使用是多数会用到mtd,调试时使用nfsroot,在移植完相关硬件后再来讨论。 当内核支持了以后还需要有initrd。使用网上下载的现成的,如何制作等会介绍。例如:我有一个initrd为ramdisk.image.gz,在bootloader下加载到指定的0x30800000处,然后启动内核即可。一般出现如下信息表示ramdisk加载成功了。 RAMDISK: Compressed image found at block 0 VFS: Mounted root (ext2 filesystem) readonly. Freeing init memory: 96K Warning: unable to open an initial console. re:unable to open an initial console是因为没有支持devfs。 但是又出现了“Warning: unable to open an initial console.”这个错误。难道是console的问题?no。有些initrd是用于又devfs支持的内核,所以在/dev/目录下没有设备文件。两种办法解决这个问题:1.copy设备节点;2.让内核支持devfs。两个方法没有什么好与差,只是在2.6.14版本中devfs的配置选项在make menuconfig的时候会看不到。linux除去了devfs的配置,提供了替代品udev,但是对这个不熟悉,而且听说也不稳定,所以还是打算用devfs。可以在.config文件中直接添加CONFIG_DEVFS_FS,但由于每次make menuconfig之后会重新写.config文件,每次编译要添加一次,很麻烦,所以不建议。我是把2.6.11版本中的/fs/Kconfig复制过来替换掉。这样配置的时候就能看到devfs的选项了。配置时注意还要选中“Automatical mount at boot”选项。 Freeing init memory: 80K Warning: unable to open an initial console. Kernel panic - not syncing: No init found. Try passing init= option to kernel. re:init参数没有传递,或者制作的ramdisk有问题。 XXX:目前这个地方我也没弄好。正疑惑。用别人的initrd可以,自己用busybox做的就不行。而且用别人的也不是很稳定。有可能时busybox在编译完了后没有copy相关库,需要静态编译。
当解决了以上问题之后,多数能兴奋的看到shell了。
4. 制作自己的initrd 这个initrd的制作可以参考网上一些制作floppy disk的文章。 4.1 制作一个ext2的文件系统映象 ~# dd if=/dev/zero of=ramdisk.image bs=1M count=2 ~# echo y | mke2fs -i 2000 -m 0 ramdisk.image ~# mkdir mnt ~# mount -o loop ramdisk.image mnt 这样,ramdisk.image这个ext2文件系统映象就做完了,他相当于一个磁盘分区。
5.显示自定义logo 在配置内核的时候选中了启动Logo的支持。 使用下面的方法可以将企鹅的Logo换成自己喜欢的任意图片。 首先准备一幅自己喜欢的图片,然后将背景涂成黑色。然后将该图片保存成png格式,例如linuxlogo.png。在Linux下使用下面的命令:
# pngtopnm linuxlogo.png > linuxlogo.pnm # pnmquant 224 linuxlogo.pnm > linuxlogo224.pnm # pnmtoplainpnm linuxlogo224.pnm > linuxlogo224.ppm
然后.ppm替换/usr/src/linux-2.6.8.1/drivers/后用生成的linuxlogo224video/logo/logo_linux_clut224.ppm(最好先做好备份),然后删除同一目录下的logo_linux_clut224.c文件,重新编译内核,启动之后就可以在屏幕左上方看到自己的Logo了。 May 30 介绍qmakeqmake是用来为不同的平台的开发项目创建makefile的Trolltech开发一个易于使用的工具。qmake简化了makefile的生成,所以为了创建一个makefile只需要一个只有几行信息的文件。qmake可以供任何一个软件项目使用,而不用管它是不是用Qt写的,尽管它包含了为支持Qt开发所拥有的额外的特征。 qmake基于一个项目文件这样的信息来生成makefile。项目文件可以由开发者生成。项目文件通常很简单,但是如果需要它是非常完善的。不用修改项目文件,qmake也可以为为Microsoft Visual Studio生成项目。 qmake的概念QMAKESPEC环境变量举例来说,如果你在Windows下使用Microsoft Visual Studio,然后你需要把QMAKESPEC环境变量设置为win32-msvc。如果你在Solaris上使用gcc,你需要把QMAKESPEC环境变量设置为solaris-g++。 在qt/mkspecs中的每一个目录里面,都有一个包含了平台和编译器特定信息的qmake.conf文件。这些设置适用于你要使用qmake的任何项目,请不要修改它,除非你是一个专家。例如,假如你所有的应用程序都必须和一个特定的库连接,你可以把这个信息添加到相应的qmake.conf文件中。 项目(.pro)文件一个项目文件是用来告诉qmake关于为这个应用程序创建makefile所需要的细节。例如,一个源文件和头文件的列表、任何应用程序特定配置、例如一个必需要连接的额外库、或者一个额外的包含路径,都应该放到项目文件中。 “#”注释你可以为项目文件添加注释。注释由“#”符号开始,一直到这一行的结束。 模板模板变量告诉qmake为这个应用程序生成哪种makefile。下面是可供使用的选择:
“app”模板“app”模板告诉qmake为建立一个应用程序生成一个makefile。当使用这个模板时,下面这些qmake系统变量是被承认的。你应该在你的.pro文件中使用它们来为你的应用程序指定特定信息。
你只需要使用那些你已经有值的系统变量,例如,如果你不需要任何额外的INCLUDEPATH,那么你就不需要指定它,qmake会为所需的提供默认值。例如,一个实例项目文件也许就像这样: TEMPLATE = app 如果条目是单值的,比如template或者目的目录,我们是用“=”,但如果是多值条目,我们使用“+=”来为这个类型添加现有的条目。使用“=”会用新值替换原有的值,例如,如果我们写了DEFINES=QT_DLL,其它所有的定义都将被删除。 “lib”模板“lib”模板告诉qmake为建立一个库而生成makefile。当使用这个模板时,除了“app”模板中提到系统变量,还有一个VERSION是被支持的。你需要在为库指定特定信息的.pro文件中使用它们。
“subdirs”模板“subdirs”模板告诉qmake生成一个makefile,它可以进入到特定子目录并为这个目录中的项目文件生成makefile并且为它调用make。 在这个模板中只有一个系统变量SUBDIRS可以被识别。这个变量中包含了所要处理的含有项目文件的子目录的列表。这个项目文件的名称是和子目录同名的,这样qmake就可以发现它。例如,如果子目里是“myapp”,那么在这个目录中的项目文件应该被叫做myapp.pro。 CONFIG变量配置变量指定了编译器所要使用的选项和所需要被连接的库。配置变量中可以添加任何东西,但只有下面这些选项可以被qmake识别。 下面这些选项控制着使用哪些编译器标志:
下面这些选项定义了所要连编的库/应用程序的类型:
例如,如果你的应用程序使用Qt库,并且你想把它连编为一个可调试的多线程的应用程序,你的项目文件应该会有下面这行: CONFIG += qt thread debug 注意,你必须使用“+=”,不要使用“=”,否则qmake就不能正确使用连编Qt的设置了,比如没法获得所编译的Qt库的类型了。 May 29 编译meshlab 1.21全接触1. 编译环境: a. visual studio 2008 perfessional edition,因为已经安装了sp1,所以不确定它会对编译有何影响。 b. QT opensource v4.5。0,只编译了其动态库文件(静态库无法编译完全)。 执行“Visual Studio 2008 命令提示 ”控制台工具后,在QT根目录执行QT编译环境设置脚本,该脚本同时为meshlab生成vc 项目工程的环境。一下为该脚本原文: @echo off set cur_dir=%cd%\ set QTDIR=%cur_dir% set QMAKESPEC=win32-msvc2008 set ConfPara=-debug-and-release -opensource -fast -no-dbus -no-webkit
set PATH=%QTDIR%/bin;%PATH% set INCLUDE=%MINGWDIR%/include;%QTDIR%/include;%QWTDIR%/src;%LOG4QTDIR%/src;%INCLUDE% set LIB=%MINGWDIR%/lib;%QTDIR%/lib;%QWTDIR%/lib;%LIB%
echo *********************************************************************** echo Created By gmail:bygreencn.gmail.com echo Includes : QT 4.5.0、Visual Studio 2008 echo QT : %QTDIR% echo QMAKESPEC: %QMAKESPEC% echo ConfPara: %ConfPara% echo *********************************************************************** @REM pause @REM nmake clean @REM nmake confclean @REM configure.exe %ConfPara%
@REM pause @REM echo build it now? @REM nmake clean cmd /k
c.下载MeshLab's source code version 1.2.1,我下载的是All Inclusive package 2. 生成所需的VC项目工程文件 a. 上一步的控制台,进入.\ meshlab\src\external,执行qmake -tp vc -recursive external.pro b. 上一步的控制台,进入.\ meshlab\src,执行qmake -tp vc -recursive meshlabv12.pro 3. 编译meshlab a. 首先编译external library。用vc打开.\ meshlab\src\external\external.sln,进入配置管理器,选择编译debug或release版本,在选择生成解决方案,等待编译全部通过,它会生成三个库文件。bz.lib,3ds.lib和muparser.lib,我发现3ds.lib的生成有些问题,会使得meshlab在LINK时无法正确连接函数,我在lib3ds\type.h做了一下修改: //#ifdef _MSC_VER //#ifdef LIB3DS_EXPORTS //#define LIB3DSAPI __declspec(dllexport) //#else //#define LIB3DSAPI __declspec(dllimport) //#endif //#else #define LIB3DSAPI //#endif b.编译meshlab:meshlab所有的plugin工程的设置有些问题,这些工程都制定输出为动态库,但是输出文件却指定为输出为*****.lib,这导致所有的plugins的工程都失败。我的做法是把指定输出为*****.dll,这里应该必须为dll,因为New的对话框中会根据plugins文件夹下的内容动态生成,因为这些应该也是动态加载的,因为静态库是不行的了。 1). 需要为io_lib指定其需要的lib3ds.lib(external library) 2). 需要为io_epoch指定其需要的bz2.lib和头文件的位置(external library) 如果哪个plugin工程出现类似这个错误: 1>Project : error PRJ0019: 某个工具从以下位置返回了错误代码: "MOC v3dImportDialog.h" 1>项目: warning PRJ0018 : 未找到下列环境变量: 1>$(QTDIR) 只需要用生成VC项目的那个控制台环境进入相应的plugin目录,执行qmake -tp vc -recursive xxxxxx.pro来重新生成该工程即可。
编译整个项目,大概需要十多分钟。然后就可以进入.\meshlab\src\meshlab\debug或者.\meshlab\src\meshlab\release执行meshlab.exe;记得要把QT的DLL库文件拷贝到这个目录或者将QT的DLL库所在目录加入到系统PATH中啊。
May 13 破解无线路由器密码Posted by chinahang 2009年5月13日
随着社会的进步!WIFI上网日益普及,特别是大城市中随便在一个小区搜索一下就能找到好多热点,搜索到热点然后链接上去那么我们就可以尽情的享受免费上网服务了。 破解静态WEP KEY全过程
发现 首先通过NetStumbler确认客户端已在某AP的覆盖区内,并通过AP信号的参数进行‘踩点’(数据搜集)。 通 过上图的红色框框部分内容确定该SSID名为demonalex的AP为802.11b类型设 备,Encryption属性为‘已加密’,根据802.11b所支持的算法标准,该算法确定为WEP。有一点需要注意:NetStumbler对任何有 使用加密算法的STA[802.11无线站点]都会在Encryption属性上标识为WEP算法,如上图中SSID为gzpia的AP使用的加密算法是 WPA2-AES。 破解下载Win32版AirCrack程序集---WinAirCrackPack工具包(下载地址:http://www.demonalex.net/download/wireless/aircrack/WinAircrackPack.zip)。解压缩后得到一个大概4MB的目录,其中包括六个EXE文件: aircrack.exe 原WIN32版aircrack程序 airdecap.exe WEP/WPA解码程序 airodump.exe 数据帧捕捉程序 Updater.exe WIN32版aircrack的升级程序 WinAircrack.exe WIN32版aircrack图形前端 wzcook.exe 本地无线网卡缓存中的WEPKEY记录程序
我们本次实验的目的是通过捕捉适当的数据帧进行IV(初始化向量)暴力破解得到WEP KEY,因此只需要使用airodump.exe(捕捉数据帧用)与WinAircrack.exe(破解WEP KEY用)两个程序就可以了。 首先打开ariodump.exe程序,按照下述操作:
选
择‘Key size’为64(目前大多数用户都是使用这个长度的WEP
KEY,因此这一步骤完全是靠猜测选定该值),最后单击主界面右下方的‘Aircrack the
key…’按钮,此时将弹出一个内嵌在cmd.exe下运行的进程对话框,并在提示得出WEP KEY: 利用打开无线网卡的连接参数设置窗口,设置参数为: SSID:demonalex 频道:6 WEP KEY:1111122222(64位) OK,现在可以享受连入别人WLAN的乐趣了。 April 28 让VS 2008支持Subversion插件AnkhsvnVisual
Studio 2005 有一个开源的Subversion插件,Ankhsvn
(http://ankhsvn.tigris.org/),安装后,VS
2005中将内置Subversion的支持,可以直接在VS里面提交修改。我经常用它和TortoiseSVN
配合来使用Subversion,十分方便。
再次打开Visual Studio 2008后,就会发现Ankhsvn已经集成进系统了。 Dim shell, filename, fso, file, content Set shell = CreateObject("wscript.shell") Set fso = CreateObject("Scripting.FileSystemObject") filename = "ankh.reg"![]() shell.run "reg export HKLMSOFTWAREMicrosoftVisualStudio8.0AddinsAnkh " & filename, 1, True![]() Set file = fso.OpenTextFile(filename, 1, False, True) content = file.ReadAll content = Replace(content, "VisualStudio8.0", "VisualStudio9.0") content = Replace(content, ".NET 2005", ".NET 2008") file.Close()![]() Set file = fso.OpenTextFile(filename, 2, True) file.Write content file.Close()![]() shell.run "reg import " & filename, 1, true![]() fso.DeleteFile filenameApril 16 Gpredict is a real-time satellite tracking and orbit prediction applicationGpredict ScreenshotsBelow you can see some sample screenshots of gpredict in in action. They illustrate the main features of gpredict. I have a whole album dedicated to screenshots of Gpredict in my Media Gallery. You can also post links to your own screenshots and pictures of Gpredict in action on the forum. Main Window, Modules Layouts, and ViewsFuture Pass PredictionsRadio and Antenna Rotator ControlPreferences and SettingsApril 14 用VC6.0编译boost 1.38.01. 从boost.org下载下1.38.0的源码 2.编译jam 在boost_1_38_0\tools\jam\src下有个build.bat ,修改其ProgramFiles变量为VC安装的目录。 执行build.bat msvc,等编译成功,会在\boost_1_38_0\tools\jam\src\bin.ntx86下生成bjam.exe 3.将bjam拷贝到boost_1_38_0\bin,将目录增加环境变量PATH。 4.编译整个boost 在根目录boost_1_38_0,执行bjam --toolset=msvc-6.0
另外: 5. 如果编译boost里面单独的模块,如regex 进入boost_1_38_0\libs\regex\build 执行bjam bjam --toolset=msvc-6.0 就会在boost_1_38_0\bin.v2\libs\regex\build\msvc-6.0\debug\threading-multi下面生成boost_regex-vc6-mt-gd-1_38_0.dll和boost_regex-vc6-mt-gd-1_38_0.lib库,可以给VC6.0使用。 March 23 Git 中文教程介绍Git --- The stupid content tracker, 傻瓜内容跟踪器。Linus 是这样给我们介绍 Git 的。 Git 是用于 Linux 内核开发的版本控制工具。与常用的版本控制工具 CVS, Subversion 等不同, 它采用了分布式版本库的方式,不必服务器端软件支持,使源代码的发布和交流极其方便。 Git 的速度很快,这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git 最为出色的是它的合并跟踪(merge tracing)能力。 实际上内核开发团队决定开始开发和使用 Git 来作为内核开发的版本控制系统的时候, 世界开源社群的反对声音不少,最大的理由是 Git 太艰涩难懂,从 Git 的内部工作机制来说,的确是这样。 但是随着开发的深入,Git 的正常使用都由一些友好的脚本命令来执行,使 Git 变得非常好用, 即使是用来管理我们自己的开发项目,Git 都是一个友好,有力的工具。 现在,越来越多的著名项目采用 Git 来管理项目开发,例如:wine, U-boot 等,详情看 http://www.kernel.org/git 作为开源自由原教旨主义项目,Git 没有对版本库的浏览和修改做任何的权限限制。 它只适用于 Linux / Unix 平台,没有 Windows 版本,目前也没有这样的开发计划。 本文将以 Git 官方文档 Tutorial, core-tutorial 和 Everyday GIT 作为蓝本翻译整理,但是暂时去掉了对 Git 内部工作机制的阐述, 力求简明扼要,并加入了作者使用 Git 的过程中的一些心得体会,注意事项,以及更多的例子。 建议你最好通过你所使用的 Unix / Linux 发行版的安装包来安装 Git, 你可以在线浏览本文 ,也可以通过下面的命令来得到本文最新的版本库,并且通过后面的学习用 Git 作为工具参加到本文的创作中来。 $ git-clone http://www.bitsun.com/git/gittutorcn.git 创建一个版本库:git-init-db创建一个 Git 版本库是很容易的,只要用命令 git-init-db 就可以了。 现在我们来为本文的写作创建一个版本库: $ mkdir gittutorcn git 将会作出以下的回应 defaulting to local storage area 这样,一个空的版本库就创建好了,并在当前目录中创建一个叫 .git 的子目录。 你可以用 ls -a 查看一下,并请注意其中的三项内容:
具体地说,子目录 refs 包含着两个子目录叫 heads 和 tags, 就像他们的名字所表达的意味一样:他们存放了不同的开发分支的头的索引, 或者是你用来标定版本的标签的索引。 请注意:master 是默认的分支,这也是为什么 .git/HEAD 创建的时候就指向 master 的原因,尽管目前它其实并不存在。 git 将假设你会在 master 上开始并展开你以后的工作,除非你自己创建你自己的分支。 另外,这只是一个约定俗成的习惯而已,实际上你可以将你的工作分支叫任何名字, 而不必在版本库中一定要有一个叫 master 的分支,尽管很多 git 工具都认为 master 分支是存在的。 现在已经创建好了一个 git 版本库,但是它是空的,还不能做任何事情,下一步就是怎么向版本库植入数据了。 植入内容跟踪信息:git-add为了简明起见,我们创建两个文件作为练习: $ echo "Hello world" > hello 我们再用 git-add 命令将这两个文件加入到版本库文件索引当中: $ git-add hello example git-add 实际上是个脚本命令,它是对 git 内核命令 git-update-index 的调用。 因此上面的命令和下面的命令其实是等价的: $ git-update-index --add hello example 如果你要将某个文件从 git 的目录跟踪系统中清除出去,同样可以用 git-update-index 命令。例如: $ git-update-index --force-remove foo.c
应该建立一个清晰的概念就是,git-add 和 git-update-index 只是刷新了 git 的跟踪信息,hello 和 example 这两个文件中的内容并没有提交到 git 的内容跟踪范畴之内。 提交内容到版本库:git-commit既然我们刷新了 Git 的跟踪信息,现在我们看看版本库的状态: $ git-status 我们能看到 git 的状态提示: # 提示信息告诉我们版本库中加入了两个新的文件,并且 git 提示我们提交这些文件, 我们可以通过 git-commit 命令来提交: $ git-commit -m "Initial commit of gittutor reposistory" 查看当前的工作:git-diffgit-diff 命令将比较当前的工作目录和版本库数据库中的差异。 现在我们编辑一些文件来体验一下 git 的跟踪功能。 $ echo "It's a new day for git" >> hello 我们再来比较一下,当前的工作目录和版本库中的数据的差别。 $ git-diff 差异将以典型的 patch 方式表示出来: diff --git a/hello b/hello 此时,我们可以再次使用组合命令 git-update-index 和 git-commit 将我们的工作提交到版本库中。 $ git-update-index hello 实际上,如果要提交的文件都是已经纳入 git 版本库的文件,那么不必为这些文件都应用 git-update-index 命令之后再进行提交,下面的命令更简捷并且和上面的命令是等价的。 $ git-commit -a -m "new day for git" 管理分支:git-branch直至现在为止,我们的项目版本库一直都是只有一个分支 master。 在 git 版本库中创建分支的成本几乎为零,所以,不必吝啬多创建几个分支。 下面列举一些常见的分支策略,仅供大家参考:
创建分支下面的命令将创建我自己的工作分支,名叫 robin,并且将以后的工作转移到这个分支上开展。 $ git-branch robin 删除分支要删除版本库中的某个分支,使用 git-branch -D 命令就可以了,例如: $ git-branch -D branch-name 查看分支运行下面的命令可以得到你当前工作目录的分支列表: $ git-branch 如果你忘记了你现在工作在哪个分支上,运行下面的命令可以告诉你: $ cat .git/HEAD 查看项目的发展变化和比较差异这一节介绍几个查看项目的版本库的发展变化以及比较差异的很有用的命令: git-show-branchgit-diffgit-whatchanged我们现在为 robin, master 两个分支都增加一些内容。 $ git-checkout robin $ git-checkout master git-show-branch 命令可以使我们看到版本库中每个分支的世系发展状态, 并且可以看到每次提交的内容是否已进入每个分支。 $ git-show-branch 这个命令让我们看到版本库的发展记录。 * [master] Some fun 譬如我们要查看世系标号为 master^ 和 robin 的版本的差异情况, 我们可以使用这样的命令: $ git-diff master^ robin 我们可以看到这两个版本的差异: diff --git a/hello b/hello
我们现在再用 git-whatchanged 命令来看看 master 分支是怎么发展的。 $ git-checkout master diff-tree 1d2fa05... (from 3ecebc0...) 从上面的内容中我们可以看到,在 robin 分支中的日志为 "Some work" 的内容, 并没有在 master 分支中出现。 合并两个分支:git-merge既然我们为项目创建了不同的分支, 那么我们就要经常地将自己或者是别人在一个分支上的工作合并到其他的分支上去。 现在我们看看怎么将 robin 分支上的工作合并到 master 分支中。 现在转移我们当前的工作分支到 master,并且将 robin 分支上的工作合并进来。 $ git-checkout master $ git-checkout master 但是,此时 git 会出现合并冲突提示: Trying really trivial in-index merge... git 的提示指出,在合并作用于文件 hello 的 'Some fun' 和 'some work' 这两个对象时有冲突, 具体通俗点说,就是在 master, robin 这两个分支中的 hello 文件的某些相同的行中的内容不一样。 我们需要手动解决这些冲突,现在先让我们看看现在的 hello 文件中的内容。 $ cat hello 此时的 hello 文件应是这样的,用过其他的版本控制系统的朋友应该很容易看出这个典型的冲突表示格式: Hello World 我们用编辑器将 hello 文件改为: Hello World 现在可以将手动解决了冲突的文件提交了。 $ git-commit -i hello 以上是典型的两路合并(2-way merge)算法,绝大多数情况下已经够用。 但是还有更复杂的三路合并和多内容树合并的情况。详情可参看: git-read-tree, git-merge 等文档。 逆转与恢复:git-reset
项目跟踪工具的一个重要任务之一,就是使我们能够随时逆转(Undo)和恢复(Redo)某一阶段的工作。
git-reset 命令就是为这样的任务准备的。 它将当前的工作分支的 头 定位到以前提交的任何版本中,它有三个重置的算法选项。 命令形式:git-reset [--mixed | --soft | --hard] [<commit-ish>] 命令的选项:
一个重要技巧--逆转提交与恢复可能有人会问,--soft 选项既不重置头索引的位置,也不改变工作树中的内容, 那么它有什么用呢?现在我们介绍一个 --soft 选项的使用技巧。 下面我们用例子来说明: $ git-checkout master 这里我们创建了一个 master 的拷贝分支 softreset, 现在我们可以看到两个分支是在同一起跑线上的。 ! [master] Merge branch 'robin' 我们为 文件增加一些内容并提交。 $ echo "Botch, botch, botch" >> hello 我们可以看到此时 softreset 比 master 推进了一个版本 "some botch" 。 ! [master] Merge branch 'robin' 现在让我们来考虑这样的一种情况,假如我们现在对刚刚提交的内容不满意, 那么我们再编辑项目的内容,再提交的话,那么 "some botch" 的内容就会留在版本库中了。 我们当然不希望将有明显问题的内容留在版本库中,这个时候 --soft 选项就很有用了。 为了深入了解 --soft 的机制,我们看看现在 softreset 分支的头和 ORIG_HEAD 保存的索引。 $ cat .git/refs/heads/softreset .git/ORIG_HEAD 结果如下: 5e7cf906233e052bdca8c598cad2cb5478f9540a 现在用 --soft 选项逆转刚才提交的内容: git-reset --soft HEAD^ 现在让我们再看看 .git/ORIG_HEAD 的中保存了什么? $ cat .git/ORIG_HEAD 结果如下: 5e7cf906233e052bdca8c598cad2cb5478f9540a 看!现在的 .git/ORIG_HEAD 等于逆转前的 .git/refs/heads/softreset 。 也就是说,git-reset --soft HEAD^ 命令逆转了刚才提交的版本进度, 但是它将那次提交的对象的索引拷贝到了 .git/ORIG_HEAD 中。 我们再编辑 hello 文件成为下面的内容: Hello World 我们甚至可以比较一下现在的工作树中的内容和被取消了的那次提交的内容有什么差异: $ git-diff ORIG_HEAD 结果如下: diff --git a/hello b/hello 接着,我们可以恢复刚才被取消了的那次提交了。 $ git-commit -a -c ORIG_HEAD 注意,这个命令会打开默认的文本编辑器以编辑原来提交的版本日志信息,我们改为 "nice work" 。 大家可以自行用 git-show-branch 命令来查看一下现在的分支状态。 并且我们还可以不断地重复上述的步骤,一直修改到你对这个版本进度满意为止。 git-reset 命令还有很多的用途和技巧,请参考 git-reset ,以及 Everyday GIT with 20 commands or So 。 提取版本库中的数据这是个很有用的小技巧,如果你对你现在的工作目录下的东西已经不耐烦了, 随时可以取出你提交过的东西覆盖掉当前的文件,譬如: $ git-checkout -f foo.c 标定版本在 git 中,有两种类型的标签,“轻标签”和“署名标签”。 技术上说,一个“轻标签”和一个分支没有任何区别,只不过我们将它放在了 .git/refs/tags/ 目录, 而不是 heads 目录。因此,打一个“轻标签”再简单不过了。 $ git-tag my-first-tag 如果你打算针对某个commit ID来打标签,虽然该命令可以通过gitk里的右键菜单来实现,但是该命令对实际应用是很有帮助的。 $ git-tag mytag f0af6283824688f9d23426031734657661b54388 “署名标签”是一个真正的 git 对象,它不但包含指向你想标记的状态的指针,还有一个标记名和信息, 可选的 PGP 签名。你可以通过 -a 或者是 -s 选项来创建“署名标签”。 $ git-tag -s <tag-name> 合并外部工作通常的情况下,合并其他的人的工作的情况会比合并自己的分支的情况要多, 这在 git 中是非常容易的事情,和你运行 git-merge 命令没有什么区别。 事实上,远程合并的无非就是“抓取(fetch)一个远程的版本库中的工作到一个临时的标签中”, 然后再使用 git-merge 命令。 可以通过下面的命令来抓取远程版本库: $ git-fetch <remote-repository> 根据不同的远程版本库所使用的通讯协议的路径来替代上面的 remoted-repository 就可以了。
到这里可能有些朋友已经想到, 实际上,我们可以通过 Rsync, SSH 之类的双向传输方式来建立类似 CVS,SVN 这样的中心版本库模式的开发组织形式。 通过电子邮件交换工作读过上一节之后,有的朋友可能要问,如果版本库是通过单向的下载协议发布的,如 HTTP, 我们就无法将工作上传到公共的版本库中。别人也不能访问我的机器来抓取我的工作,那怎么办呢? 不必担心,我们还有 email !别忘了 git 本来就是为了管理 Linux 的内核开发而设计的。 所以,它非常适合像 Linux Kernel 这样的开发组织形式高度分散,严重依赖 email 来进行交流的项目。 下面模拟你参加到《Git 中文教程》的编写工作中来,看看我们可以怎么通过 email 进行工作交流。 你可以通过下面的命令下载这个项目的版本库。 $ git-clone http://www.bitsun.com/git/gittutorcn.git
你可以直接在 master 下开展工作,也可以创建你自己的工作分支。 当你对项目做了一定的工作,并提交到库中。我们用 git-show-branch 命令先看下库的状态。 * [master] your buddy's contribution 上面就假设你已经提交了一个叫 "your buddy's contribution" 的工作。 现在我们来看看怎么通过 email 来交流工作了。 $ git-fetch origin (1) 上面的几个命令,会在当前目录下生成一个大概名为 0001-your-buddy-s-contribution.txt 补丁文件, 建议你用文本工具查看一下这个文件的具体形式,然后将这个文件以附件的形式发送到项目维护者的邮箱: vortune@gmail.com 当项目的维护者收到你的邮件后,只需要用 git-am 命令,就可以将你的工作合并到项目中来。 $ git-checkout -b buddy-incomming 用 Git 协同工作假设 Alice 在一部机器上自己的个人目录中创建了一个项目 /home/alice/project, Bob 想在同一部机器自己的个人目录中为这个项目做点什么。 Bob 首先这样开始: $ git-clone /home/alice/project myrepo 这样就创建了一个保存着 Alice 的版本库的镜像的新目录 "myrepo"。 这个镜像保存着原始项目的起点和它的发展历程。 接着 Bob 对项目做了些更改并提交了这些更改: (编辑一些文件) 当他搞定之后,他告诉 Alice 将他的东西从 /home/bob/myrepo 中引入,她只需要这样: $ cd /home/alice/project 这样就将 Bob 的版本库中的 "master" 分支的变化引入了。 Alice 也可以通过在 pull 命令的后面加入参数的方式来引入其他的分支。 在导入了 Bob 的工作之后,用 "git-whatchanged" 命令可以查看有什么信的提交对象。 如果这段时间里以来,Alice 也对项目做过自己的修改,当 Bob 的修改被合并进来的时候, 那么她需要手动修复所有的合并冲突。 谨慎的 Alice 在导入 Bob 的工作之前,希望先检查一下。 那么她可以先将 Bob 的工作导入到一个新创建的临时分支中, 以方便研究 Bob 的工作: $ git fetch /home/bob/myrepo master:bob-incoming 这个命令将 Bob 的 master 分支的导入到名为 bob-incoming 的分支中( 不同于 git-pull 命令,git-fetch 命令只是取得 Bob 的开发工作的拷贝, 而不是合并经来)。接着: $ git whatchanged -p master..bob-incoming 这会列出 Bob 自取得 Alice 的 master 分支之后开始工作的所有变化。 检查过这些工作,并做过必须的调整之后, Alice 就可以将变化导入到她的 master 分支中: $ git-checkout master 最后的命令就是将 "bob-incoming" 分支的东西导入到 Alice 自己的版本库中的, 稍后,Bob 就可以通过下面的命令同步 Alice 的最新变化。 $ git-pull 注意不需为这个命令加入 Alice 的版本库的路径,因为当 Bob 克隆 Alice 的版本库的时候, git 已经将这个路径保存到 .git/remote/origin 文件中,它将会是所以的导入操作的默认路径。 Bob 可能已经注意到他并没有在他的版本库中创建过分支(但是分支已经存在了): $ git branch "origin" 分支,它是运行 "git-clone" 的时候自动创建的,他是 Alice 的 master 分支的原始镜像, Bob 应该永远不要向这个分支提交任何东西。 如果 Bob 以后决定在另外一部主机上开展工作,那么他仍然需要通过 SSH 协议从新克隆和导入( Alice 的版本库): $ git-clone alice.org:/home/alice/project/ myrepo 我们可以使用 git 自然协议,或者是 rsync, http 等协议的任何一种,详情请参考 git-pull。 Git 同样可以建立类似 CVS 那样的开发模式,也就是所有开发者都向中心版本库提交工作的方式, 详情参考 git_push 和 git for CVS users 。 为版本库打包在前面,我们已经看到在 .git/objects/??/ 目录中保存着我们创建的每一个 git 对象。 这样的方式对于自动和安全地创建对象很有效,但是对于网络传输则不方便。 git 对象一旦创建了,就不能被改变,但有一个方法可以优化对象的存储,就是将他们“打包到一起”。 $ git repack 上面的命令让你做到这点,如果你一直是做着我们的例子过来的, 你现在大约会在 .git/objects/??/ 目录下积累了17个对象。 git-repack 会告诉你有几个对象被打包了, 并且将他们保存在 .git/objects/pack 目录当中。
如果你是个偏执狂,就运行一下 git-verity-pack 命令来检查一下有缺陷的包吧, 不过,其实你无须太多担心,我们的程序非常出色 ;-). 一旦你已经对那些对象打包了,那么那些已经被打过包的原始的对象,就没有必要保留了。 $ git prune-packed 会帮你清楚他们。 如果你好奇的话,你可以在执行 git-prune-repacked 命令之前和之后, 都运行一下 find .git/objects -type f,这样你就能看到有多少没有打包的对象, 以及节省了多少磁盘空间。
如果你此时再次运行 git-repack,它就会说 "Nothing to pack"。 要是你继续开发,并且积累了一定数量的变迁,再运行 git-repack 将会创建一个新的包, 它会包含你自上次对库打包以来创建的对象。我们建议你尽快在初始化提交之后打包一下你的版本库( 除非你现在的项目是个涂鸦式的草稿项目),并且在项目经历过一段很活跃的时期时, 再运行 git-repack 一下。 当一个版本库通过 git-push 和 git-pull 命令来同步源版本库中打包过的对像的时候, 通常保存到目标版本库中的是解包了的对象,除非你使用的是 rsync(远程同步协议)协议的传输方式。 正是这种容许你在两头的版本库中有不同的打包策略的方式,他意味着你也许在过一段时间之后, 需要在两头的版本库中都重新打包一下。 发布你的工作我们可以通过一个远程的版本库来利用他人的工作,但是,你如何准备一个自己的版本库来供其他人下载呢? 你在自己的工作目录下进行工作,这样你的版本库就被作为.git的一个子目录放在你的工作树下。 你可以让其他人来远程的访问你的版本库,但是实际上这不是通常的做法。推荐的做法是创建一个公共的版本库, 让它可供其他人访问,并且,当你在你的工作目录下做了很好的改动时,你可以更新到公共的版本库中。这通常称为pushing。
从你的本地的(私有的)版本库中发布改动到你的远程的(公共的)版本库中需要远程机器上的写权限。你需要一个SSH的 帐号来运行一个简单的命令,git-receive-pack。 首先,你需要在远程机器上创建一个空的版本库来存放你的公共版本库。这个空版本库以后将通过pushing来保持更新。 显然,这个版本库之需要在开始的时候创建一次。
你本地的版本库的git目录通常是.git,但是你的公共版本库通常还要加上你的项目名,即.git。 让我们来为my-git创建这样一个版本库。首先,登入远程的机器,创建一个空目录(如果你选择HTTP作为发布方法, 这个空目录需要建在web server的根目录下面): $ mkdir my-git.git 然后运行git init-db命令将这个目录加入git版本库中,这里,因为这个版本库的名字不是通常的.git, 我们需要稍微改动一下命令: $ GIT_DIR=my-git.git git-init-db 有很多种传输方式可以发布公共版本库。这里,要确认这个目录可以通过你选择的传输方式来被其他人访问。你也需要确认 你有git-receive-pack这个程序在$PATH这个路径下。
现在你的“公共的版本库”可以接受你的任何改动了。回到你的本地机上,运行命令: $ git push :/path/to/my-git.git master 该命令将你的公共版本库和你当前的版本库中指定名称的分支头部同步(这里是master)。举一个实际的例子,你可以 这样来更新公共的git版本库。Kernel.org的镜像网络也这样来同步其他公共的可访问的机器: $ git push master.kernel.org:/pub/scm/git/git.git/ 将工作捆绑到一起通过 git 的分支功能,你可以非常容易地做到好像在同一时间进行许多“相关-或-无关”的工作一样。 我们已经通过前面的 "fun and work" 使用两个分支的例子,看到分支是怎么工作的。 这样的思想在多于两个的分支的时候也是一样的,比方说,你现在在 master 的头, 并有些新的代码在 master 中,另外还有两个互不相关的补丁分别在 "commit-fix" 和 "diff-fix" 两个分支中。 $ git show-branch 两个补丁我们都测试好了,到这里,你想将他们俩合并起来, 于是你可以先合并 diff-fix ,然后再合并 commit-fix,像这样: $ git merge 'Merge fix in diff-fix' master diff-fix 结果如下: $ git show-branch 然而,当你确信你手头上的确是一堆互不相关的项目变化时,就没有任何理由将这堆东西一个个地合并( 假如他们的先后顺序很重要,那么他们就不应该被定以为无关的变化), 你可以一次性将那两个分支合并到当前的分支中,首先我们将我们刚刚做过的事情逆转一下, 我们需要通过将 master 分支重置到 master~2 位置的方法来将它逆转到合并那两个分支之前的状态。 $ git reset --hard master~2 你可以用 git-show-branch 来确认一下的确是回到了两次 git-merge 的状态了。 现在你可以用一行命令将那两个分支导入的方式来替代两次运行( 也就是所谓的 炮制章鱼 -- making an Octopus)git-merge : $ git pull . commit-fix diff-fix 注意那些不适合制作章鱼的场合,尽管你可以那样做。一只“章鱼”往往可以使项目的提交历史更具可读性, 前提是你在同一时间导入的两份以上的变更是互不关联的。 然而,如果你在合并任何分支的过程中出现合并冲突,并且需要手工解决的话, 那意味着这些分支当中有相互干涉的开发工作在进行,那么你就应该将这个两个冲突先合并, 并且记录下你是如何解决这个冲突,以及你首先处理他们的理由。(译者按:处理完冲突之后, 你就可以放心制作“章鱼”了) 否则的话将会造成项目的发展历史很难跟踪。 管理版本库版本库的管理员可以用下面的工具来建立和维护版本库。
update hook howto 一个很好的管理中心版本库的例子。 例子
项目开发的模式推介尽管 git 是一个正式项目发布系统,它却可以方便地将你的项目建立在松散的开发人员组织形式上。 Linux 内核的开发,就是按这样的模式进行的。在 Randy Dunlap 的著作中("Merge to Mainline" 第17页) 就有很好的介绍(http://tinyurl.com/a2jdg)。 需要强调的是正真的非常规的开发组织形式, git 这种组织形式,意味着对于工作流程的约束,没有任何强迫性的原则。 你不必从唯一一个远程版本库中导入(工作目录)。 项目领导人(project lead)的工作推介
项目的子系统负责人(subsystem maintainer)也有自己的公共库,工作流程大致如下:
“一般开发人员”无须自己的公共库,大致的工作方式是:
Last updated 27-Mar-2006 15:20:34 UTC
学习 Git作者:孔建军1 Git 介绍Git
版本控制工具的作者是Linux之父Linus
Trovalds,最初是专门针对Linux内核开发的特点编写的,即协作人员异地分布、人数众多、项目规模巨大、复杂度高等。与常用的版本控制
CVS,Subversion等不同,它采用了分布式版本库的方式,不毕服务器断软件支持,使源代码的发布和维护极其方便。Git的本地查询、搜索,补丁
制作、提交和应用,项目跟踪,分支合并等功能,可以大大提高开发效率,具有较强的灵活性。有人认为Git太艰涩难懂,实际上结合一些有用的脚本命令使用,
会使其变得非常好用。 2 Git 配置Git命令的使用,一般有两种两种形式,一种是git后面带参数(如:git add),另一种是直接减号连接的一条命令(如:git-add),后面讲解全部使用后者,这样可以避免空格的使用带来的问题。
3 Git 使用
4 Git的项目开发模式Git作为一个正式项目发布系统,它能够极其有效的组织松散的开发人员,是一种非常规的开发组织形式,对工作流程没有任何强迫性的约束,比较灵活。
5 免费git项目注册网址:http://repo.or.cz 0、使用git-init-db在本地创建版本库; 6 总结7 参考资料
February 26 Collection Class Notes------especial CTypedPtrList
Expand the Header Files folder, and open file StdAfx.h. At the end of the file, type: #include <afxtempl.h> // MFC templates ///////////////////////////////////////////////////////////////////////// CTypedPtrList < class BASE_CLASS, class TYPE > BASE_CLASS Base class of the typed pointer list class; must be a pointer list class (CObList or CPtrList). TYPE Type of the elements stored in the base-class list. The CTypedPtrList class provides a type-safe “wrapper” for objects of class CPtrList. When you use CTypedPtrList rather than CObList or CPtrList, the C++ type-checking facility helps eliminate errors caused by mismatched pointer types. In addition, the CTypedPtrList wrapper performs much of the casting that would be required if you used CObList or CPtrList. ================================================== Class Members ================================================== AddHead POSITION AddHead( TYPE newElement ); void AddHead( CTypedPtrList<BASE_CLASS, TYPE> *pNewList ); AddTail POSITION AddTail( TYPE newElement ); void AddTail( CTypedPtrList<BASE_CLASS, TYPE> *pNewList ); GetAt TYPE& GetAt( POSITION position ); TYPE GetAt( POSITION position ) const; GetHead TYPE& GetHead( ); TYPE GetHead( ) const; GetNext TYPE& GetNext( POSITION& rPosition ); TYPE GetNext( POSITION& rPosition ) const; GetPrev TYPE& GetPrev(POSITION& rPosition ); TYPE GetPrev( POSITION& rPosition ) const; GetTail TYPE& GetTail( ); TYPE GetTail( ) const; RemoveHead TYPE RemoveHead( ); RemoveTail TYPE RemoveTail( ); SetAt void SetAt( POSITION pos, TYPE newElement ); ================================================== Create ================================================== typedef CTypedPtrList<CPtrList, CMyStruct*> CMyStructList; CMyStructList m_myPtrList; typedef CTypedPtrList<CObList, CMyObject*> CMyObList; CMyObList m_myObList; ================================================== Insert ================================================== CMyStruct* pMyStruct = new CMyStruct(); pMyStruct->m_int = 1234; pMyStruct->m_float = 12.34f; pMyStruct->m_str.LoadString(IDS_INITIAL_STRING); m_myPtrList.AddTail(pMyStruct); m_myPtrList.InsertBefore(pos, pMyStruct); CMyObject* pMyObject = new CMyObject(); m_myObList.AddTail(pMyObject); ================================================== Iterate ================================================== POSITION pos = m_myPtrList.GetHeadPosition(); while( pos != NULL ) { CMyStruct* pMyStruct = m_myPtrList.GetNext( pos ); } ================================================== Find ================================================== pos = m_myPtrList.Find(pMyStruct); ================================================== Update ================================================== m_myPtrList.SetAt(pos, pMyStruct); ================================================== Delete ================================================== m_myPtrList.RemoveAt(pos); POSITION pos = m_myPtrList.GetHeadPosition(); while (pos != NULL) { delete m_myPtrList.GetNext(pos); } m_myPtrList.RemoveAll(); while (!m_myObList.IsEmpty()) { delete m_myObList.GetHead(); m_myObList.RemoveHead(); } ================================================== Serialize ================================================== nCount = (WORD)m_myPtrList.GetCount(); if (ar.IsStoring()) { ar << nCount; pos = m_myPtrList.GetHeadPosition(); while (pos != NULL) { CMyStruct* pMyStruct = m_myPtrList.GetNext(pos); w = (WORD)pMyStruct->m_int; ar << w; ar << pMyStruct->m_float; ar << pMyStruct->m_str; nCount--; } ASSERT(nCount == 0); } else { ar >> nCount; while (nCount-- > 0) { CMyStruct* pMyStruct = new CMyStruct; ar >> w; pMyStruct->m_int = w; ar >> pMyStruct->m_float; ar >> pMyStruct->m_str; m_myPtrList.AddTail(pMyStruct); } } m_myObList.Serialize(ar); // Note: CMyObject serializes itself void CMyObject::Serialize(CArchive& ar) { CObject::Serialize( ar ); if( ar.IsStoring() ) ar << i; else ar >> i; } January 13 Index.dat Analyzer v2.5Index.dat Analyzer is a tool to view, examine and delete contents of index.dat files.
Index.dat files are hidden files on your computer that contain all tracks of your online activity, where have you been on internet, what sites you visited, list of URL-s, files and documents you recently accessed. Index.dat files stored on your computer are obviously a potential privacy threat as they can be found and viewed without your knowledge. Windows Sysinternals Suite Build 2009.01.12Sysinternals Suite是微软发布的一套非常强大的免费工具程序集.我想介绍就不用多说了吧.用好Windows Sysinternals Suite里的工具,你将更有能力处理Windows的各种问题,而且不花一毛钱.Sysinternals 之前为Winternals公司提供的免费工具,Winternals原本是一间主力产品为系统复原与资料保护的公司,为了解决工程师平常在工作上遇到的各种问题,便开发出许多小工具.之后他们将这些工具集合起来称为Sysinternals,并放在网路供人免费下载,其中也包含部分工具的原始码,一直以来都颇受IT专家社群的好评. The Suite is a bundling of the following selected Sysinternals Utilities: September 02 InfoWorld2008最佳开源软件大奖新闻来源:CHIP中文版 事实上,InfoWorld的年度开源软件大奖很有分量,不过遗憾的是因为没有中文版本,所以很少有国内用户关注这个奖项。 InfoWorld 2008年的"开源软件大奖"最新出炉,CHIP软件社区乘此机会将InfoWorld 2008年的"开源软件大奖"中文化并进行整理,希望能够为中国用户带来便利,也希望能够为开源社区共享绵薄之力。 事实上,千万不要把开源软件想得那么神秘,比如在"InfoWorld2008最佳开源软件大奖"中:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Windows | Linux | 类别 |
CreateProcess() CreateProcessAsUser()
| fork() setuid() exec() | 可映射
|
TerminateProcess()
| kill()
| 可映射
|
SetThreadpriority() GetThreadPriority()
| Setpriority() getPriority()
| 可映射
|
GetCurrentProcessID()
| getpid()
| 可映射
|
Exitprocess()
| exit()
| 可映射
|
Waitforsingleobject() Waitformultipleobject() GetExitCodeProcess()
| waitpid() Using Sys V semaphores, Waitforsingleobject/multipleobject 不能实现
| 与上下文相关
|
GetEnvironmentVariable SetEnvironmentVariable
| getenv() setenv()
| 可映射 |
"类别"一列(解释了本文中所使用的分类结构)表明了 Windows 结构是否 可映射 或者 与上下文相关:
在 Windows 中,您可以使用 CreateProcess() 来创建一个新的进程。 CreateProcess() 函数创建一个新的进程及其主线程,如下:
BOOL CreateProcess( LPCTSTR lpApplicationName, // name of executable module LPTSTR lpCommandLine, // command line string LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD BOOL bInheritHandles, // handle inheritance option DWORD dwCreationFlags, // creation flags LPVOID lpEnvironment, // new environment block LPCTSTR lpCurrentDirectory, // current directory name LPSTARTUPINFO lpStartupInfo, // startup information LPPROCESS_INFORMATION lpProcessInformation // process information ) |
bInheritHandles 确定了子进程是否要继承父进程的句柄。lpApplicationName 和 lpCommandLine 给出了将要被启动的进程的名称与路径。lpEnvironment 定义了进程可使用的环境变量。
在 Linux 中,exec* 家族函数使用一个新的进程映像取代当前进程映像(如下所示):
int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg , ..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); |
exec* 的这些版本只是内核函数 execve() (int execve(const char *filename, char *const argv [], char *const envp[]))的各种调用接口。在这里,argv 是包含有参数 list 的指针,envp 是包含有环境变量列表(主要是 key=value 对)的指针。
它必须与 fork() 命令一起使用,所以父进程和子进程都在运行: pid_t fork(void)。fork() 会创建一个子进程,与父进程相比只是 PID 和 PPID 不同;实际上,资源利用设为 0。
默认情况下,exec() 继承父进程的组和用户 ID,这就使得它会依赖于父进程。可以使用以下方法来改变:
set-uid 和 set-gid 位
setpgid() 和 setuid() 系统调用 CreateProcessAsUser() 函数与 CreateProcess() 类似,只是新进程是在用户通过 hToken 参数描述的安全上下文中运行。在 Linux 中,没有与此函数惟一对应的函数,但是可以使用下面的逻辑来实现对它的复制:
fork() 创建一个具有新的 PID 的子进程
setuid() 切换到那个新的 PID
exec() 将现有进程改变为将要执行的进程 在 Windows 中,您可以使用 TerminateProcess() 强制终止一个运行中的进程。
BOOL TerminateProcess( HANDLE hProcess, // handle to the process UINT uExitCode // exit code for the process ); |
这个函数终止运行中的进程及其相关线程。只是在非常极端的场合才会使用这个函数。
在 Linux 中,您可以使用 kill() 来强行杀死一个进程: int kill(pid_t pid, int sig)。这个系统调用会终止 id 为 PID 的进程。您也可以使用它向任何组或者进程发出信号。
在子进程依赖于父进程的情况下,您可以在父进程中使用等待函数来等待子进程的终止。在 Windows 中,您可以使用 WaitForSingleObject() 函数调用来实现此功能。
您可以使用 WaitForMultipleObject() 函数来等待多个对象。
DWORD WaitForMultipleObjects( DWORD nCount, // number of handles in array CONST HANDLE *lpHandles, // object-handle array BOOL bWaitAll, // wait option DWORD dwMilliseconds // time-out interval ); |
您可以向对象句柄数组(object-handle array)中填充很多需要等待的对象。根据 bWaitALL 选项,您既可以等待所有对象被信号通知,也可以等待其中任意一个被信号通知。
在这两个函数中,如果您想等待有限的一段时间,则可以在第二个参数中指定时间间隔。如果您想无限制等待,那么使用 INFINITE 作为 dwMilliseconds 的值。将 dwMilliseconds 设置为 0 则只是检测对象的状态并返回。
在 Linux 中,如果您希望无限期等待进程被杀死,则可以使用 waitpid()。在 Linux 中,使用 waitpid() 调用无法等待限定的时间。
在这段代码中:pid_t waitpid(pid_t pid, int *status, int options),waitpid() 会无限期等待子进程的终止。在 Windows 和 Linux 中,等待函数会挂起当前进程的执行,直到它完成等待,不过,在 Windows 中可以选择在指定的时间后退出。使用 System V 信号量,您可以实现类似于 WaitForSingleObject() 和 WaitForMultipleObject() 的限时等待或者 NO WAIT 功能,在本系列的第 2 部分中将讨论此内容。本系列的第 3 部分将深入讨论等待函数。
退出进程指的是优雅(graceful)地退出进程,并完成适当的清除工作。在 Windows 中,您可以使用 ExitProcess() 来执行此操作。
VOID ExitProcess( UINT uExitCode // exit code for all threads ); |
ExitProcess() 是在进程结束处执行的方法。这个函数能够干净地停止进程。包括调用所有链接到的动态链接库(DLL)的入口点函数,给出一个值,指出这个进程正在解除那个 DLL 的链接。
Linux 中与 ExitProcess() 相对应的是 exit():void exit(int status);。
exit() 函数会令程序正常终止,并将 &0377 状态值返回给父进程。 C 语言标准规定了两个定义(EXIT_SUCCESS 和 EXIT_FAILURE),可以被传递到状态参数,以说明终止成功或者不成功。
每个进程都拥有关联到它的一组环境,其中主要是 name=value 对,指明进程可以访问的各种环境变量。尽管我们可以在创建进程时指定环境,不过也有特定函数可以在进程创建后设置和获得环境变量。
在 Windows 中,您可以使用 GetEnvironmentVariable() 和 SetEnvironmentVariable() 来获得和设置环境变量。
DWORD GetEnvironmentVariable( LPCTSTR lpName, // environment variable name LPTSTR lpBuffer, // buffer for variable value DWORD nSize // size of buffer ); |
如果成功,则此函数返回值缓存的大小,如果指定的名称并不是一个合法的环境变量名,则返回 0。 SetEnvironmentVariable() 函数为当前进程设置指定的环境变量的内容。
BOOL SetEnvironmentVariable( LPCTSTR lpName, // environment variable name LPCTSTR lpValue // new value for variable ); |
如果函数成功,则返回值非零。如果函数失败,则返回值为零。
在 Linux 中,getenv() 和 setenv() 系统调用提供了相应的功能。
char *getenv(const char *name); int setenv(const char *name, const char *value, int overwrite); |
getenv() 函数会在环境列表中搜索与名称字符串相匹配的字符串。这个函数会返回一个指向环境中的值的指针,或者如果不匹配则返回 NULL。setenv() 函数将变量名和值添加到环境中,如果那个名称并不存在。如果环境中已经存在那个名称,而且如果 overwrite 非零,则它的值会被修改为 value。如果 overwrite 为零,则 name 的值不会被改变。如果成功,则 setenv() 会返回零,如果环境中空间不足,则返回 -1。
下面的例子解释了我们在本节中讨论的内容。
//Sample Application that explain process concepts
//Parameters Declaration/Definition
int TimetoWait;
STARTUPINFO si;
PROCESS_INFORMATION pi;
LPTSTR lpszCurrValue,LPTSTR lpszVariable;
TCHAR tchBuf[BUFSIZE];
BOOL fSuccess;
if(argc > 2)
{
printf("InvalidArgument");
ExitProcess(1); //Failure
}
//Get and display an environment variable PATH
lpszCurrValue = ((GetEnvironmentVariable("PATH",tchBuf, BUFSIZE) > 0) ? tchBuf : NULL);
lpszVariable = lpszCurrValue;
//Display the environment variable
while (*lpszVariable)
putchar(*lpszVariable++);
putchar('\n');
//Initialise si and pi
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//Create a childProcess
if( !CreateProcess( NULL, // No module name (use command line).
"SomeProcess", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed." );
}
// Wait until child process exits.
if(argc == 2)
{
TIMEOUT = atoi(argv[1]);
ret = WaitForSingleObject( pi.hProcess, TIMEOUT );
if(ret == WAIT_TIMEOUT)
{
TerminateProcess(pi.hProcess);
}
}
else
{
WaitForSingleObject( pi.hProcess, INFINITE );
...
}
ExitProcess(0); //Success
|
#include <stdlib.h>
int main(int argc,char *argv[])
{
//Parameters Declaration/Definition
char PathName[255];
char *Argptr[20];
int rc;
char *EnvValue,*lpszVariable;
if(argc > 1)
{
printf(" Wrong parameters !!");
exit(EXIT_FAILURE);
}
//Get and display an environment variable PATH
EnvValue = getenv("PATH");
if(EnvValue == NULL)
{
printf("Invalid environment variable passed as param !!");
}else
{
lpszVariable = EnvValue;
while (*lpszVariable)
putchar(*lpszVariable++);
putchar('\n');
}
rc = fork(); //variable rc's value on success would be process ID in the parent
//process, and 0 in the child's thread of execution.
switch(rc)
{
case -1:
printf("Fork() function failed !!");
ret = -1;
break;
case 0:
printf("Child process...");
setpgid(0,0); //Change the parent grp ID to 0
ret = execv(PathName,Argptr); // there are other flavours of exec available,
// u can use any of them based on the arguments.
if(ret == -1)
{
kill(getpid(),0);
}
break;
default:
// infinitely waits for child process to die
Waitpid(rc,&status,WNOHANG);
//Note RC will have PID returned since this is parent process.
break;
}
exit(EXIT_SUCCESS);
}
|
|
在 Windows 中,线程是基本的执行单位。在进程的上下文中会有一个或多个线程在运行。调度代码在内核中实现。没有单独的"调度器(scheduler)"模块或例程。
Linux 内核使用的是进程模型,而不是线程模型。Linux 内核提供了一个轻量级进程框架来创建线程;实际的线程在用户空间中实现。在 Linux 中有多种可用的线程库(LinuxThreads、NGPT、NPTL 等等)。本文中的资料基于 LinuxThreads 库,不过这里的资料也适用于 Red Hat 的 Native POSIX Threading Library(NPTL)。
本节描述 Windows 和 Linux 中的线程。内容涵盖了创建线程、设置其属性以及修改其优先级。
| Windows | Linux | 类别 |
CreateThread
| pthread_create pthread_attr_init pthread_attr_setstacksize pthread_attr_destroy
| 可映射
|
ThreadExit
| pthread_exit
| 可映射
|
WaitForSingleObject
| pthread_join pthread_attr_setdetachstate pthread_detach
| 可映射
|
SetPriorityClass SetThreadPriority
| setpriority sched_setscheduler sched_setparam pthread_setschedparam pthread_setschedpolicy pthread_attr_setschedparam pthread_attr_setschedpolicy
| 与上下文相关 |
在 Windows 中,您可以使用 CreateThread() 来创建线程,创建的线程在调用进程的虚拟地址空间中运行。
HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD SIZE_T dwStackSize, // initial stack size LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPVOID lpParameter, // thread argument DWORD dwCreationFlags, // creation option LPDWORD lpThreadId // thread identifier ); |
lpThreadAttributes 是指向线程属性的指针,决定了线程句柄是否能由子进程继承。
Linux 使用 pthread 库调用 pthread_create() 来派生线程:
int pthread_create (pthread_t *thread_id, pthread_attr_t *threadAttr,
void * (*start_address)(void *), void * arg);
|
注意:在 Windows 中,受可用虚拟内存的限制,一个进程可以创建的线程数目是有限的。默认情况下,每个线程有一兆栈空间。因此,您最多可以创建 2,028 个线程。如果您减小默认栈大小,那么可以创建更多线程。在 Linux 中,使用 ULIMIT -a(limits for all users)可以获得每个用户可以创建的线程的最大数目,可以使用 ULIMIT -u 来修改它,不过只有在登录时才可以这样做。 /usr/Include/limit.h 和 ulimit.h 下的头文件定义了这些内容。您可以修改它们并重新编译内核,以使其永久生效。对于 POSIX 线程限制而言,local_lim.h 中定义的 THREAD_THREADS_MAX 宏定义了数目的上限。
CreateThread() 中的 lpStartAddress 参数是刚创建的线程要执行的函数的地址。
pthread_create() 库调用的 start_address 参数是刚创建的线程要执行的函数的地址。
在 Windows 中,系统调用 CreateThread() 的参数 lpParameter 指定了要传递给刚创建的线程的参数。它指明了将要传递给新线程的数据条目的地址。
在 Linux 中,库调用 pthread_create() 的参数 arg 指定了将要传递给新线程的参数。
在 Windows 中,CreateThread() 的参数 dwStackSize 是将要分配给新线程的以字节为单位的栈大小。栈大小应该是 4 KB 的非零整数倍,最小为 8 KB。
在 Linux 中,栈大小在线程属性对象中设置;也就是说,将类型为 pthread_attr_t 的参数 threadAttr 传递给库调用 pthread_create()。在设置任何属性之前,需要通过调用 pthread_attr_init() 来初始化这个对象。使用调用 pthread_attr_destroy() 来销毁属性对象:
int pthread_attr_init(pthread_attr_t *threadAttr); int pthread_attr_destroy(pthread_attr_t *threadAttr); |
注意,所有 pthread_attr_setxxxx 调用都有与 pthread_xxxx 调用(如果有)类似的功能,只是您只能在线程创建之前使用 pthread_attr_xxxx,来更新将要作为参数传递给 pthread_create 的属性对象。同时,您在创建线程之后的任意时候都可以使用 pthread_xxxx。
使用调用 pthread_attr_setstacksize() 来设置栈大小: int pthread_attr_setstacksize(pthread_attr_t *threadAttr, int stack_size);。
在 Windows 中,系统调用 ExitThread() 会终止线程。 dwExitCode 是线程的返回值,另一个线程通过调用 GetExitCodeThread() 就可以得到它。
VOID ExitThread( DWORD dwExitCode // exit code for this thread ); |
Linux 中与此相对应的是库调用 pthread_exit()。 retval 是线程的返回值,可以在另一个线程中通过调用 pthread_join() 来获得它: int pthread_exit(void* retval);。
在 Windows 中,没有保持关于线程终止的显式线程状态。不过,WaitForSingleObject() 让线程能够显式地等待进程中某个指定的或者非指定的线程终止。
在 Linux 中,默认以可连接(joinable)的状态创建线程。在可连接状态中,另一个线程可以同步这个线程的终止,使用函数 pthread_join() 来重新获得其终止代码。可连接的线程只有在被连接后才释放线程资源。
Windows 使用 WaitForSingleObject() 来等待某个线程终止:
DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds ); |
其中:
hHandle 是指向线程句柄的指针。
dwMilliseconds 是以毫秒为单位的超时值。如果这个值被设置为 INFINITE,则它会无限期地阻塞进行调用的线程/进程。 Linux 使用 pthread_join() 来完成同样的功能: int pthread_join(pthread_t *thread, void **thread_return);。
在分离的状态中,线程终止后线程资源会立即被释放。通过对线程属性对象调用 pthread_attr_setdetachstate() 可以设置分离状态: int pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate);。以可连接状态创建的线程,稍后可以被转为分离状态,方法是使用 pthread_detach() 调用:int pthread_detach (pthread_t id);。
在 Windows 中,线程的优先级由其进程的优先级等级以及进程优先级等级中的线程优先级层次决定。在 Linux 中,线程本身就是一个执行单位,有其自己的优先级。它与其进程的优先级没有依赖关系。
在 Windows 中,您可以使用 SetPriorityClass() 来设置特定进程的优先级等级:
BOOL SetPriorityClass( HANDLE hProcess, // handle to the process DWORD dwPriorityClass // Priority class ); |
dwPriorityClass 是进程的优先级等级,它可以设置为下列值中的任意一个:
IDLE_PRIORITY_CLASS
BELOW_NORMAL_PRIORITY_CLASS
NORMAL_PRIORITY_CLASS
ABOVE_NORMAL_PRIORITY_CLASS
HIGH_PRIORITY_CLASS
REALTIME_PRIORITY_CLASS 一旦设置了进程的优先级等级,就可以使用 SetThreadPriority() 在进程的优先级等级内部设置线程的优先级层次:
BOOL SetThreadPriority( HANDLE hThread, int nPriority ); |
nPriority 是线程的优先级值,它被设置为下列之一;
THREAD_PRIORITY_ABOVE_NORMAL 将优先级设置为比优先级等级高 1 级。
THREAD_PRIORITY_BELOW_NORMAL 将优先级设置为比优先级等级低 1 级。
THREAD_PRIORITY_HIGHEST 将优先级设置为比优先级等级高 2 级。
THREAD_PRIORITY_IDLE 为 IDLE_PRIORITY_CLASS、BELOW_NORMAL_PRIORITY_CLASS、NORMAL_PRIORITY_CLASS、ABOVE_NORMAL_PRIORITY_CLASS 或 HIGH_PRIORITY_CLASS 进程将基优先级设置 1,为 REALTIME_PRIORITY_CLASS 进程将基优先级设置为 16。
THREAD_PRIORITY_LOWEST 将优先级设置为比优先级等级低 2 级。
THREAD_PRIORITY_NORMAL 为优先级等级设置为普通优先级。
THREAD_PRIORITY_TIME_CRITICAL 为 IDLE_PRIORITY_CLASS、BELOW_NORMAL_PRIORITY_CLASS、NORMAL_PRIORITY_CLASS、ABOVE_NORMAL_PRIORITY_CLASS 或 HIGH_PRIORITY_CLASS 进程将基优先级设置 15,为 REALTIME_PRIORITY_CLASS 进程将基优先级设置为 31。
|
为了结束这一期文章,让我们来看下面类型的进程和线程的一些例子:
使用 Linux 系统调用 setpriority() 来设置或者修改普通进程和线程的优先级层次。参数的范围是 PRIO_PROCESS。将 id 设置为 0 来修改当前进程(或线程)的优先级。此外,delta 是优先级的值 —— 这一次是从 -20 到 20。另外,要注意在 Linux 中较低的 delta 值代表较高的优先级。所以,使用 +20 设置 IDLETIME 优先级,使用 0 设置 REGULAR 优先级。
在 Windows 中,常规线程的优先级的范围是从 1(较低的优先级)到 15(较高的优先级)。不过,在 Linux 中,普通非实时进程的优先级范围是从 -20(较高的)到 +20(较低的)。在使用之前必须对此进行映射: int setpriority(int scope, int id, int delta);。
您可以使用 Linux 系统调用 sched_setscheduler() 来修改正在运行的进程的调度优先级: int sched_setscheduler(pit_t pid, int policy, const struct sched_param *param);。
参数 policy 是调度策略。policy 的可能的值是 SCHED_OTHER (常规的非实时调度)、SCHED_RR(实时 round-robin 策略)和 SCHED_FIFO(实时 FIFO 策略)。
在此,param 是指向描述调度优先级结构体的指针。它的范围是 1 到 99,只用于实时策略。对于其他的(普通的非实时进程),它为零。
在 Linux 中,作为一个大家所熟知的调度策略,也可以通过使用系统调用 sched_setparam 来仅修改进程优先级: int sched_setparam(pit_t pid, const struct sched_param *param);。
LinuxThreads 库调用 pthread_setschedparam 是 sched_setscheduler 的线程版本,用于动态修改运行着的线程的调度优先级和策略: int pthread_setschedparam(pthread_t target_thread, int policy, const struct sched_param *param);。
参数 target_thread 告知线程要修改谁的优先级;param 指定了优先级。
LinuxThreads 库会调用 pthread_attr_setschedpolicy,并且您可以在线程被创建之前使用 pthread_attr_setschedparam 来设置线程属性对象的调度策略和优先级层次:
int pthread_attr_setschedpolicy(pthread attr_t *threadAttr, int policy); int pthread_attr_setschedparam(pthread attr_t *threadAttr, const struct sched_param *param); |
在 Windows 中,实时线程的优先级范围是从 16(较低的优先级)到 31(较高的优先级)。在 Linux 中,实时线程的优先级范围是从 99(较高的)到 1(较低的优先级)。在使用前必须对此进行映射。
下面的清单阐述了本节中的概念。
Main Thread
enum stackSize = 120 * 1024 ;
// create a thread normal and real time thread
DWORD normalTId, realTID;
HANDLE normalTHandle, realTHandle;
normalTHandle = CreateThread(
NULL, // default security attributes
stackSize, // 120K
NormalThread, // thread function
NULL, // argument to thread function
0, // use default creation flags
&normalTId); // returns the thread identifier
// Set the priority class as "High priority"
SetPriorityClass(pHandle, HIGH_PRIORITY_CLASS);
normalTHandle = CreateThread(
NULL, // default security attributes
stackSize, // 120K
NormalThread, // thread function
NULL, // argument to thread function
0, // use default creation flags
&normalTId); // returns the thread identifier
CloseHandle(threadHandle);
...
...
// Thread function
DWORD WINAPI NormalThread ( LPVOID lpParam )
{
HANDLE tHandle,pHandle;
pHandle = GetCurrentProcess();
tHandle = GetCurrentThread();
// Set the priority class as "High priority"
SetPriorityClass(pHandle, HIGH_PRIORITY_CLASS);
// increase the priority by 2 points above the priority class
SetThreadPriority(tHandle,THREAD_PRIORITY_HIGHEST);
// perform job at high priority
...
...
...
// Reset the priority class as "Normal"
SetPriorityClass(pHandle, NORMAL_PRIORITY_CLASS);
// set the priority back to normal
SetThreadPriority(tHandle,THREAD_PRIORITY_NORMAL);
// Exit thread
ExitThread(0);
}
// Thread function
DWORD WINAPI RealTimeThread ( LPVOID lpParam )
{
HANDLE tHandle, pHandle ;
pHandle = GetCurrentProcess();
tHandle = GetCurrentThread ();
// Set the priority class as "Real time"
SetPriorityClass(pHandle, REALTIME_PRIORITY_CLASS);
// increase the priority by 2 points above the priority class
SetThreadPriority(tHandle,THREAD_PRIORITY_HIGHEST);
// do time critical work
...
...
...
// Reset the priority class as "Normal"
SetPriorityClass(pHandle, NORMAL_PRIORITY_CLASS);
// Reset the priority back to normal
SetThreadPriority(tHandle,THREAD_PRIORITY_NORMAL);
ExitThread(0);
}
|
static void * RegularThread (void *);
static void * CriticalThread (void *);
// Main Thread
pthread_t thread1, thread2; // thread identifiers
pthread_attr_t threadAttr;
struct sched_param param; // scheduling priority
// initialize the thread attribute
pthread_attr_init(&threadAttr);
// Set the stack size of the thread
pthread_attr_setstacksize(&threadAttr, 120*1024);
// Set thread to detached state. No need for pthread_join
pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);
// Create the threads
pthread_create(&thread1, &threadAttr, RegularThread, NULL);
pthread_create(&thread2, &threadAttr, CriticalThread,NULL);
// Destroy the thread attributes
pthread_attr_destroy(&threadAttr);
...
...
// Regular non-realtime Thread function
static void * RegularThread (void *d) {
int priority = -18;
// Increase the priority
setpriority(PRIO_PROCESS, 0, priority);
// perform high priority job
...
...
// set the priority back to normal
setpriority(PRIO_PROCESS, 0, 0);
pthread_exit(NULL);
}
// Time Critical Realtime Thread function
static void * CriticalThread (void *d) {
// Increase the priority
struct sched_param param; // scheduling priority
int policy = SCHED_RR; // scheduling policy
// Get the current thread id
pthread_t thread_id = pthread_self();
// To set the scheduling priority of the thread
param.sched_priority = 90;
pthread_setschedparam(thread_id, policy, ¶m);
// Perform time critical task
...
...
// set the priority back to normal
param.sched_priority = 0;
policy = 0; // for normal threads
pthread_setschedparam(thread_id, policy, ¶m);
....
....
pthread_exit(NULL);
}
|
|
本系列文章的第 1 部分已经给出了一个指南,能够帮助您将 Windows 进程和线程映射到 Linux 中的对应物。系列的第 2 部分介绍了同步对象和原语,首先是信号量和事件。第 3 部分介绍了互斥体、关键区域和等待函数。
|
|
Srinivasan S. Muthuswamy 是 IBM Global Services Group 的一位软件工程师。他于 2000 年加入 IBM,他所精通的编程语言涵盖了多种平台上(Linux、Windows、WebSphere、Lotus 等等)的脚本语言以及面向对象和面向过程的语言。他已经开发的解决方案包括 Linux 和 Windows 上的系统编程以及用于 J2EE 的 Web 解决方案。他在印度的 Coimbatore 国立科技大学(Government College of Technology)获得计算机工程学士学位,主要致力于集成和迁移。您可以通过 smuthusw@in.ibm.com 与他联系。 | |
|
|
从 2000 年 12 月起,Kavitha Varadarajan 一直在 IBM India Software Lab 担任软件工程师。她的工作经验包括 host-access 客户机产品(比如 PCOMM)和网络软件(比如通信服务器)的开发与支持。Varadarajan 拥有迁移项目的实践经验,其中涉及到了面向对象 IPC Windows 应用程序向 Linux 的移植。她拥有印度的 Tanjore 山姆哈工程大学(Shanmugha College of Engineering)的计算机科学与工程硕士学位。您可以通过 vkavitha@in.ibm.com 与她联系。 | |
级别: 初级
Nam Keung (mailto:namkeung@us.ibm.com), 高级程序员, IBM
2005 年 4 月 21 日
将您的 Win32 C/C++ 应用程序迁移到 POWER™ 上的 Linux™,并从信号(semaphore)应用程序接口(application program interface,API)的角度理解 Win32 到 Linux 的映射。Nam Keung 将通过详细的代码示例来为您描述这一过程。
本系列第三篇文章从信号的角度阐述了 Win32 C/++ 应用程序向 POWER 上的 Linux 的迁移。本系列的第 1 部分介绍了 Win32 API 映射,第 2 部分从互斥(mutex)API 的角度集中阐述了如何将 Win32 映射到 Linux。在继续阅读之前,建议您先去阅读本系列的第 1 部分和第 2 部分。
|
信号是包含有一个正数的资源。信号允许进程通过一个单一的原子操作来测试和设置那个整数的值,以此实现同步。通常,信号的主要用途是同步某个线程与其他线程的动作。在多个进程竞争访问同一操作系统资源时,这也是协调或者同步那些行为的一种实用技术。
Linux 支持 Portable Operating System Interface(POSIX)信号以及 pthread 条件变量,以此来映射 Win32 信号 API。它们各有其优缺点。您应该基于应用程序的逻辑来判断使用哪种方法。在映射事件信号的过程中需要考虑的方面包括:
pthread_cond_timedwait() 调用能给出等待期间的超时的值,pthread_cond_wait() 则用于不确定的等待。
pthread_cond_signal() 调用会唤醒一个线程,pthread_cond_broadcast() 调用会向所有等待那个信号的线程发出信号。
| Win32 | pthread Linux | POSIX |
CreateSemaphore
| pthread_mutex_init(&(token)->mutex, NULL))
| sem_init
|
CloseHandle (semHandle)
| pthread_mutex_destroy(&(token->mutex))
| sem_destroy
|
ReleaseSemaphore(semHandle, 1, NULL)
| pthread_cond_signal(&(token->condition))
| sem_post
|
WaitForSingleObject(semHandle,
| pthread_cond_wait(&(token->condition),
| sem_wait |
|
条件变量让开发者能够实现一个条件,在这个条件下线程执行然后被阻塞。Microsoft® Win32 接口本身不支持条件变量。为解决此缺憾,我使用 POSIX 条件变量模拟同步原语,并在一系列文章中对此进行了概述。在 Linux 中,它可以确保因某条件被阻塞的线程,当那个条件改变时,会被解除阻塞。它还允许您原子地(atomically)解除互斥的锁定,并等待条件变量,而不会有干涉其他线程的可能。不过,每个条件变量都应该伴有一个互斥。前面的表 1 给出了用于线程间同步的 pthread 条件变量。
|
在 Win32 中,CreateSemaphore 函数可以创建一个有名称的或者无名称的信号对象。Linux 不支持有名称的信号。
HANDLE CreateSemaphore ( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximunCount, LPCTSTR lpName ); |
在 Linux 中,sem_init() 调用会创建一个 POSIX 信号:
清单 2. POSIX 信号
int sem_init(sem_t *sem, int pshared, unsigned int value |
Linux 使用 pthread_condition_init 调用在当前进程内创建信号对象,在其中维持一个在零与最大值之间的计数值。每次有某个线程完成对信号的等待,这个计数值会减小,而每次当某个线程释放这个信号时,计数值增加。当计数值成为零时,信号对象的状态成为 non-signaled。
清单 3. 创建信号对象的 pthread_condition_init 调用
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); |
HANDLE semHandle;
semHandle = CreateSemaphore(NULL, 0, 256000, NULL);
/* Default security descriptor */
if( semHandle == (HANDLE) NULL)
/* Semaphore object without a name */
{
return RC_OBJECT_NOT_CREATED;
}
|
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t condition;
int semCount;
}sem_private_struct, *sem_private;
sem_private token;
token = (sem_private) malloc(sizeof(sem_private_struct));
if(rc = pthread_mutex_init(&(token->mutex), NULL))
{
free(token);
return RC_OBJECT_NOT_CREATED;
}
if(rc = pthread_cond_init(&(token->condition), NULL))
{
pthread_mutex_destroy( &(token->mutex) );
free(token);
return RC_OBJECT_NOT_CREATED;
}
token->semCount = 0;
|
|
Win32 使用 CloseHandle 来删除由 CreateSemaphore 所创建的信号对象。
BOOL CloseHandle (HANDLE hObject); |
Linux POSIX 信号使用 sem_destroy() 来销毁无名称的信号。
int sem_destroy(sem_t *sem); |
在 Linux pthreads 中,使用 pthread_cond_destroy() 来销毁条件变量。
清单 8. pthread_cond_destroy()
int pthread_cond_destroy(pthread_cond_t *cond); |
| Win32 代码 | 相应的 Linux 代码 |
CloseHandle(semHandle);
| pthread_mutex_destroy(&(token->mutex)); |
|
在 Win32 中,ReleaseSemaphore 函数会令指定的信号对象的计数值增加指定数量。
清单 10. ReleaseSemaphore 函数
BOOL ReleaseSemaphore( HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount ); |
Linux POSIX 信号使用 sem_post() 来发布事件信号。这将唤醒阻塞于此信号的所有线程。
清单 11. sem_post()
int sem_post(sem_t * sem); |
在 Linux 中,pthread_cond_signal 会唤醒等待某个条件变更的某个线程。Linux 调用这个函数来为此对象所标识的信号发布一个事件完成信号。调用的线程增加那个信号的值。如果信号的值从零开始增加,而且 pthread_cond 中有任何线程被阻塞,那么请等待这个信号,因为其中一个会被唤醒。默认情况下,实现可以选择任意的正在等待的线程。
清单 12. pthread_cond_signal
int pthread_cond_signal(pthread_cond_t *cond); |
| Win32 代码 | 相应的 Linux 代码 |
ReleaseSemaphore(semHandle, 1, NULL)
| if (rc = pthread_mutex_lock(&(token->mutex))) |
|
Win32 调用 WaitForSingleObject 函数来等待所需要的信号上事件的完成。当等待单一线程同步对象时,可以使用此方法。当对象被设置发出信号或者超时时间段结束时,这个方法会得到通知。如果时间间隔是 INFINITE,那么它就会无止境地等待下去。
清单 14. WaitForSingleObject 函数
DWORD WaitForSingleObject( HANDLE hHANDLE, DWORD dwMilliseconds ); |
使用 WaitForMultipleObjects 函数来等待多个被通知的对象。在信号线程同步对象中,当计数器变为零时,对象是 non-signaled。
清单 15. WaitForMultipleObjects 函数
DWORD WaitForMultipleObjects( DWORD nCount, Const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds ); |
Linux POSIX 信号使用 sem_wait() 来挂起发出调用的线程,直到信号拥有了非零的计数值。然后它自动地减少信号的计数值。
清单 16. sem_wait() 函数
int sem_wait(sem_t * sem); |
在 POSIX 信号中不能使用超时选项。不过,您可以通过在某个循环中执行非阻塞的 sem_trywait() 来完成此功能,它会计算超时的值。
清单 17. sem_trywait() 函数
int sem_trywait(sem_t * sem); |
在 Linux 中,pthread_cond_wait() 会阻塞发出调用的线程。发出调用的线程会减小那个信号。如果当 pthread_cond_wait 被调用时信号是零,则 pthread_cond_wait() 就会阻塞,直到另一个线程增加了那个信号的值。
清单 18. pthread_cond_wait() 函数
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); |
pthread_cond_wait 函数首先释放相关联的 external_mutex of type pthread_mutex_t,当调用者检查条件表达式时必须持有它。
| Win32 代码 | 相应的 Linux 代码 |
DWORD retVal;
| if (rc = pthread_mutex_lock(&(token->mutex))) |
如果您需要在指定的一段时间内阻塞发出调用的线程,那么请使用 pthread_cond_timewait 来阻塞它。调用这个方法来等待所需要信号上某个事件的完成,等待指定的一段时间。
int pthread_cond_timewait( pthread_cond_t *cond, pthread_mutex_t *mutex, timespec *tm ); |
| Win32 代码 | 相应的 Linux 代码 |
retVal = WaitForSingleObject(SemHandle, timelimit);
| int rc; |
|
清单 22 使用 POSIX 信号来实现线程 A 和 B 之间的同步:
清单 22. POSIX 信号示例代码
sem_t sem; /* semaphore object */ int irc; /* return code */ /* Initialize the semaphore - count is set to 1*/ irc = sem_init (sem, 0,1) ... /* In Thread A */ /* Wait for event to be posted */ sem_wait (&sem); /* Unblocks immediately as semaphore initial count was set to 1 */ ....... /* Wait again for event to be posted */ sem_wait (&sem); /* Blocks till event is posted */ /* In Thread B */ /* Post the semaphore */ ... irc = sem_post (&sem); /* Destroy the semaphore */ irc = sem_destroy(&sem); |
|
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void thrdproc (void *data); // the thread procedure (function)
to be executed
HANDLE semHandle;
int
main( int argc, char **argv )
{
HANDLE *threadId1;
HANDLE *threadId2;
int hThrd;
unsigned stacksize;
int arg1;
if( argc < 2 )
arg1 = 7;
else
arg1 = atoi( argv[1] );
printf( "Intra Process Semaphor test.\n" );
printf( "Start.\n" );
semHandle = CreateSemaphore(NULL, 1, 65536, NULL);
if( semHandle == (HANDLE) NULL)
{
printf("CreateSemaphore error: %d\n", GetLastError());
}
printf( "Semaphor created.\n" );
if( stacksize < 8192 )
stacksize = 8192;
else
stacksize = (stacksize/4096+1)*4096;
hThrd = _beginthread( thrdproc, // Definition of a thread entry
NULL,
stacksize,
"Thread 1");
if (hThrd == -1)
return RC_THREAD_NOT_CREATED);
*threadId1 = (HANDLE) hThrd;
hThrd = _beginthread( thrdproc, // Definition of a thread entry
NULL,
stacksize,
"Thread 2");
if (hThrd == -1)
return RC_THREAD_NOT_CREATED);
*threadId2 = (HANDLE) hThrd;
printf( "Main thread sleeps 5 sec.\n" );
sleep(5);
if( ! ReleaseSemaphore(semHandle, 1, NULL) )
printf("ReleaseSemaphore error: %d\n", GetLastError());
printf( "Semaphor released.\n" );
printf( "Main thread sleeps %d sec.\n", arg1 );
sleep (arg1);
if( ! ReleaseSemaphore(semHandle, 1, NULL) )
printf("ReleaseSemaphore error: %d\n", GetLastError());
printf( "Semaphor released.\n" );
printf( "Main thread sleeps %d sec.\n", arg1 );
sleep (arg1);
CloseHandle(semHandle);
printf( "Semaphor deleted.\n" );
printf( "Main thread sleeps 5 sec.\n" );
sleep (5);
printf( "Stop.\n" );
return OK;
}
void
thread_proc( void *pParam )
{
DWORD retVal;
printf( "\t%s created.\n", pParam );
retVal = WaitForSingleObject(semHandle, INFINITE);
if (retVal == WAIT_FAILED)
return RC_SEM_WAIT_ERROR;
printf( "\tSemaphor blocked by %s. (%lx)\n", pParam, retVal);
printf( "\t%s sleeps for 5 sec.\n", pParam );
sleep(5);
if( ! ReleaseSemaphore(semHandle, 1, NULL) )
printf("ReleaseSemaphore error: %d\n", GetLastError());
printf( "\tSemaphor released by %s.)\n", pParam);
}
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
void thread_proc (void * data);
pthread_mutexattr_t attr;
pthread_mutex_t mutex;
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t condition;
int semCount;
}sem_private_struct, *sem_private;
sem_private token;
int main( int argc, char **argv )
{
pthread_t threadId1;
pthread_t threadId2;
pthread_attr_t pthread_attr;
pthread_attr_t pthread_attr2;
int arg1;
int rc;
if( argc < 2 )
arg1 = 7;
else
arg1 = atoi( argv[1] );
printf( "Intra Process Semaphor test.\n" );
printf( "Start.\n" );
token =(sem_private) malloc (sizeof (sem_private_struct));
if(rc = pthread_mutex_init( &(token->mutex), NULL))
{
free(token);
return 1;
}
if(rc = pthread_cond_init(&(token->condition), NULL))
{
printf( "pthread_condition ERROR.\n" );
pthread_mutex_destroy( &(token->mutex) );
free(token);
return 1;
}
token->semCount = 0;
printf( "Semaphor created.\n" );
if (rc = pthread_attr_init(&pthread_attr))
{
printf( "pthread_attr_init ERROR.\n" );
exit;
}
if (rc = pthread_attr_setstacksize(&pthread_attr, 120*1024))
{
printf( "pthread_attr_setstacksize ERROR.\n" );
exit;
}
if (rc = pthread_create(&threadId1,
&pthread_attr,
(void*(*)(void*))thread_proc,
"Thread 1" ))
{
printf( "pthread_create ERROR.\n" );
exit;
}
if (rc = pthread_attr_init(&pthread_attr2))
{
printf( "pthread_attr_init2 ERROR.\n" );
exit;
}
if (rc = pthread_attr_setstacksize(&pthread_attr2, 120*1024))
{
printf( "pthread_attr_setstacksize2 ERROR.\n" );
exit;
}
if (rc = pthread_create(&threadId2,
&pthread_attr2,
(void*(*)(void*))thread_proc,
"Thread 2" ))
{
printf( "pthread_CREATE ERROR2.\n" );
exit ; // EINVAL, ENOMEM
}
printf( "Main thread sleeps 5 sec.\n" );
sleep( 5 );
if (rc = pthread_mutex_lock(&(token->mutex)))
{
printf( "pthread_mutex_lock ERROR 1.\n" );
return 1;
}
token->semCount ++;
if (rc = pthread_mutex_unlock&(token->mutex)))
{
printf( "pthread_mutex_unlock ERROR 1.\n" );
return 1;
}
if (rc = pthread_cond_signal(&(token->condition)))
{
printf( "pthread_cond_signal ERROR1.\n" );
return 1;
}
printf( "Semaphor released.\n" );
printf( "Main thread sleeps %d sec.\n", arg1 );
sleep( arg1 );
if (rc = pthread_mutex_lock(&(token->mutex)))
{
printf( "pthread_mutex_lock ERROR.\n" );
return 1;
}
token->semCount ++;
if (rc = pthread_mutex_unlock(&(token->mutex)))
{
printf( "pthread_mutex_lock ERROR.\n" );
return 1;
}
if (rc = pthread_cond_signal(&(token->condition)))
{
printf( "pthread_cond_signal ERROR.\n" );
return 1;
}
printf( "Semaphor released.\n" );
printf( "Main thread sleeps %d sec.\n", arg1 );
sleep( arg1 );
pthread_mutex_destroy(&(token->mutex));
pthread_cond_destroy(&(token->condition));
printf( "Semaphor deleted.\n" );
printf( "Main thread sleeps 5 sec.\n" );
sleep( 5 );
printf( "Stop.\n" );
return 0;
}
void
thread_proc( void *pParam )
{
int rc;
printf( "\t%s created.\n", pParam );
if (token == (sem_private) NULL)
return ;
if (rc = pthread_mutex_lock(&(token->mutex)))
{
printf( "pthread_mutex_lock ERROR2.\n" );
return ;
}
while (token->semCount <= 0)
{
rc = pthread_cond_wait(&(token->condition), &(token->mutex));
if (rc && errno != EINTR )
break;
}
if( rc )
{
pthread_mutex_unlock(&(token->mutex));
printf( "pthread_mutex_unlock ERROR3.\n" );
return;
}
token->semCount--;
if (rc = pthread_mutex_unlock(&(token->mutex)))
{
printf( "pthread_mutex_lock ERROR.\n" );
return ;
}
printf( "\tSemaphor blocked by %s. (%lx)\n", pParam, rc );
printf( "\t%s sleeps for 5 sec.\n", pParam );
sleep( 5 );
if (rc = pthread_mutex_lock(&(token->mutex)))
{
printf( "pthread_mutex_lock ERROR.\n" );
return ;
}
token->semCount ++;
if (rc = pthread_mutex_unlock(&(token->mutex)))
{
printf( "pthread_mutex_unlock ERROR.\n" );
return ;
}
if (rc = pthread_cond_signal(&(token->condition)))
{
printf( "pthread_cond_signal ERROR.\n" );
return ;
}
printf( "\tSemaphor released by %s. (%lx)\n", pParam, rc );
|
|
#include <stdio.h>
#include <windows.h>
#define WAIT_FOR_ENTER printf( "Press ENTER\n" );getchar()
int main()
{
HANDLE semaphore;
int nRet;
DWORD retVal;
SECURITY_ATTRIBUTES sec_attr;
printf( "Inter Process Semaphore test - Process 1.\n" );
printf( "Start.\n" );
sec_attr.nLength = sizeof( SECURITY_ATTRIBUTES );
sec_attr.lpSecurityDescriptor = NULL;
sec_attr.bInheritHandle = TRUE;
semaphore = CreateSemaphore( &sec_attr, 1, 65536, "456789" );
if( semaphore == (HANDLE) NULL )
return RC_OBJECT_NOT_CREATED;
printf( "Semaphore created. (%lx)\n", nRet );
WAIT_FOR_ENTER;
if( ! ReleaseSemaphore(semaphore, 1, NULL) )
return SEM_POST_ERROR;
printf( "Semaphore Posted. \n");
WAIT_FOR_ENTER;
retVal = WaitForSingleObject (semaphore, INFINITE );
if (retVal == WAIT_FAILED)
return SEM_WAIT_ERROR;
printf( "Wait for Semaphore. \n");
WAIT_FOR_ENTER;
CloseHandle (semaphore);
printf( "Semaphore deleted.\n" );
printf( "Stop.\n" );
return 0;
}
|
清单 26 给出了作为支持进程间共享的有名称信号示例的消息 IPC 代码。
清单 26. 相应的 Linux 进程间信号示例代码,进程 1
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#define WAIT_FOR_ENTER printf( "Press ENTER\n" );getchar()
struct msgbuf {
long mtype; /* type of message */
char mtext[1]; /* message text */
};
int main()
{
key_t msgKey;
int flag;
struct msgbuf buff;
int sem;
int nRet =0;
printf( "Inter Process Semaphore test - Process 1.\n" );
printf( "Start.\n" );
flag = IPC_CREAT|IPC_EXCL;
if( ( msgKey = (key_t) atol( "456789" ) ) <= 0 )
return 1;
flag |= S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
sem = (int) msgget( msgKey, flag );
if (sem == -1)
if( errno == EEXIST )
{
flag &= ~IPC_EXCL;
sem = (int) msgget( msgKey, flag );
if (msgctl(sem, IPC_RMID, NULL ) != 0)
return 1;
sem = (int) msgget( msgKey, flag );
if (sem == -1)
return 1;
}
else
return 1;
printf( "Semaphore created. \n" );
WAIT_FOR_ENTER;
buff.mtype = 123;
if( msgsnd( sem, &buff, 1, 0 ) < 0 )
return 1;
printf( "Semaphore Posted. \n" );
WAIT_FOR_ENTER;
if( msgrcv( sem, &buff, 1, 0, 0 ) < 0 )
return 1;
printf( "Wait for Semaphore. \n" );
WAIT_FOR_ENTER;
msgctl(sem, 0, IPC_RMID );
printf( "Semaphore deleted.\n" );
printf( "Stop.\n" );
return 0;
}
|
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE semaphore;
DWORD retVal;
printf( "Inter Process Semaphore test - Process 2.\n" );
printf( "Start.\n" );
SECURITY_ATTRIBUTES sec_attr;
sec_attr.nLength = sizeof( SECURITY_ATTRIBUTES );
sec_attr.lpSecurityDescriptor = NULL;
sec_attr.bInheritHandle = TRUE;
semaphore = CreateSemaphore( &sec_attr, 0, 65536, "456789" );
if( semaphore == (HANDLE) NULL )
return RC_OBJECT_NOT_CREATED;
printf( "Semaphore opened. (%lx)\n", nRet );
printf( "Try to wait for semaphore.\n" );
while( ( retVal = WaitForSingleObject( semaphore, 250 ) ) == WAIT_TIMEOUT)
printf( "Timeout. \n");
printf( "Semaphore acquired. \n");
printf( "Try to post the semaphore.\n" );
if( ! ReleaseSemaphore(semaphore, 1, NULL) )
return RC_SEM_POST_ERROR;
printf( "Semaphore posted. \n");
CloseHandle(semaphore);
printf( "Semaphore closed. \n");
printf( "Stop.\n" );
return 0;
}
|
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#define RC_TIMEOUT = 3
struct msgbuf {
long mtype; /* type of message */
char mtext[1]; /* message text */
};
int main()
{
key_t msgKey;
int flag=0;
struct msgbuf buff;
int sem;
int nRet =0;
printf( "Inter Process Semaphore test - Process 2.\n" );
printf( "Start.\n" );
if( ( msgKey = (key_t) atol( "456789" ) ) <= 0 )
return 1;
flag |= S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
sem = (int) msgget( msgKey, flag );
if (sem == -1)
if( errno == EEXIST )
{
flag &= ~IPC_EXCL;
sem = (int) msgget( msgKey, flag );
if (msgctl(sem, IPC_RMID, NULL ) != 0)
return 1;
sem = (int) msgget( msgKey, flag );
if (sem == -1)
return 1;
}
else
return 1;
printf( "Semaphore opened. (%lx)\n", nRet );
if( nRet != 0 )
return 0;
printf( "Try to wait for semaphore.\n" );
while( ( nRet = sem_shared_wait_timed( sem, 250 ) ) == 3)
printf( "Timeout. (%lx)\n", nRet );
printf( "Semaphore acquired. (%lx)\n", nRet );
printf( "Try to post the semaphore.\n" );
buff.mtype = 123;
if( msgsnd( sem, &buff, 1, 0 ) < 0 )
return 1;
printf( "Semaphore posted. (%lx)\n", nRet );
if( nRet != 0 )
return 0;
printf( "Semaphore closed. (%lx)\n", nRet );
printf( "Stop.\n" );
return 0;
}
int sem_shared_wait_timed( int sem, unsigned long timelimit)
{
struct msgbuf buff;
struct timeval timeOut;
int msg[1];
int nRet=0;
timeOut.tv_sec = timelimit / 1000;
timeOut.tv_usec = (timelimit % 1000) * 1000;
msg[0] = sem;
nRet = select( 0x1000, (fd_set *)msg, NULL, NULL, &timeOut );
if(nRet == 0)
return 3;
if( msgrcv( sem, &buff, 1, 0, 0 ) < 0 )
return 1;
}
|
|
本系列的第三篇文章从信号 API 的角度讲述了从 Win32 到 Linux 的映射,并给出了您可以引用的信号示例代码。线程化的、同步的系统所提出的挑战不仅是设计与实现,也包括了质量保证的所有阶段。当进行从 Win32 到 Linux 的迁移时,可以将这些文章做为参考。一定要去阅读本系列中以前的文章。
Microsoft、Windows、Windows NT 和 Windows 徽标是 Microsoft Corporation 在美国和/或其他国家或地区的商标或注册商标。
Intel、Intel Inside(logos)、MMX 和 Pentium 是 Intel 公司在美国和/或其他国家或地区的商标。
UNIX 是 The Open Group 在美国和其他国家或地区的注册商标。
Linux 是 Linus Torvalds 在美国和/或其他国家或地区的商标。
|
|
Nam Keung 是 IBM 的一名高级程序员,他曾致力于 AIX 通信开发、AIX 多媒体、SOM/DSOM 开发和 Java 性能方面的工作。他目前的工作包括帮助独立软件提供商(Independent Software Vendors,ISV)进行应用程序设计、部署应用程序、性能调优和关于 pSeries 平台的教育。您可以通过 namkeung@us.ibm.com 与 Nam 联系。 | |
Level: Advanced
Srinivasan S. Muthuswamy (smuthusw@in.ibm.com), Software Engineer, IBM
Kavitha Varadarajan (vkavitha@in.ibm.com), Software Engineer, IBM
25 Aug 2005
The wave of migration to open source in business has the potential to cause a tremendous porting traffic jam as developers move the pervasive Windows® applications to the Linux™ platform. In this three-part series, you get a mapping guide, complete with examples, to ease your transition from Windows to Linux. This part takes a look at mutexes, critical sections, and wait functions.
Today, many global businesses and services are going open source -- all the major corporate players in the industry are pushing for it. This trend has spurred a major migration exercise in which lots of existing products maintained for various platforms (Windows, OS2, Solaris, etc.) will be ported to open source Linux platforms.
Many applications are designed without considering the need to port them to Linux. This has the potential to be a porting nightmare, but it doesn't have to be. The goal of this series of articles is to help you migrate complex applications involving IPC and threading primitives from Windows to Linux. We will share our experiences in moving these critical Windows IPC applications, applications that include multithreaded apps that require thread syncronization and multiprocess apps that require interprocess syncronization.
In short, this series can be called a mapping document -- it provides mapping of various Windows calls to Linux calls related to threads, processes, and interprocess communication elements (mutexes, semaphores, etc.). To create easily digestible chunks, we've divided the series into three articles:
Let's finish our Windows-to-Linux mapping guide by starting with mutexes.
A mutex (which stands for "mutual exclusion" lock) is a locking or synchronization object that allows multiple threads to synchronize access to shared resources. It is often used to ensure that shared variables are always seen by other threads in a consistent state.
In Windows, the mutexes are both named and un-named. The named mutex is shared between the threads of different process.
In Linux, the mutexes are shared only between the threads of the same process. To achieve the same functionality in Linux, a System V semaphore can be used (see Resources for a link to Part 2 of this series).
In Windows, wait functions are used to request the ownership of the mutex. There are different types of wait functions available -- the one we're using as an example is WaitForSingleObject().
The following points should be considered in the mapping process of a mutex:
| Table 1. Mutex mapping | |||
| Windows | Linux threads | Linux process | Classification |
|---|---|---|---|
CreateMutex
| pthreads_mutex_init
| semget
| context specific |
OpenMutex
| not applicable | semget
| context specific |
WaitForSingleObject
| pthread_mutex_lock
| semop
| context specific |
ReleaseMutex
| pthread_mutex_unlock
| semop
| context specific |
CloseHandle
| pthread_mutex_destroy
| semctl
| context specific |
In Windows, CreateMutex() is used to create or open a named or un-named mutex object. Named mutexes are mainly used to provide synchronization between processess: HANDLE CreateMutex (LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCTSTR lpName). In this code:
lpMutexAttributes is a pointer to the structure that determines whether the handle can be inherited by the child process or not. If this attribute is null, the handle cannot be inherited.
bInitialOwner is a boolean value and if this value is TRUE then the calling thread initially owns the mutex.
lpName is a pointer to the name of the semaphore. If null, then un-named semaphore is created. In Windows, OpenMutex() is used to open the named mutex. This function returns the handle of the mutex.
HANDLE OpenMutex( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName ) |
In the code:
dwDesiredAccess is the desired access for the user requesting for the mutex object.
bInheritHandle is a flag and if true, related process can inherit the handle.
lpName is the name of the mutex (and is case sensitive). Notice in this code that the named mutex should have been created already.
In Linux, the pthread library call pthread_mutex_init() is used to create the mutex: int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr).
There are three kinds of mutexes in Linux, each type determined by what happens if a thread attempts to lock a mutex it already owns with pthread_mutex_lock():
pthread_mutex_lock() the calling thread suspends forever.
pthread_mutex_lock() returns immediately with a success return code. This is used as equivalent for a Windows mutex since it is recursive in nature.
pthread_mutex_lock() returns immediately with the error code EDEADLK. The mutex kind can be set in two ways. The static way of setting is as follows:
/* Fast */ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* Recursive */ pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; /* Errorcheck */ pthread_mutex_t errchkmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
Another way of setting mutex kind is by using a mutex attribute object. To do this, pthread_mutexattr_init() is called to initialize the object followed by pthread_mutexattr_settype() which sets the kind of the mutex.
int pthread_mutexattr_init(pthread_mutexattr_t *attr); int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind); |
The parameter kind takes the following values:
PTHREAD_MUTEX_FAST_NP
PTHREAD_MUTEX_RECURSIVE_NP
PTHREAD_MUTEX_ERRORCHECK_NP The attribute can be destroyed using pthread_mutexattr_destroy(): int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);.
Setting the initial state of the mutex
In Linux the initial state of the mutex cannot be set using the pthread_mutex_init() call. This can be achieved by following steps:
pthread_mutex_init().
pthread_mutex_lock(). In Windows wait funtions provide the facility to acquire the synchronization objects. There are different types of wait functions -- in this section we're using WaitForSingleObject(). This function takes the handle to the mutex object and waits until it is signaled or timeout occurs.
DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds ); |
In this code:
hHandle is the pointer to the mutex handle.
dwMilliseconds is the timeout value in milliseconds. If the value is INFINITE then it blocks the calling thread/process indefinitely. In Linux, the pthread library call pthread_mutex_lock() / pthread_mutex_trylock() is used to acquire the mutex.
int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); |
pthread_mutex_lock() is a blocking call which means that if the mutex is already locked by another thread, pthread_mutex_lock() suspends the calling thread until the mutex is unlocked. On the other hand, pthread_mutex_trylock() returns immediately if the mutex is already locked by another thread. Remember that in Linux, the timeout option is not available. This can be achieved by issuing a non-blocking pthread_mutex_trylock() call along with a delay in a loop which counts the timeout value.
In Windows the function ReleaseMutex() releases the ownership of the mutex and sets the mutex to the signaled state: BOOL ReleaseMutex(HANDLE hMutex). In this code, hMutex is the handle of the mutex.
Notice that mutexes in Windows are basically recursive mutexes -- if the thread which already owns the mutex tries to acquire ownership again, the wait function returns without blocking and deadlock is avoided.
Linux uses pthread_mutex_unlock() to release/unlock the mutex: int pthread_mutex_unlock(pthread_mutex_t *mutex);.
The mutex functions are not asynchronous signal-safe and should not be called from a signal handler. In particular, calling pthread_mutex_lock or pthread_mutex_unlock from a signal handler may deadlock the calling thread.
In Windows, CloseHandle() is used to close or destroy the mutex object.
BOOL CloseHandle( HANDLE hObject ); |
In the code, hObject is the pointer to the handle to the synchronization object.
In Linux, pthread_mutex_destroy() destroys a mutex object, freeing the resources it might hold. It also checks to determine whether the mutex is unlocked at that time: int pthread_mutex_destroy(pthread_mutex_t *mutex).
In Windows, named mutexes are mainly used between processes to achieve synchronization (to access the shared resource in a mutually exclusive manner). The mutex provided by the Linux threads libraries are limited to the threads of the same process. To achieve the same functionality between processes in Linux, a System V semaphore can be used.
System V semaphores are count variables. To achieve the same function as the Windows named mutex, the initial count of the semaphore is set to 0 using semctl() function. To acquire mutually exclusive access to the shared resource, semop() is used with sem_op value as -1. The calling process is blocked until mutually exclusive access is released. The creating process can acquire the mutually exclusive access by creating a semaphore with the initial count as 0 using semctl() function. After using the shared resource, the semaphore count can be set to 1 by using semop() function, allowing the other processes to access the shared resource. (See Resources for a link to the Part 2 section on semaphores.)
Following is code samples dealing with mutexes. Listings 13 and 14 show two scenarios each. In the first, a mutex is used without a specified timeout value. In the second, a mutex is used with a timeout value of two seconds.
Listing 1. Windows example for un-named mutex
HANDLE hMutexWithNoTimeOut, hMutexWithTimeOut;
DWORD dwRetCode;
// create a mutex
hMutexWithNoTimeOut = CreateMutex(
NULL, // no security attriutes
FALSE, // initially not owned
NULL); // un named mutex so NULL
hMutexWithTimeOut = CreateMutex(
NULL, // no security attriutes
FALSE, // initially not owned
NULL); // un named mutex so NULL
// acquire a mutex
dwRetCode = WaitForSingleObject(
hMutexWithNoTimeOut, // Mutex handle
INFINITE); // Infinite wait
if (dwRetCode == WAIT_OBJECT_0) {
// success
// access the shared resource
.....
// release mutex
ReleaseMutex(hMutexWithNoTimeOut); // Mutex Handle
}
// case 2, using mutex with timeout specified.
dwRetCode = WaitForSingleObject(
hMutexWithTimeOut,
2000L); // 2 secs timeout
switch(dwRetCode) {
case WAIT_OBJECT_0 :
// success
// access the shared resource
.....
// After using the shared resource, release the semaphore
ReleaseMutex(hMutexWithTimeOut);
....
break;
case WAIT_TIMEOUT :
// Handle the timeout case
...
break;
case WAIT_ABANDONED :
// probe for abandoned mutex
break;
}
....
// close all the mutex
CloseHandle(hMutexWithTimeout);
CloseHandle(hMutexWithNoTimeout);
|
#define TIMEOUT 200 // 2 Secs delay time
struct timespec delay; // structure for providing timeout
pthread_mutexattr_t mutexattr; // Mutex Attribute
pthread_mutex_t mutexWithNoTimeOut, mutexWithTimeOut; // Mutex variables
// Set the mutex as a recursive mutex
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
// Create the mutex with the attributes set
pthread_mutex_init(&mutexWithNoTimeOut, &mutexattr);
pthread_mutex_init(&mutexWithTimeOut, &mutexattr);
// destroy the attribute
pthread_mutexattr_destroy(&mutexattr)
// Lock/Acquire the mutex and access the shared resource
pthread_mutex_lock (&mutexWithNoTimeOut);
// access the shared resource
.. ...
// Unlock the mutex
pthread_mutex_unlock (&mutexWithNoTimeOut);
...
// Case 2, Accessing share resource with time out value specified in the
// Mutex call
while (timeout < TIMEOUT ) {
delay.tv_sec = 0;
delay.tv_nsec = 1000000; // 1 milli sec delay
// Tries to acquire the mutex and access the shared resource,
// if success, access the shared resource,
// if the shared reosurce already in use, it tries every 1 milli sec
// to acquire the resource
// if it does not acquire the mutex within 2 secs delay,
// then it is considered to be failed
irc = pthread_mutex_trylock(&mutexWithTimeOut);
if (!irc) {
// Acquire mutex success
// Access the shared resource
// Unlock the mutex and release the shared resource
pthread_mutex_unlock (&mutexWithTimeOut);
break;
}
else {
// check whether somebody else has the mutex
if (irc == EPERM ) {
// Yes, Resource already in use so sleep
nanosleep(&delay, NULL);
timeout++ ;
}
else{
// Handle error condition
}
}
}
// Close all the mutex
pthread_mutex_destroy (&mutexWithNoTimeOut);
pthread_mutex_destroy (&mutexWithTimeOut);
|
// Process 1 HANDLE hMutex; DWORD dwRetCode; // create a mutex hMutex = CreateMutex( NULL, // no security attriutes FALSE, // initially not owned NULL); // un named mutex so NULL // acquire a mutex dwRetCode = WaitForSingleObject( hMutex, // Mutex handle INFINITE); // Infinite wait if (dwRetCode == WAIT_OBJECT_0) { // success // access the shared resource ..... // release mutex ReleaseMutex(hMutex); // Mutex Handle } // close the mutex CloseHandle(hMutex); // Process 2 HANDLE hMutex; DWORD dwRetCode; // Open the mutex created by the Process 1 hMutex = OpenMutex( NULL, // no security attriutes NULL, // handle cannot be inhereited "testMuex"); // named mutex // acquire a mutex dwRetCode = WaitForSingleObject( hMutex, // Mutex handle INFINITE); // Infinite wait if (dwRetCode == WAIT_OBJECT_0) { // success // access the shared resource ..... // release mutex ReleaseMutex(hMutex); // Mutex Handle } // close the mutex CloseHandle(hMutex); |
// Process 1 #define TIMEOUT 200 int main() { //Definition of variables key_t key; int semid; int Ret; int timeout = 0; struct sembuf operation[1] ; union semun { int val; struct semid_ds *buf; USHORT *array; } semctl_arg,ignored_argument; key = ftok(); //Generate a unique key or supply a value semid = semget(key, // a unique identifier to identify semaphore set 1, // number of semaphore in the semaphore set 0666 | IPC_CREAT // permissions (rwxrwxrwx) on the new //semaphore set and creation flag ); if(semid < 0) { printf("Create semaphore set failed "); Exit(1); } //Set Initial value for the resource semctl_arg.val = 1; //Setting semval to 1 semctl(semid, 0, SETVAL, semctl_arg); //Wait for Zero while(timeout < TIMEOUT) { delay.tv_sec = 0; delay.tv_nsec = 1000000; /* 1 milli sec */ //Call Wait for Zero with IPC_NOWAIT option,so it will be // non blocking operation[0].sem_op = -1; //Wait operation[0].sem_num = subset; operation[0].sem_flg = IPC_NOWAIT; ret = semop(semid, operation,1); if(ret < 0) { /* check whether somebody else has the mutex */ if (retCode == EPERM ) { /* sleep for delay time */ nanosleep(&delay, NULL); timeout++ ; } else { printf("ERROR while wait "); break; } } else { /*semaphore got triggered */ break; } } //Close semaphore iRc = semctl(semid, 1, IPC_RMID , ignored_argument); } // Process 2 int main() { int key = 0x20; //Process 2 shd know key value in order to open the // existing semaphore set struct sembuf operation[1] ; //Open semaphore semid = semget(key, 1, 0); operation[0].sem_op = 1; //Release the resource so Wait in process // 1 will be triggered operation[0].sem_num = 0; operation[0].sem_flg = SEM_UNDO; //Release semaphore semop(semid, operation,1); } |
|
In Windows, critical sections are synchronization objects that are similar to mutexes but with some limitations. The critical sections can only be used between threads of same process. A mutex uses timeout when requesting access to the mutex, but a critical section does not provide such feature -- it waits indefinitely.
The critical section uses the spin count. In the single-processor system, the spin count is ignored and initialized to 0, but in the multiprocessor system, the calling thread will spin dwSpinCount times before it actually waits for the critical section so that if the critical section becomes free during that time, the calling thread does not wait. Since the critical sections are used only between the threads of the same process, a pthreads mutex is used to achieve the same results on Linux systems.
| Table 2. Critical section mapping | ||
| Windows | Linux | Classification |
|---|---|---|
InitializeCriticalSection
| pthread_mutex_init
| mappable |
EnterCriticalSection
| pthread_mutex_lock
| mappable |
LeaveCriticalSection
| pthread_mutex_trylock
| mappable |
DeleteCriticalSection
| pthread_mutex_destroy
| mappable |
Creating/initializing a critical section
In Windows, critical sections need to be initialized before the threads of same process can actually be used. InitializeCriticalSection() or InitializeCriticalSectionAndSpinCount() can be used to initialize the critical section.
void InitializeCriticalSection( LPCRITICAL_SECTION lpCriticalSection ) |
In this code, lpCriticalSection is a pointer to the handle to the critical section. In low memory situations, this function raises the STATUS_NO_MEMORY exception.
InitializeCriticalSectionAndSpinCount() is used to initialize and set the spin count.
BOOL InitializeCriticalSectionAndSpinCount( LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount ) |
In this code:
lpCriticalSection is a pointer to the handle of the critical section.
dwSpinCount is the spin count for the critical section. In Linux, pthread_mutex_init() is used to create or initialize the mutex object.
Entering/acquiring a critical section
In Windows, EnterCriticalSection() or TryEnterCriticalSection() is used to request the ownership of the critical section. If the critical section is already in use, EnterCriticalSection() will block the calling thread, but TryEnterCriticalSection() will attempt to enter the critical section without blocking the thread.
EnterCriticalSection() is used to enter the critical section:
void EnterCriticalSection( LPCRITICAL_SECTION lpCriticalSection ); |
In this code, lpCriticalSection is a pointer to the handle of the critical section.
The TryEnterCriticalSection() function attempts to enter a critical section without blocking. If the call is successful, the calling thread takes ownership of the critical section:
BOOL TryEnterCriticalSection( LPCRITICAL_SECTION lpCriticalSection ) |
In this code, lpCriticalSection is the pointer to the handle of the critical section.
In Linux, the same can be achieved using pthread_mutex_lock() which blocks the calling thread. pthread_mutex_trylock attempts to acquire the mutex without blocking the thread.
Leaving/releasing a critical section
In Windows, LeaveCriticalSection() is used to release the ownership of the critical section. LeaveCriticalSection() needs to be called as many times as the critical section is entered.void LeaveCriticalSection( LPCRITICAL_SECTION lpCriticalSection )
In this code, lpCriticalSection is the pointer to the handle of the critical section.
In Linux, pthread_mutex_unlock() is used to release the ownership of the mutex object.
In Windows, DeleteCriticalSection() is used to delete the critical section; once used, this critical section can no longer be used for synchronization.
void DeleteCriticalSection( LPCRITICAL_SECTION lpCriticalSection ) |
In this code, lpCriticalSection is a pointer to the handle of the critical section.
In Linux, pthread_mutex_destroy() is used to delete the mutex object.
The following examples are very simple -- they present code snippets for accessing a shared resource using a critical section for mutual exclusion.
Listing 5. Windows critical section example
CRITICAL_SECTION csCriticalSection;
// Initialize a Critical Section
InitializeCriticalSection(
&csCriticalSection); // Critical Section Object
// Enter a critical Section
EnterCriticalSection(
&csCriticalSection); // Critical Section Object
// Access a shared resource
// Leave a Critical Section
LeaveCriticalSection(
&csCriticalSection); // Critical Section Object
// Delete a Critical Section
DeleteCriticalSection(
&csCriticalSection); // Critical Section Object
|
pthread_mutex_t mutex; // Mutex pthread_mutexattr_t mutexattr; // Mutex attribute variable // Set the mutex as a recursive mutex pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP); // create the mutex with the attributes set pthread_mutex_init(&mutex, &mutexattr); //After initializing the mutex, the thread attribute can be destroyed pthread_mutexattr_destroy(&mutexattr) // Acquire the mutex to access the shared resource pthread_mutex_lock (&mutex); // access the shared resource .. // Release the mutex and release the access to shared resource pthread_mutex_unlock (&mutex); ... // Destroy / close the mutex irc = pthread_mutex_destroy (&mutex); |
|
In Windows, the wait functions block the calling thread/process until the specified criteria is met -- in other words, they allow threads to block their own executions. The type of the wait function determines the set of wait criteria used. There are four types of wait functions:
We won't be addressing alertable or registered wait functions in this series.
In Linux, wait functions are provided in the respective synchronization library itself (mutexes and semaphores have their own wait functions.)
The following points should be considered when mapping wait functions:
| Table 3. Wait function mapping | |||
| Windows | Linux threads | Linux process | Classification |
|---|---|---|---|
SignalObjectAndWait
| semop
| semop
| context specific |
WaitForMultipleObjects
| sem_wait
| semop
| context specific |
SignalObjectAndWait() is also an alertable wait function and it is different as it signals an object and waits on another object in an atomic manner.
DWORD SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds, BOOL bAlertable ) |
In this code:
hObjectToSignal is a pointer to the handle to the object to be signaled.
hObjectToWaitOn is a pointer handle to the object for which the thread has to wait.
dwMilliseconds is the time out specified in milliseconds.
bAlertable is a flag and if this parameter is TRUE, then the wait function returns when the system queues an I/O completion routine or APC function and the thread calls the function. For this article we can ignore this flag
In Linux, the same functionality can be achieved by using System V semaphores. These semaphores provide function in which we can specify operation sets. To signal an object and wait for another synchronization object, we can create two operations sets -- one for signaling the object and other to wait on the specified object. The operations sets are performed in an atomic manner, meaning the semop() call succeeds if both the operations succeed; otherwise, it fails.
System V semaphores do not provide timeout functionality. This can be implemented in application logic as we discussed in Part 2 of this series by making semop() call with flag IPC_NOWAIT. By doing it this way, the calling thread or process is not blocked.
The following examples should illustrated the wait functions we've discussed.
Listing 7. Windows example for SignalObjectAndWait()
// Main thread HANDLE hEventOne; // Global Variable HANDLE hEventTwo; // Global Variable // Thread 1 DWORD dwRetCode; // Create Event One hEventOne = CreateEvent( NULL, // no security attributes TRUE, // Auto reset event FALSE, // initially set to non signaled state NULL); // un named event // Create Event Two hEventTwo = CreateEvent( NULL, // no security attributes TRUE, // Auto reset event FALSE, // initially set to non signaled state NULL); // un named event // Signal hEventOne and Wait for the hEventTwo to be signaled dwRetCode = SignalObjectAndWait( hEventOne, // Object to be signaled hEventTwo, // Object to wait on INFINITE, // Infinite wait FALSE); // Not alertable switch(dwRetCode) { case WAIT_OBJECT_O : // Event is signaled // go ahead and proceed the work default : // Probe for error } // Completed the job, // now close the event handle CloseHandle(hEventOne); CloseHandle(hEventTwo); // Thread 2 // Condition met for the event hEventTwo // now set the event SetEvent( hEventTwo); // Event Handle |
// Main thread int key = 0x20; // Semaphore key // Thread 1 struct sembuf operation[2] ; // Create 2 semaphores semid = semget(key, 2, 0666 | IPC_CREAT); operation[0].sem_op = 1; //Release first resource operation[0].sem_num = 0; operation[0].sem_flg = SEM_UNDO; operation[0].sem_op = -1; // Wait on the second resource operation[0].sem_num = 1; operation[0].sem_flg = SEM_UNDO; //Release semaphore 1 and wait on semaphore 2 // note : thread is suspended until the semaphore 2 is released. semop(semid, operation, 2); // thread is released // delete the semaphore semctl(semid, 0, IPC_RMID , 0) // Thread 2 struct sembuf operation[1] ; // open semaphore mysemid = semget(key, 2, 0); operation[0].sem_op = 1; // Release on the second resource operation[0].sem_num = 1; operation[0].sem_flg = SEM_UNDO; //Release semaphore 2 semop(semid, operation, 1); |
WaitForMultipleObjects() is the simplest function available in this type. This function takes an array on one or more synchronized objects as input and blocks the calling thread until any of the following criteria is met:
DWORD WaitForMultipleObjects( DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds ) |
In this code:
nCount is the number of object handles in the array pointed to by lpHandles.
lpHandles is a pointer to array of object handles.
bWaitAll is a flag and if this parameter is TRUE, the function waits until all the objects are in signaled state.
dwMilliseconds is the timeout value in milliseconds. In Linux, the same functionality can be achieved by using additional logic in the code. In the context of threads, POSIX semaphores are used and in the context of processes, System V semaphores are used. In Windows, if the bWaitAll flag is FALSE, the thread/process is released if any one of the synchronization objects are signaled. Linux does not provide this functionality. This logic needs to be handled in the application logic.
Following are examples for multiple objects wait functions.
Listing 9. Windows example for any single object to be signaled
HANDLE hEvents[2];
DWORD i, dwRetCode;
// Create two event objects.
for (i = 0; i < 2; i++)
{
hEvents[i] = CreateEvent(
NULL, // no security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
NULL); // unnamed object
}
// The creating thread waits for other threads or processes
// to signal the event objects.
dwRetCode = WaitForMultipleObjects(
2, // number of objects in array
hEvents, // array of objects
FALSE, // wait for any
INFINITE); // indefinite wait
// Return value indicates which event is signaled.
switch (dwEvent)
{
// hEvent[0] was signaled.
case WAIT_OBJECT_0 + 0:
// Perform tasks required by this event.
break;
// hEvent[1] was signaled.
case WAIT_OBJECT_0 + 1:
// Perform tasks required by this event.
break;
// Return value is invalid.
default:
// probe for error
}
|
// Semaphore sem_t semOne ; sem_t semTwo ; sem_t semMain ; // Main thread sem_init(semOne,0,0) ; sem_init(semTwo,0,0) ; sem_init(semMain,0,0) ; // create 2 threads each one waits on one semaphore // if signaled signals the main semaphore sem_wait(&semMain); // Thread 1 sem_wait(&semOne); sem_post(&semMain); // Thread 2 sem_wait(&semTwo); sem_post(&semMain); |
HANDLE hEvents[2];
DWORD i, dwRetCode;
// Create two event objects.
for (i = 0; i < 2; i++)
{
hEvents[i] = CreateEvent(
NULL, // no security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
NULL); // unnamed object
}
// The creating thread waits for other threads or processes
// to signal the event objects.
dwRetCode = WaitForMultipleObjects(
2, // number of objects in array
hEvents, // array of objects
TRUE, // wait for both the objects to be signaled
INFINITE); // indefinite wait
// Return value indicates which event is signaled.
switch (dwEvent)
{
// hEvent[0] and hEvent[1] were signaled.
case WAIT_OBJECT_0 :
// Perform tasks required by this event.
break;
// Return value is invalid.
default:
// probe for error
}
|
// Semaphore sem_t semOne ; sem_t semTwo ; sem_t semMain ; pthread_mutex_t mutMain = PTHREAD_MUTEX_INITIALIZER; // Main thread sem_init(semOne,0,0) ; sem_init(semTwo,0,0) ; sem_init(semMain,0,0) ; // create 2 threads each one waits on one semaphore // if signaled signals the main semaphore sem_wait(&semMain); // Thread 1 sem_wait(&semOne); // lock the Mutex pthread_mutex_lock(&mutMain); count ++; if(count == 2) { // semaphore semTwo is already signaled // so post the main semaphore sem_post(&semMain); } pthread_mutex_unlock(&mutMain); // Thread 2 sem_wait(&semTwo); // lock the Mutex pthread_mutex_lock(&mutMain); count ++; if(count == 2) { // semaphore semOne is already signaled // so post the main semaphore sem_post(&semMain); } pthread_mutex_unlock(&mutMain); |
// Main thread int key = 0x20; // Semaphore key // Thread 1 struct sembuf operation[2] ; // Create 2 semaphores semid = semget(key, 2, 0666 | IPC_CREAT); operation[0].sem_op = -1; // Wait on first resource operation[0].sem_num = 0; operation[0].sem_flg = SEM_UNDO; operation[0].sem_op = -1; // Wait on the second resource operation[0].sem_num = 1; operation[0].sem_flg = SEM_UNDO; // Wait on both the semaphores // Note : thread is suspended until both the semaphores are released. semop(semid, operation, 2); // thread is released // delete the semaphore semctl(semid, 0, IPC_RMID , 0) // Thread 2 struct sembuf operation[1] ; // open semaphore mysemid = semget(key, 2, 0); operation[0].sem_op = 1; // Release on the second resource operation[0].sem_num = 1; operation[0].sem_flg = SEM_UNDO; //Release semaphore 2 semop(semid, operation, 1); // Thread 3 struct sembuf operation[1] ; // open semaphore mysemid = semget(key, 2, 0); operation[0].sem_op = 1; // Release on the first resource operation[0].sem_num = 0; operation[0].sem_flg = SEM_UNDO; //Release semaphore 1 semop(semid, operation, 1); |
|
In this series, we've provided a guide to help map Windows processes to their functional counterparts in Linux.
In the first article we covered creating, terminating, and exiting a process; we've introduced wait functions (more about them in Part Three); and we've discussed environment variables. On the threads side, we've highlighted creation, parameter passing, specifying the function, setting the stack size, exiting, thread states, and changing priorities. And we addressed the differences in Windows and Linux of normal and time-critical threads and processes.
In the second article, we introduced synchronization objects, discussing semaphores -- creating, opening, acquiring, releasing, closing, and destroying them -- and event objects -- creating, opening, waiting on, signaling, resetting, closing, destroying, and named and un-named. In each section, we illustrated the difference between the functionality of each in Windows and in Linux.
In the last article of the series, we've defined and provided a mapping guide for mutexes, critical sections, and wait functions.
We hope this extensive mapping guide can lay the groundwork for your moving to Linux systems.
ResourcesLearn
|
|
Srinivasan S. Muthuswamy works as a Software Engineer for IBM Global Services Group. He joined IBM in 2000, and his expertise in programming reaches from scripting languages to object- and procedure-oriented languages on multiple platforms (Linux, Windows, WebSphere, Lotus, and so on). Muthuswamy has developed solutions ranging from system programming on Linux and Windows to Web solutions for J2EE. His primary focus is on integration and porting, and he holds a B.Eng. in Computer Engineering from the Government College of Technology, Coimbatore, India. You can contact him at <a href="mailto:smuthusw@in.ibm.com">smuthusw@in.ibm.com</a>. | |
|
|
Kavitha Varadarajan has worked as a software Engineer in the IBM India Software Lab from December 2000. Her work experience involves development and support of host-access client products such as PCOMM and networking software like the communication server. Varadarajan has experience with a migration project that involves porting object-oriented IPC Windows applications to Linux. She holds a B.Eng. in Computer Science and Engineering from Shanmugha College of Engineering, Tanjore, India. She can be contacted at <a href="mailto:vkavitha@in.ibm.com">vkavitha@in.ibm.com</a>. | |
级别: 初级
Nam Keung (namkeung@us.ibm.com), 高级程序员
Chakarat Skawratananond (chakarat@us.ibm.com), pSeries Linux 技术顾问
2005 年 1 月 06 日
本文的内容是 Win32 API(特别是进程、线程和共享内存服务)到 POWER 上 Linux 的映射。本文可以帮助您确定哪种映射服务最适合您的需要。作者向您详细介绍了他在移植 Win32 C/C++ 应用程序时遇到的 API 映射。
有很多方式可以将 Win32 C/C++ 应用程序移植和迁移到 pSeries 平台。您可以使用免费软件或者第三方工具来将 Win32 应用程序代码移到 Linux。在我们的方案中,我们决定使用一个可移植层来抽象系统 API 调用。可移植层将使我们的应用程序具有以下优势:
由于 Windows 环境与 pSeries Linux 环境有很大区别,所以进行跨 UNIX 平台的移植比进行从 Win32 平台到 UNIX 平台的移植要容易得多。这是可以想到的,因为很多 UNIX 系统都使用共同的设计理念,在应用程序层有非常多的类似之处。不过,Win32 API 在移植到 Linux 时是受限的。本文剖析了由于 Linux 和 Win32 之间设计的不同而引发的问题。
|
在 Win2K/NT 上,DLL 的初始化和终止入口点是 _DLL_InitTerm 函数。当每个新的进程获得对 DLL 的访问时,这个函数初始化 DLL 所必需的环境。当每个新的进程释放其对 DLL 的访问时,这个函数为那个环境终止 DLL。当您链接到那个 DLL 时,这个函数会自动地被调用。对应用程序而言,_DLL_InitTerm 函数中包含了另外一个初始化和终止例程。
在 Linux 上,GCC 有一个扩展,允许指定当可执行文件或者包含它的共享对象启动或停止时应该调用某个函数。语法是 __attribute__((constructor)) 或 __attribute__((destructor))。这些基本上与构造函数及析构函数相同,可以替代 glibc 库中的 _init 和 _fini 函数。
这些函数的 C 原型是:
void __attribute__ ((constructor)) app_init(void); void __attribute__ ((destructor)) app_fini(void); |
Win32 sample
_DLL_InitTerm(HMODULE modhandle, DWORD fdwReason, LPVOID lpvReserved)
{
WSADATA Data;
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
if (_CRT_init() == -1)
return 0L;
/* start with initialization code */
app_init();
break;
case DLL_PROCESS_DETACH:
/* Start with termination code*/
app_fini();
_CRT_term();
break;
…..
default:
/* unexpected flag value - return error indication */
return 0UL;
} return 1UL; /* success */
}
|
|
Win32 进程模型没有与 fork() 和 exec() 直接相当的函数。在 Linux 中使用 fork() 调用总是会继承所有内容,与此不同, CreateProcess() 接收用于控制进程创建方面的显式参数,比如文件句柄继承。
CreateProcess API 创建一个包含有一个或多个在此进程的上下文中运行的线程的新进程,子进程与父进程之间没有关系。在 Windows NT/2000/XP 上,返回的进程 ID 是 Win32 进程 ID。在 Windows ME 上,返回的进程 ID 是除去了高位(high-order bit)的 Win32 进程 ID。当创建的进程终止时,所有与此进程相关的数据都从内存中删除。
为了在 Linux 中创建一个新的进程, fork() 系统调用会复制那个进程。新进程创建后,父进程和子进程的关系就会自动建立,子进程默认继承父进程的所有属性。Linux 使用一个不带任何参数的调用创建新的进程。 fork() 将子进程的进程 ID 返回给父进程,而不返回给子进程任何内容。
Win32 进程同时使用句柄和进程 ID 来标识,而 Linux 没有进程句柄。
| Win32 | Linux |
| CreateProcess | fork() execv() |
| TerminateProcess | kill |
| ExitProcess() | exit() |
| GetCommandLine | argv[] |
| GetCurrentProcessId | getpid |
| KillTimer | alarm(0) |
| SetEnvironmentVariable | putenv |
| GetEnvironmentVariable | getenv |
| GetExitCodeProcess | waitpid |
在 Win32 中, CreateProcess() 的第一个参数指定要运行的程序,第二个参数给出命令行参数。CreateProcess 将其他进程参数作为参数。倒数第二个参数是一个指向某个 STARTUPINFORMATION 结构体的指针,它为进程指定了标准的设备以及其他关于进程环境的启动信息。在将 STARTUPINFORMATION 结构体的地址传给 CreateProcess 以重定向进程的标准输入、标准输出和标准错误之前,您需要设置这个结构体的 hStdin、hStdout 和 hStderr 成员。最后一个参数是一个指向某个 PROCESSINFORMATION 结构体的指针,由被创建的进程为其添加内容。进程一旦启动,它将包含创建它的进程的句柄以及其他内容。
Win32 example PROCESS_INFORMATION procInfo; STARTUPINFO startupInfo; typedef DWORD processId; char *exec_path_name char *_cmd_line; GetStartupInfo( &startupInfo ); // You must fill in this structure if( CreateProcess( exec_path_name, // specify the executable program _cmd_line, // the command line arguments NULL, // ignored in Linux NULL, // ignored in Linux TRUE, // ignored in Linux DETACHED_PROCESS | HIGH_PRIORITY_CLASS, NULL, // ignored in Linux NULL, // ignored in Linux &startupInfo, &procInfo)) *processId = procInfo.dwProcessId; else { *processId = 0; return RC_PROCESS_NOT_CREATED; } |
在 Linux 中,进程 ID 是一个整数。Linux 中的搜索目录由 PATH 环境变量(exec_path_name)决定。 fork() 函数建立父进程的一个副本,包括父进程的数据空间、堆和栈。 execv() 子例程使用 exec_path_name 将调用进程当前环境传递给新的进程。
这个函数用一个由 exec_path_name 指定的新的进程映像替换当前的进程映像。新的映像构造自一个由 exec_path_name 指定的正规的、可执行的文件。由于调用的进程映像被新的进程映像所替换,所以没有任何返回。
Equivalent Linux code #include <stdlib.h> #include <stdio.h> int processId; char *exec_path_name; char * cmd_line ; cmd_line = (char *) malloc(strlen(_cmd_line ) + 1 ); if(cmd_line == NULL) return RC_NOT_ENOUGH_MEMORY; strcpy(cmd_line, _cmd_line); if( ( *processId = fork() ) == 0 ) // Create child { char *pArg, *pPtr; char *argv[WR_MAX_ARG + 1]; int argc; if( ( pArg = strrchr( exec_path_name, '/' ) ) != NULL ) pArg++; else pArg = exec_path_name; argv[0] = pArg; argc = 1; if( cmd_line != NULL && *cmd_line != '\0' ) { pArg = strtok_r(cmd_line, " ", &pPtr); while( pArg != NULL ) { argv[argc] = pArg; argc++; if( argc >= WR_MAX_ARG ) break; pArg = strtok_r(NULL, " ", &pPtr); } } argv[argc] = NULL; execv(exec_path_name, argv); free(cmd_line); exit( -1 ); } else if( *processId == -1 ) { *processId = 0; free(cmd_line); return RC_PROCESS_NOT_CREATED; } |
在 Win32 进程中,父进程和子进程可能需要单独访问子进程所继承的由某个句柄标识的对象。父进程可以创建一个可访问而且可继承的副本句柄。Win32 示例代码使用下面的方法终止进程:
如果函数成功,则使用 TerminateThread 函数来释放同一进程上的主线程。然后使用 TerminateThread 函数来无条件地使一个进程退出。它启动终止并立即返回。
Win32 sample code if( thread != (HANDLE) NULL ) { HANDLE thread_dup; if( DuplicateHandle( OpenProcess(PROCESS_ALL_ACCESS, TRUE, processId), thread, GetCurrentProcess(), &thread_dup, //Output 0, FALSE, DUPLICATE_SAME_ACCESS )) { TerminateThread( thread_dup, 0); } } TerminateProcess( OpenProcess(PROCESS_ALL_ACCESS, TRUE, processId), (UINT)0 ); |
在 Linux 中,使用 kill 子例程发送 SIGTERM 信号来终止特定进程(processId)。然后调用设置 WNOHANG 位的 waitpid 子例程。这将检查特定的进程并终止。
Equivalent Linux code
pid_t nRet;
int status;
kill( processId, SIGTERM );
nRet = waitpid( processId, &status, WNOHANG); //Check specified
process is terminated
|
Win32 OpenProcess 返回特定进程(processId)的句柄。如果函数成功,则 GetExitCodeProcess 将获得特定进程的状态,并检查进程的状态是否是 STILL_ACTIVE。
Win 32 sample
HANDLE nProc;
DWORD dwExitCode;
nProc = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processId);
if ( nProc != NULL)
{
GetExitCodeProcess( nProc, &dwExitCode );
if (dwExitCode == STILL_ACTIVE )
return RC_PROCESS_EXIST;
else
return RC_PROCESS_NOT_EXIST;
}
else
return RC_PROCESS_NOT_EXIST;
|
在 Linux 中,使用 kill 子例程发送通过 Signal 参数指定的信号给由 Process 参数(processId)指定的特定进程。Signal 参数是一个 null 值,会执行错误检查,但不发送信号。
Equivalent Linux code
if ( kill ( processId, 0 ) == -1 && errno == ESRCH ) // No process can
be found
// corresponding to processId
return RC_PROCESS_NOT_EXIST;
else
return RC_PROCESS_EXIST;
|
|
线程 是系统分配 CPU 时间的基本单位;当等待调度时,每个线程保持信息来保存它的"上下文"。每个线程都可以执行程序代码的任何部分,并共享进程的全局变量。
构建于 clone() 系统调用之上的 LinuxThreads 是一个 pthreads 兼容线程系统。因为线程由内核来调度,所以 LinuxThreads 支持阻塞的 I/O 操作和多处理器。不过,每个线程实际上是一个 Linux 进程,所以一个程序可以拥有的线程数目受内核所允许的进程总数的限制。Linux 内核没有为线程同步提供系统调用。Linux Threads 库提供了另外的代码来支持对互斥和条件变量的操作(使用管道来阻塞线程)。
对有外加 LinuxThreads 的信号处理来说,每个线程都会继承信号处理器(如果派生这个线程的父进程注册了一个信号处理器的话。只有在 Linux Kernel 2.6 和更高版本中支持的新特性才会包含 POSIX 线程支持,比如 用于 Linux 的 Native POSIX Thread Library(NPTL)。
线程同步、等待函数、线程本地存储以及初始化和终止抽象是线程模型的重要部分。在这些之下,线程服务只负责:
| Win32 | Linux |
| _beginthread | pthread_attr_init pthread_attr_setstacksize pthread_create |
| _endthread | pthread_exit |
| TerminateThread | pthread_cancel |
| GetCurrentThreadId | pthread_self |
Win32 应用程序使用 C 运行期库,而不使用 Create_Thread API。使用了 _beginthread 和 _endthread 例程。这些例程会考虑任何可重入性(reentrancy)和内存不足问题、线程本地存储、初始化和终止抽象。
Linux 使用 pthread 库调用 pthread_create() 来派生一个线程。
threadId 作为一个输出参数返回。为创建一个新线程,要传递一组参数。当新线程被创建时,这些参数会执行一个函数。stacksize 用作新线程的栈的大小(以字节为单位),当新线程开始执行时,实际的参数被传递给函数。
进行创建的线程必须指定要执行的新线程的启动函数的代码。启动地址是 threadproc 函数(带有一个单独的参数,即 threadparam)的名称。如果调用成功地创建了一个新线程,则返回 threadId。Win32 threadId 的类型定义是 HANDLE。Linux threadId 的类型定义是 pthread_t。
在 Win32 中,线程的栈由进程的内存空间自动分配。系统根据需要增加栈的大小,并在线程终止时释放它。在 Linux 中,栈的大小在 pthread 属性对象中设置;pthread_attr_t 传递给库调用 pthread_create()。
Win32 sample int hThrd; DWORD dwIDThread; unsigned stacksize; void *thrdparam; //parameter to be passed to the thread when it //begins execution HANDLE *threadId; if( stacksize < 8192 ) stacksize = 8192; else stacksize = (stacksize/4096+1)*4096; hThrd = _beginthread( thrdproc, // Definition of a thread entry //point NULL, stacksize, thrdparam); if (hThrd == -1) return RC_THREAD_NOT_CREATED); *threadId = (HANDLE) hThrd; __________________________________________________________________ Equivalent Linux code #include <pthread.h> pthread_t *threadId; void thrdproc (void *data); //the thread procedure (function) to //be executed. //It receives a single void parameter void *thrdparam; //parameter to be passed to the thread when it //begins execution pthread_attr_t attr; int rc = 0; if (thrdproc == NULL || threadId == NULL) return RC_INVALID_PARAM); if (rc = pthread_attr_init(&attr)) return RC_THREAD_NOT_CREATED); // EINVAL, ENOMEM if (rc = pthread_attr_setstacksize(&attr, stacksize)) return RC_THREAD_NOT_CREATED); // EINVAL, ENOSYS if (rc = pthread_create(threadId, &attr, (void*(*)(void*))thrdproc, thrdparam)) return RC_THREAD_NOT_CREATED); // EINVAL, EAGAIN |
在 Win32 中,一个线程可以使用 TerminateThread 函数终止另一个线程。不过,线程的栈和其他资源将不会被收回。如果线程终止自己,则这样是可取的。在 Linux 中,pthread_cancel 可以终止由具体的 threadId 所标识的线程的执行。
| Win32 | Linux |
| TerminateThread((HANDLE *) threadId, 0); | pthread_cancel(threadId); |
在 Linux 中,线程默认创建为可合并(joinable)状态。另一个线程可以使用 pthread_join() 同步线程的终止并重新获得终止代码。可合并线程的线程资源只有在其被合并后才被释放。
Win32 使用 WaitForSingleObject() 来等待线程终止。
Linux 使用 pthread_join 完成同样的事情。
| Win32 | Linux |
| unsigned long rc; rc = (unsigned long) WaitForSingleObject (threadId, INIFITE); | unsigned long rc=0; rc = pthread_join(threadId, void **status); |
在 Win32 中,使用 _endthread() 来结束当前线程的执行。在 Linux 中,推荐使用 pthread_exit() 来退出一个线程,以避免显式地调用 exit 例程。在 Linux 中,线程的返回值是 retval,可以由另一个线程调用 pthread_join() 来获得它。
| Win32 | Linux |
| _endthread(); | pthread_exit(0); |
在 Win32 进程中,GetCurrentThreadId 函数获得进行调用的线程的线程标识符。Linux 使用 pthread_self() 函数来返回进行调用的线程的 ID。
| Win32 | Linux |
| GetCurrentThreadId() | pthread_self() |
sleep 服务用于 Win32 Sleep 函数的时间段的单位是毫秒,可以是 INFINITE,在这种情况下线程将永远不会再重新开始。 Linux sleep 函数类似于 Sleep,但是时间段以秒来计。要获得毫秒级的精度,则使用 nanosleep 函数来提供同样的服务。
| Win32 | Equivalent Linux code |
| Sleep (50) | struct timespec timeOut,remains; timeOut.tv_sec = 0; timeOut.tv_nsec = 500000000; /* 50 milliseconds */ nanosleep(&timeOut, &remains); |
Win32 SleepEx 函数挂起 当前线程,直到下面事件之一发生:
Linux 使用 sched_yield 完成同样的事情。
| Win32 | Linux |
| SleepEx (0,0) | sched_yield() |
|
共享内存允许多个进程将它们的部分虚地址映射到一个公用的内存区域。任何进程都可以向共享内存区域写入数据,并且数据可以由其他进程读取或修改。共享内存用于实现进程间通信媒介。不过,共享内存不为使用它的进程提供任何访问控制。使用共享内存时通常会同时使用"锁"。
一个典型的使用情形是:
| Win32 | Linux |
| CreateFileMaping, OpenFileMapping | mmap shmget |
| UnmapViewOfFile | munmap shmdt |
| MapViewOfFile | mmap shmat |
Win32 通过共享的内存映射文件来创建共享内存资源。Linux 使用 shmget/mmap 函数通过直接将文件数据合并入内存来访问文件。内存区域是已知的作为共享内存的段。
文件和数据也可以在多个进程和线程之间共享。不过,这需要进程或线程之间同步,由应用程序来处理。
如果资源已经存在,则 CreateFileMapping() 重新初始化共享资源对于进程的约定。如果没有足够的空闲内存来处理错误的共享资源,此调用可能会失败。 OpenFileMapping() 需要共享资源必须已经存在;这个调用只是请求对它的访问。
在 Win32 中,CreateFileMapping 不允许您增加文件大小,但是在 Linux 中不是这样。在 Linux 中,如果资源已经存在,它将被重新初始化。它可能被销毁并重新创建。Linux 创建可以通过名称访问的共享内存。 open() 系统调用确定映射是否可读或可写。传递给 mmap() 的参数必须不能与 open() 时请求的访问相冲突。 mmap() 需要为映射提供文件的大小(字节数)。
对 32-位内核而言,有 4GB 虚地址空间。最前的 1 GB 用于设备驱动程序。最后 1 GB 用于内核数据结构。中间的 2GB 可以用于共享内存。当前,POWER 上的 Linux 允许内核使用 4GB 虚地址空间,允许用户应用程序使用最多 4GB 虚地址空间。
| Win32 | Linux |
| PAGE_READONLY | PROT_READ |
| PAGE_READWRITE | (PROT_READ | PROT_WRITE) |
| PAGE_NOACCESS | PROT_NONE |
| PAGE_EXECUTE | PROT_EXEC |
| PAGE_EXECUTE_READ | (PROT_EXEC |PROT_READ) |
| PAGE_EXECUTE_READWRITE | (PROT_EXEC | PROT_READ | PROT_WRITE) |
要获得 Linux 共享内存的分配,您可以查看 /proc/sys/kernel 目录下的 shmmax、shmmin 和 shmall。
在 Linux 上增加共享内存的一个示例:
echo 524288000 > /proc/sys/kernel/shmmax |
下面是创建共享内存资源的 Win32 示例代码,以及相对应的 Linux nmap 实现。
Win32 sample code typedef struct { // This receives a pointer within the current process at which the // shared memory is located. // The same shared memory may reside at different addresses in other // processes which share it. void * location; HANDLE hFileMapping; }mem_shared_struct, *mem_shared, *token; mem_shared_struct *token; if ((*token = (mem_shared) malloc(sizeof(mem_shared_struct))) == NULL) return RC_NOT_ENOUGH_MEMORY; if (newmode == new_shared_create) (*token)->hFileMapping = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL, PAGE_READWRITE, 0, (DWORD) size, (LPSTR) name); else (*token)->hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPSTR) name); if ((*token)->hFileMapping == NULL) { free( *token ); return RC_SHM_NOT_CREATED ); } (*token)->location = MapViewOfFile((*token)->hFileMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); if ((*token)->location == NULL) { CloseHandle((*token)->hFileMapping); free(*token); return RC_OBJECT_NOT_CREATED; } ____________________________________________________________________ Equivalent Linux code typedef struct { void *location; int nFileDes; cs_size nSize; char *pFileName; }mem_shared_struct, *mem_shared, token; mode_t mode=0; int flag=0; int i, ch='\0'; char name_buff[128]; if (newmode == new_shared_create) flag = O_CREAT; else if (newmode != new_shared_attach) return RC_INVALID_PARAM; if ((*token = (mem_shared) malloc(sizeof(mem_shared_struct))) == NULL) return RC_NOT_ENOUGH_MEMORY; strcpy(name_buff, "/tmp/" ); strcat(name_buff, name ); if(((*token)->pFileName = malloc(strlen(name_buff)+1)) == NULL ) { free(*token); return RC_NOT_ENOUGH_MEMORY; } mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; flag |= O_RDWR; if(newmode == new_shared_create) remove(name_buff); if(((*token)->nFileDes = open(name_buff, flag, mode)) < 0) { free((*token)->pFileName); free(*token); return RC_OBJECT_NOT_CREATED; } if(newmode == new_shared_create) { lseek((*token)->nFileDes, size - 1, SEEK_SET); write((*token)->nFileDes, &ch, 1); } if(lseek((*token)->nFileDes, 0, SEEK_END) < size) { free((*token)->pFileName); free(*token); return RC_MEMSIZE_ERROR; } (*token)->location = mmap( 0, size, PROT_READ | PROT_WRITE, MAP_VARIABLE | MAP_SHARED, (*token)->nFileDes, 0); if((int)((*token)->location) == -1) { free((*token)->pFileName); free(*token); return RC_OBJECT_NOT_CREATED; } (*token)->nSize = size;strcpy((*token)->pFileName, name_buff); |
为销毁共享内存资源,munmap 子例程要取消被映射文件区域的映射。munmap 子例程只是取消对 mmap 子例程的调用而创建的区域的映射。如果某个区域内的一个地址被 mmap 子例程取消映射,并且那个区域后来未被再次映射,那么任何对那个地址的引用将导致给进程发出一个 SIGSEGV 信号。
| Win32 | 等价的 Linux 代码 |
| UnmapViewOfFile(token->location); CloseHandle(token->hFileMapping); | munmap(token->location, token->nSize); close(token->nFileDes); remove(token->pFileName); free(token->pFileName); |
|
本文介绍了关于初始化和终止、进程、线程及共享内存服务从 Win32 API 到 POWER 上 Linux 的映射。这绝对没有涵盖所有的 API 映射,而且读者只能将此信息用作将 Win32 C/C++ 应用程序迁移到 POWER Linux 的一个参考。
IBM、eServer 和 pSeries 是 IBM Corporation 在美国和/或其它国家或地区的商标。
UNIX 是 The Open Group 在美国和其它国家或地区的注册商标。
Microsoft 和 Windows 是 Microsoft Corporation 在美国和/或其它国家或地区的商标或注册商标。
所有其他商标和注册商标是它们相应公司的财产。
此出版物/说明是在美国完成的。IBM 可能不在其他国家或地区提供在此讨论的产品、程序、服务或特性,而且信息可能会不加声明地加以修改。有关您当前所在区域的产品、程序、服务和特性的信息,请向您当地的 IBM 代表咨询。任何对 IBM 产品、程序、服务或者特性的引用并非意在明示或暗示只能使用 IBM 的产品、程序、服务或者特性。只要不侵犯 IBM 的知识产权,任何同等功能的产品、程序、服务或特性,都可以代替 IBM 产品、程序、服务或特性。
涉及非 IBM 产品的信息可从这些产品的供应商、其出版说明或其他可公开获得的资料中获取,并不构成 IBM 对此产品的认可。非 IBM 价目及性能数字资源取自可公开获得的信息,包括供应商的声明和供应商的全球主页。 IBM 没有对这些产品进行测试,也无法确认其性能的精确性、兼容性或任何其他关于非 IBM 产品的声明。有关非 IBM 产品性能的问题应当向这些产品的供应商提出。
有关非 IBM 产品性能的问题应当向这些产品的供应商提出。IBM 公司可能已拥有或正在申请与本说明中描述的内容有关的各项专利。提供本说明并未授予用户使用这些专利的任何许可。您可以用书面方式将许可查询寄往: IBM Director of Licensing IBM Corporation North Castle Drive Armonk, NY 10504-1785 U.S.A。所有关于 IBM 未来方向或意向的声明都可随时更改或收回,而不另行通知,它们仅仅表示了目标和意愿而已。联系您本地的 IBM 办公人员或者 IBM 授权的转销商,以获得特定的 Statement of General Direction 的全文。
本说明中所包含的信息没有提交给任何正式的 IBM 测试,而只是"按原样"发布。虽然 IBM 可能为了其在特定条件下的精确性而已经对每个条目进行了检查,但不保证在其他地方可以获得相同的或者类似的结果。使用此信息或者实现这里所描述的任何技术是客户的责任,取决于客户评价并集成它们到客户的操作环境的能力。尝试为他们自己的环境而修改这些技术的客户,这样做所带来的风险由他们自行承担。
|
|
Nam Keung 是一名高级程序员,他曾致力于 AIX 通信开发、AIX 多媒体、SOM/DSOM 开发和 Java 性能方面的工作。他目前的工作包括帮助 ISV 进行应用程序设计、部署应用程序、性能调优和关于 pSeries 平台的教育。他从 1987 年起就是 IBM 的程序员了。您可以通过 namkeung@us.ibm.com 与 Nam 联系。 | |
|
|
Chakarat Skawratananond 是 IBM eServer Solutions Enablement 组织的一名技术顾问,在那里,他帮助独立软件开发商在 IBM pSeries 平台上使用他们的用于 AIX 5L 和 Linux 的应用程序。您可以通过 chakarat@us.ibm.com 与他联系。 | |
级别: 初级
Srinivasan S. Muthuswamy (smuthusw@in.ibm.com), 软件工程师, IBM Global Services Group
Kavitha Varadarajan (vkavitha@in.ibm.com), 软件工程师, IBM India Software Lab
2005 年 6 月 27 日
随着开发人员将一些普及的 Windows® 应用程序迁移到 Linux™ 平台,企业中正在进行的向开放源码迁移的浪潮有可能引发极大的移植问题。这个由三部分组成的系列文章提供了一个映射指南,并附有一些例子,以简化从 Windows 到 Linux 的转移。本文是系列文章的第 2 部分,将介绍两种同步对象类型:信号量和事件。
当前,很多全球商务和服务都正在走向开源 —— 业界的所有主要参与者都在争取实现此目标。这一趋势催生了一个重要的迁移模式:为不同平台(Windows、OS2、Solaris 等)维持的许多现有产品都将被移植到开放源码的 Linux 平台。
很多应用程序在设计时并未考虑到需要将它们移植到 Linux。这有可能使移植成为一件痛苦的事情,但并非绝对如此。本系列文章的目的是,帮助您将涉及到 IPC 和线程原语的复杂应用程序从 Windows 迁移到 Linux。我们与您分享迁移这些关键应用程序的经验,其中包括要求线程同步的多线程应用程序以及要求进程间同步的多进程应用程序。
简言之,可以将此系列文章看作是一个映射文档 —— 它提供了与线程、进程和进程间通信元素(互斥、信号量等等)相关的各种 Windows 调用到 Linux 调用的映射。我们将那些映射分为三部分:
在本文中,我们将从同步技术入手,继续从 Windows 到 Linux 的映射指导。
在 Windows 上,同步是使用等待函数中的同步对象来实现的。同步对象可以有两种状态:有信号(signaled)状态和无信号(non-signaled)状态。当在一个等待函数中使用同步对象时,等待函数就会阻塞调用线程,直到同步对象的状态被设置为有信号为止。
下面是在 Windows 上可以使用的一些同步对象:
在 Linux 中,可以使用不同的同步原语。Windows 与 Linux 的不同之处在于每个原语都有自己的等待函数(所谓等待函数就是用来修改同步原语状态的函数);在 Windows 中,有一些通用的等待函数来实现相同的目的。以下是 Linux 上可以使用的一些同步原语:
通过使用上面列出的这些原语,各种库都可以用于 Linux 之上,以提供同步机制。
| Windows | Linux —— 线程 | Linux —— 进程 |
互斥
| 互斥 - pthread 库
| System V 信号量
|
临界区
| 互斥 - pthread 库
| 不适用,因为临界区只用于同一进程的不同线程之间
|
信号量
| 具有互斥的条件变量 - pthreads POSIX 信号量
| System V 信号量
|
事件
| 具有互斥的条件变量 - pthreads
| System V 信号量 |
|
Windows 信号量是一些计数器变量,允许有限个线程/进程访问共享资源。Linux POSIX 信号量也是一些计数器变量,可以用来在 Linux 上实现 Windows 上的信号量功能。
在对进程进行映射时,我们需要考虑以下问题:
| Windows | Linux 线程 | Linux 进程 | 类别 |
CreateSemaphore
| sem_init
| semget semctl
| 与上下文相关
|
OpenSemaphore
| 不适用
| semget
| 与上下文相关
|
WaitForSingleObject
| sem_wait sem_trywait
| semop
| 与上下文相关
|
ReleaseSemaphore
| sem_post
| semop
| 与上下文相关
|
CloseHandle
| sem_destroy
| semctl
| 与上下文相关 |
在 Windows 中,可以使用 CreateSemaphore() 创建或打开一个有名或无名的信号量。
HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCTSTR lpName ); |
在这段代码中:
lpSemaphoreAttributes 是一个指向安全性属性的指针。如果这个指针为空,那么这个信号量就不能被继承。
lInitialCount 是该信号量的初始值。
lMaximumCount 是该信号量的最大值,该值必须大于 0。
lpName 是信号量的名称。如果该值为 NULL,那么这个信号量就只能在相同进程的不同线程之间共享。否则,就可以在不同的进程之间进行共享。 这个函数创建信号量,并返回这个信号量的句柄。它还将初始值设置为调用中指定的值。这样就可以允许有限个线程来访问某个共享资源。
在 Linux 中,可以使用 sem_init() 来创建一个无名的 POSIX 信号量,这个调用可以在相同进程的线程之间使用。它还会对信号量计数器进行初始化:int sem_init(sem_t *sem, int pshared, unsigned int value)。在这段代码中:
value(信号量计数器)是这个信号量的初始值。
pshared 可以忽略,因为在目前的实现中,POSIX 信号量还不能在进程之间进行共享。 这里要注意的是,最大值基于 demaphore.h 头文件中定义的 SEM_VALUE_MAX。
在 Linux 中,semget() 用于创建 System V 信号量,它可以在不同集成的线程之间使用。可以用它来实现与 Windows 中有名信号量相同的功能。这个函数返回一个信号量集标识符,它与一个参数的键值关联在一起。当创建一个新信号量集时,对于与 semid_ds 数据结构关联在一起的信号量,semget() 要负责将它们进行初始化,方法如下:
sem_perm.cuid 和 sem_perm.uid 被设置为调用进程的有效用户 ID。
sem_perm.cgid 和 sem_perm.gid 被设置为调用进程的有效组 ID。
sem_perm.mode 的低 9 位被设置为 semflg 的低 9 位。
sem_nsems 被设置为 nsems 的值。
sem_otime 被设置为 0。
sem_ctime 被设置为当前时间。 用来创建 System V 信号量使用的代码是:int semget(key_t key, int nsems, int semflg)。下面是对这段代码的一些解释:
key 是一个惟一的标识符,不同的进程使用它来标识这个信号量集。我们可以使用 ftok() 生成一个惟一的键值。IPC_PRIVATE 是一个特殊的 key_t 值;当使用 IPC_PRIVATE 作为 key 时,这个系统调用就会只使用 semflg 的低 9 位,但却忽略其他内容,从而新创建一个信号量集(在成功时)。
nsems 是这个信号量集中信号量的数量。
semflg 是这个新信号量集的权限。要新创建一个信号量集,您可以将使用 IPC_CREAT 来设置位操作或访问权限。如果具有该 key 值的信号量集已经存在,那么 IPC_CREAT/IPC_EXCL 标记就会失败。 注意,在 System V 信号量中,key 被用来惟一标识信号量;在 Windows 中,信号量是使用一个名称来标识的。
为了对信号量集数据结构进行初始化,可以使用 IPC_SET 命令来调用 semctl() 系统调用。将 arg.buf 所指向的 semid_ds 数据结构的某些成员的值写入信号量集数据结构中,同时更新这个结构的 sem_ctime member 的值。用户提供的这个 arg.buf 所指向的 semid_ds 结构如下所示:
sem_perm.uid
sem_perm.gid
sem_perm.mode (只有最低 9 位有效) 调用进程的有效用户 ID 应该是超级用户,或者至少应该与这个信号量集的创建者或所有者匹配: int semctl(int semid, int semnum, int cmd = IPC_SET, ...)。在这段代码中:
semid 是信号量集的标识符。
semnum 是信号量子集偏移量(从 0 到 nsems -1,其中 n 是这个信号量集中子集的个数)。这个命令会被忽略。
cmd 是命令;它使用 IPC_SET 来设置信号量的值。
args 是这个信号量集数据结构中要通过 IPC_SET 来更新的值(在这个例子中会有解释)。 最大计数器的值是根据在头文件中定义的 SEMVMX 来决定的。
在 Windows 中,我们使用 OpenSemaphore() 来打开某个指定信号量。只有在两个进程之间共享信号量时,才需要使用信号量。在成功打开信号量之后,这个函数就会返回这个信号量的句柄,这样就可以在后续的调用中使用它了。
HANDLE OpenSemaphore( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName ) |
在这段代码中:
dwDesiredAccess 是针对该信号量对象所请求的访问权。
bInheritHandle 是用来控制这个信号量句柄是否可继承的标记。如果该值为 TRUE,那么这个句柄可以被继承。
lpName 是这个信号量的名称。 在 Linux 中,可以调用相同的 semget() 来打开某个信号量,不过此时 semflg 的值为 0:int semget(key,nsems,0)。在这段代码中:
key 应该指向想要打开的信号量集的 key 值。
nsems 和标记设置为 0。semflg 值是在返回信号量集标识符之前对访问权限进行验证时设置的。 在 Windows 中,等待函数提供了获取同步对象的机制。可以使用的等待函数有多种类型;在这一节中,我们只考虑 WaitForSingleObject()(其他类型将会分别进行讨论)。这个函数使用一个信号量对象的句柄作为参数,并会一直等待下去,直到其状态变为有信号状态或超时为止。 DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds );
在这段代码中:
hHandle 是指向互斥句柄的指针。
dwMilliseconds 是超时时间,以毫秒为单位。如果该值是 INFINITE,那么它阻塞调用线程/进程的时间就是不确定的。 在 Linux 中,sem_wait() 用来获取对信号量的访问。这个函数会挂起调用线程,直到这个信号量有一个非空计数为止。然后,它可以原子地减少这个信号量计数器的值:int sem_wait(sem_t * sem)。
在 POSIX 信号量中并没有超时操作。这可以通过在一个循环中执行一个非阻塞的 sem_trywait() 实现,该函数会对超时值进行计算:int sem_trywait(sem_t * sem)。
在使用 System V 信号量时,如果通过使用 IPC_SET 命令的 semctl() 调用设置初始的值,那么必须要使用 semop() 来获取信号量。semop() 执行操作集中指定的操作,并阻塞调用线程/进程,直到信号量值为 0 或更大为止:int semop(int semid, struct sembuf *sops, unsigned nsops)。
函数 semop() 原子地执行在 sops 中所包含的操作 —— 也就是说,只有在这些操作可以同时成功执行时,这些操作才会被同时执行。sops 所指向的数组中的每个 nsops 元素都使用 struct sembuf 指定了一个要对信号量执行的操作,这个结构包括以下成员:
unsigned short sem_num; (信号量个数)
short sem_op; (信号量操作)
short sem_flg; (操作标记) 要获取信号量,可以通过将 sem_op 设置为 -1 来调用 semop();在使用完信号量之后,可以通过将 sem_op 设置为 1 来调用 semop() 释放信号量。通过将 sem_op 设置为 -1 来调用 semop(),信号量计数器将会减小 1,如果该值小于 0(信号量的值是不能小于 0 的),那么这个信号量就不能再减小,而是会让调用线程/进程阻塞,直到其状态变为有信号状态为止。
sem_flg 中可以识别的标记是 IPC_NOWAIT 和 SEM_UNDO。如果某一个操作被设置了 SEM_UNDO 标记,那么在进程结束时,该操作将被取消。如果 sem_op 被设置为 0,那么 semop() 就会等待 semval 变成 0。这是一个"等待为 0" 的操作,可以用它来获取信号量。
记住,超时操作在 System V 信号量中并不适用。这可以在一个循环中使用非阻塞的 semop()(通过将 sem_flg 设置为 IPC_NOWAIT)实现,这会计算超时的值。
在 Windows 中,ReleaseSemaphore() 用来释放信号量。
BOOL ReleaseSemaphore( HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount ); |
在这段代码中:
hSemaphore 是一个指向信号量句柄的指针。
lReleaseCount 是信号量计数器,可以通过指定的数量来增加计数。
lpPreviousCount 是指向上一个信号量计数器返回时的变量的指针。如果并没有请求上一个信号量计数器的值,那么这个参数可以是 NULL。 这个函数会将信号量计数器的值增加在 lReleaseCount 中指定的值上,然后将这个信号量的状态设置为有信号状态。
在 Linux 中,我们使用 sem_post() 来释放信号量。这会唤醒对这个信号量进行阻塞的所有线程。信号量的计数器同时被增加 1。要为这个信号量的计数器添加指定的值(就像是 Windows 上一样),可以使用一个互斥变量多次调用以下函数:int sem_post(sem_t * sem)。
对于 System V 信号量来说,只能使用 semop() 来释放信号量:int semop(int semid, struct sembuf *sops, unsigned nsops)。
函数 semop() 原子地执行 sops 中包含的一组操作(只在所有操作都可以同时成功执行时,才会将所有的操作同时一次执行完)。sops 所指向的数组中的每个 nsops 元素都使用一个 struct sembuf 结构指定了一个要对这个信号量执行的操作,该结构包含以下元素:
unsigned short sem_num;(信号量个数)
short sem_op; (信号量操作)
short sem_flg; (操作标记) 要释放信号量,可以通过将 sem_op 设置为 1 来调用 semop()。通过将 semop() 设置为 1 来调用 semop(),这个信号量的计数器会增加 1,同时用信号通知这个信号量。
在 Windows 中,我们使用 CloseHandle() 来关闭或销毁信号量对象。
BOOL CloseHandle( HANDLE hObject ); |
hObject 是指向这个同步对象句柄的指针。
在 Linux 中,sem_destroy() 负责销毁信号量对象,并释放它所持有的资源: int sem_destroy(sem_t *sem)。对于 System V 信号量来说,只能使用 semctl() 函数的 IPC_RMID 命令来关闭信号量集:int semctl(int semid, int semnum, int cmd = IPC_RMID, ...)。
这个命令将立即删除信号量集及其数据结构,并唤醒所有正在等待的进程(如果发生错误,则返回,并将 errno 设置为 EIDRM)。调用进程的有效用户 ID 必须是超级用户,或者可以与该信号量集的创建者或所有者匹配的用户。参数 semnum 会被忽略。
下面是信号量的几个例子。
清单 1. Windows 无名信号量的代码
HANDLE hSemaphore;
LONG lCountMax = 10;
LONG lPrevCount;
DWORD dwRetCode;
// Create a semaphore with initial and max. counts of 10.
hSemaphore = CreateSemaphore(
NULL, // no security attributes
0, // initial count
lCountMax, // maximum count
NULL); // unnamed semaphore
// Try to enter the semaphore gate.
dwRetCode = WaitForSingleObject(
hSemaphore, // handle to semaphore
2000L); // zero-second time-out interval
switch (dwRetCode)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
// Semaphore is signaled
// go ahead and continue the work
goto success:
break;
case WAIT_TIMEOUT:
// Handle the time out case
break;
}
Success:
// Job done, release the semaphore
ReleaseSemaphore(
hSemaphore, // handle to semaphore
1, // increase count by one
NULL) // not interested in previous count
// Close the semaphore handle
CloseHandle(hSemaphore);
|
// Main thread #define TIMEOUT 200 /* 2 secs */ // Thread 1 sem_t sem ; // Global Variable int retCode ; // Initialize event semaphore retCode = sem_init( sem, // handle to the event semaphore 0, // not shared 0); // initially set to non signaled state while (timeout < TIMEOUT ) { delay.tv_sec = 0; delay.tv_nsec = 1000000; /* 1 milli sec */ // Wait for the event be signaled retCode = sem_trywait( &sem); // event semaphore handle // non blocking call if (!retCode) { /* Event is signaled */ break; } else { /* check whether somebody else has the mutex */ if (retCode == EPERM ) { /* sleep for delay time */ nanosleep(&delay, NULL); timeout++ ; } else{ /* error */ } } } // Completed the job, // now destroy the event semaphore retCode = sem_destroy( &sem); // Event semaphore handle // Thread 2 // Condition met // now signal the event semaphore sem_post( &sem); // Event semaphore Handle |
清单 3. Linux 使用 System V 信号量的等效代码
// Process 1 #define TIMEOUT 200 //Definition of variables key_t key; int semid; int Ret; int timeout = 0; struct sembuf operation[1] ; union semun { int val; struct semid_ds *buf; USHORT *array; } semctl_arg,ignored_argument; key = ftok(); // Generate a unique key, U can also supply a value instead semid = semget(key, // a unique identifier to identify semaphore set 1, // number of semaphore in the semaphore set 0666 | IPC_CREAT // permissions (rwxrwxrwx) on the new // semaphore set and creation flag ); //Set Initial value for the resource semctl_arg.val = 0; //Setting semval to 0 semctl(semid, 0, SETVAL, semctl_arg); //Wait for Zero while(timeout < TIMEOUT) { delay.tv_sec = 0; delay.tv_nsec = 1000000; /* 1 milli sec */ //Call Wait for Zero with IPC_NOWAIT option,so it will be non blocking operation[0].sem_op = -1; // Wait until the semaphore count becomes 0 operation[0].sem_num = 0; operation[0].sem_flg = IPC_NOWAIT; ret = semop(semid, operation,1); if(ret < 0) { /* check whether somebody else has the mutex */ if (retCode == EPERM ) { /* sleep for delay time */ nanosleep(&delay, NULL); timeout++ ; } else { printf("ERROR while wait "); break; } } else { /*semaphore got triggered */ break; } } //Close semaphore iRc = semctl(semid, 1, IPC_RMID , ignored_argument); } // Process 2 key_t key = KEY; // Process 2 should know key value in order to open the // existing semaphore set struct sembuf operation[1] ; //Open semaphore semid = semget(key, 1, 0); operation[0].sem_op = 1; // Release the resource so Wait in process 1 will // be triggered operation[0].sem_num = 0; operation[0].sem_flg = SEM_UNDO; //Release semaphore semop(semid, operation,0); } |
|
在 Windows 中,事件对象是那些需要使用 SetEvent() 函数显式地将其状态设置为有信号状态的同步对象。事件对象来源有两种类型:
ResetEvent() 函数显式地重新设置它为止。
事件对象有两种状态,有信号(signaled)状态 和 无信号(non-signaled)状态。对事件对象调用的等待函数会阻塞调用线程,直到其状态被设置为有信号状态为止。
在进行平台的迁移时,需要考虑以下问题:
还有几点非常重要,需要说明一下:
| Windows | Linux 线程 | Linux 进程 | 类别 |
CreateEvent OpenEvent
| pthread_cond_init sem_init
| semget semctl
| 与上下文相关
|
SetEvent
| pthread_cond_signal sem_post
| semop
| 与上下文相关
|
ResetEvent
| N/A
| N/A
| 与上下文相关
|
WaitForSingleObject
| pthread_cond_wait pthread_cond_timedwait sem_wait sem_trywait
| semop
| 与上下文相关
|
CloseHandle
| pthread_cond_destroy sem_destroy
| semctl
| 与上下文相关 |
在 Windows 中,我们使用 CreateEvent() 来创建事件对象。
HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCTSTR lpName ) |
在这段代码中:
lpEventAttributes 是一个指针,它指向一个决定这个句柄是否能够被继承的属性。如果这个指针为 NULL,那么这个对象就不能被初始化。
bManualReset 是一个标记,如果该值为 TRUE,就会创建一个手工重置的事件,应该显式地调用 ResetEvent(),将事件对象的状态设置为无信号状态。
bInitialState 是这个事件对象的初始状态。如果该值为 true,那么这个事件对象的初始状态就被设置为有信号状态。
lpName 是指向这个事件对象名的指针。对于无名的事件对象来说,该值是 NULL。 这个函数创建一个手工重置或自动重置的事件对象,同时还要设置改对象的初始状态。这个函数返回事件对象的句柄,这样就可以在后续的调用中使用这个事件对象了。
OpenEvent() 用来打开一个现有的有名事件对象。这个函数返回该事件对象的句柄。
HANDLE OpenEvent( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName ) |
在这段代码中:
dwDesiredAccess 是针对这个事件对象所请求的访问权。
bInheritHandle 是用来控制这个事件对象句柄是否可继承的标记。如果该值为 TRUE,那么这个句柄就可以被继承;否则就不能被继承。
lpName 是一个指向事件对象名的指针。 在 Linux 中,可以调用 sem_init() 来创建一个 POSIX 信号量:int sem_init(sem_t *sem, int pshared, unsigned int value)(其中 value(即信号量计数值)被设置为这个信号量的初始状态)。
Linux pthreads 使用 pthread_cond_init() 来创建一个条件变量:int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)。
可以使用 PTHREAD_COND_INITIALIZER 常量静态地对 pthread_cond_t 类型的条件变量进行初始化,也可以使用 pthread_condattr_init() 对其进行初始化,这个函数会对与这个条件变量关联在一起的属性进行初始化。可以调用 pthread_condattr_destroy() 用来销毁属性:
int pthread_condattr_init(pthread_condattr_t *attr) int pthread_condattr_destroy(pthread_condattr_t *attr) |
在 Windows 中,等待函数提供了获取同步对象的机制。我们可以使用不同类型的等待函数(此处我们只考虑 WaitForSingleObject())。这个函数会使用一个互斥对象的句柄,并一直等待,直到它变为有信号状态或超时为止。
DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds ); |
在这段代码中:
hHandle 是指向互斥句柄的指针。
dwMilliseconds 是超时时间的值,单位是毫秒。如果该值为 INFINITE,那么它阻塞调用线程/进程的时间就是不确定的。 Linux POSIX 信号量使用 sem_wait() 来挂起调用线程,直到信号量的计数器变成非零的值为止。然后它会自动减小信号量计数器的值:int sem_wait(sem_t * sem)。
在 POSIX 信号量中并没有提供超时操作。这可以通过在一个循环中执行非阻塞的 sem_trywait() 来实现,该函数会对超时时间进行计数:int sem_trywait(sem_t * sem).
Linux pthreads 使用 pthread_cond_wait() 来阻塞调用线程,其时间是不确定的:int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)。在另外一方面,如果调用线程需要被阻塞一段确定的时间,那么就可以使用 pthread_cond_timedwait() 来阻塞这个线程。如果在这段指定的时间内条件变量并没有出现,那么 pthread_cond_timedwait() 就会返回一个错误:int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,const struct timespec *abstime)。在这里,abstime 参数指定了一个绝对时间(具体来说,就是从 1970 年 1 月 1 日 0 时 0 分 0 秒到现在所经过的时间。)
函数 SetEvent() 用来将事件对象的状态设置为有信号状态。对一个已经设置为有信号状态的事件对象再次执行该函数是无效的。
BOOL SetEvent( HANDLE hEvent ) |
Linux POSIX 信号量使用 sem_post() 来发出一个事件信号量。这会唤醒在该信号量上阻塞的所有线程:int sem_post(sem_t * sem)。
调用 pthread_cond_signal() 被用在 LinuxThreads 中,以唤醒在某个条件变量上等待的一个线程,而 pthread_cond_broadcast() 用来唤醒在某个条件变量上等待的所有线程。
int pthread_cond_signal(pthread_cond_t *cond) int pthread_cond_broadcast(pthread_cond_t *cond) |
注意,条件函数并不是异步信号安全的,因此不能在信号处理函数中调用。具体地说,在信号处理函数中调用 pthread_cond_signal() 或 pthread_cond_broadcast() 可能会导致调用线程的死锁。
在 Windows 中,ResetEvent() 用来将事件对象的状态重新设置为无信号状态。
BOOL ResetEvent( HANDLE hEvent ); |
在 Linux 中,条件变量和 POSIX 信号量都是自动重置类型的。
在 Windows 中,CloseHandle() 用来关闭或销毁事件对象。
BOOL CloseHandle( HANDLE hObject ); |
在这段代码中,hObject 是指向同步对象句柄的指针。
在 Linux 中, sem_destroy()/ pthread_cond_destroy() 用来销毁信号量对象或条件变量,并释放它们所持有的资源:
int sem_destroy(sem_t *sem) int pthread_cond_destroy(pthread_cond_t *cond) |
在 Linux 中,进程之间有名事件对象所实现的功能可以使用 System V 信号量实现。System V 信号量是计数器变量,因此可以实现 Windows 中事件对象的功能,信号量的计数器的初始值可以使用 semctl() 设置为 0。
要将某个事件的状态修改为有信号状态,可以使用 semop(),并将 sem_op 的值设置为 1。要等待某个事件,则可以使用 semop() 函数,并将 sem_op 的值设置为 -1,这样就可以阻塞调用进程,直到它变为有信号状态为止。
可以通过使用 semctl() 将信号量计数器的初始值设置为 0 来获得信号量。在使用完共享资源之后,可以使用 semop() 将信号量计数设置为 1。关于每个 System V 信号量的原型,请参阅本文中有关信号量一节的内容。
下面几个例子可以帮助您理解我们在这一节中所讨论的内容。
清单 4. Windows 无名事件对象的代码
// Main thread HANDLE hEvent; // Global Variable // Thread 1 DWORD dwRetCode; // Create Event hEvent = CreateEvent( NULL, // no security attributes FALSE, // Auto reset event FALSE, // initially set to non signaled state NULL); // un named event // Wait for the event be signaled dwRetCode = WaitForSingleObject( hEvent, // Mutex handle INFINITE); // Infinite wait switch(dwRetCode) { case WAIT_OBJECT_O : // Event is signaled // go ahead and proceed the work default : // Probe for error } // Completed the job, // now close the event handle CloseHandle(hEvent); // Thread 2 // Condition met for the event hEvent // now set the event SetEvent( hEvent); // Event Handle |
// Main thread sem_t sem ; // Global Variable // Thread 1 int retCode ; // Initialize event semaphore retCode = sem_init( sem, // handle to the event semaphore 0, // not shared 0); // initially set to non signaled state // Wait for the event be signaled retCode = sem_wait( &sem); // event semaphore handle // Indefinite wait // Event Signaled // a head and proceed the work // Completed the job, // now destroy the event semaphore retCode = sem_destroy( &sem); // Event semaphore handle // Thread 2 // Condition met // now signal the event semaphore sem_post( &sem); // Event semaphore Handle |
// Main thread pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condvar = PTHREAD_COND_INITIALIZER; // Thread 1 ... pthread_mutex_lock(&mutex); // signal one thread to wake up pthread_cond_signal(&condvar); pthread_mutex_unlock(&mutex); // this signal is lost as no one is waiting // Thread 1 now tries to take the mutex lock // to send the signal but gets blocked ... pthread_mutex_lock(&mutex); // Thread 1 now gets the lock and can // signal thread 2 to wake up pthread_cond_signal(&condvar); pthread_mutex_unlock(&mutex); // Thread 2 pthread_mutex_lock(&mutex); pthread_cond_wait(&condvar, &mutex); pthread_mutex_unlock(&mutex); // Thread 2 blocks indefinitely // One way of avoiding losing the signal is as follows // In Thread 2 - Lock the mutex early to avoid losing signal pthread_mutex_lock (&mutex); // Do work ....... // This work may lead other threads to send signal to thread 2 // Thread 2 waits for indefinitely for the signal to be posted pthread_cond_wait (&condvar, &Mutex ); // Thread 2 unblocks upon receipt of signal pthread_mutex_unlock (&mutex); |
// Process 1 DWORD dwRetCode; HANDLE hEvent; // Local variable // Create Event hEvent = CreateEvent( NULL, // no security attributes FALSE, // Auto reset event FALSE, // initially set to non signaled state "myEvent"); // un named event // Wait for the event be signaled dwRetCode = WaitForSingleObject( hEvent, // Mutex handle INFINITE); // Infinite wait switch(dwRetCode) { case WAIT_OBJECT_O : // Event is signaled // go ahead and proceed the work default : // Probe for error } // Completed the job, // now close the event handle CloseHandle(hEvent); // Process 2 HANDLE hEvent; // Local variable // Open the Event hEvent = CreateEvent( NULL, // no security attributes FALSE, // do not inherit handle "myEvent"); // un named event // Condition met for the event hEvent // now set the event SetEvent( hEvent); // Event Handle // completed the job, now close the event handle CloseHandle(hEvent); |
清单 8. Linux 中使用 System V 信号量的等效代码
// Process 1 int main() { //Definition of variables key_t key; int semid; int Ret; int timeout = 0; struct sembuf operation[1] ; union semun { int val; struct semid_ds *buf; USHORT *array; } semctl_arg,ignored_argument; key = ftok(); /Generate a unique key, U can also supply a value instead semid = semget(key, // a unique identifier to identify semaphore set 1, // number of semaphore in the semaphore set 0666 | IPC_CREAT // permissions (rwxrwxrwx) on the new // semaphore set and creation flag ); if(semid < 0) { printf("Create semaphore set failed "); Exit(1); } //Set Initial value for the resource - initially not owned semctl_arg.val = 0; //Setting semval to 0 semctl(semid, 0, SETVAL, semctl_arg); // wait on the semaphore // blocked until it is signaled operation[0].sem_op = -1; operation[0].sem_num = 0; operation[0].sem_flg = IPC_WAIT; ret = semop(semid, operation,1); // access the shared resource ... ... //Close semaphore iRc = semctl(semid, 1, IPC_RMID , ignored_argument); } // Process 2 int main() { key_t key = KEY; //Process 2 shd know key value in order to open the // existing semaphore set struct sembuf operation[1] ; //Open semaphore semid = semget(key, 1, 0); // signal the semaphore by incrementing the semaphore count operation[0].sem_op = 1; operation[0].sem_num = 0; operation[0].sem_flg = SEM_UNDO; semop(semid, operation,0); } |
|
本文是这一系列的第 2 部分,这篇文章从信号量和事件入手,介绍了有关同步对象和原语的内容。第 3 部分的内容将涉及互斥、临界区和等待函数。
|
|
Srinivasan S. Muthuswamy 是 IBM Global Services Group 的一位软件工程师。他于 2000 年加入 IBM,他所精通的编程语言涵盖了多种平台上(Linux、Windows、WebSphere、Lotus,等等)的脚本语言以及面向对象和面向过程的语言。他已经开发的解决方案包括 Linux 和 Windows 上的系统编程以及用于 J2EE 的 Web 解决方案。他在印度的 Coimbatore 国立科技大学(Government College of Technology)获得计算机工程学士学位,主要致力于集成和迁移。您可以通过 smuthusw@in.ibm.com 与他联系。 | |
|
|
从 2000 年 12 月起,Kavitha Varadarajan 一直在 IBM India Software Lab 担任软件工程师。她的工作经验包括 host-access 客户机产品(比如 PCOMM)和网络软件(比如通信服务器)的开发与支持。Varadarajan 拥有迁移项目的实践经验,其中涉及到了面向对象 IPC Windows 应用程序向 Linux 的移植。她拥有印度的 Tanjore 山姆哈工程大学(Shanmugha College of Engineering)的计算机科学与工程硕士学位。您可以通过 vkavitha@in.ibm.com 与她联系。 | |
|
|