MyMSDN

MyMSDN记录开发新知道

2010年1月16日 #

如何在Windows 7下安装Wubi以启动安装在Windows下(传统方式+EasyBCD方式)

如何在Windows 7下安装Wubi以启动安装在Windows下(传统方式+EasyBCD方式)


传统方式

  1. 前提条件

    1. 本节适合“主分区+逻辑分区”的分区方式,不支持“系统分区+主分区+逻辑分区”的分区方式,也就是说,针对于Windows Vista之前的系统,均适用本节,针对Windows Vista以及之后的系统,若是采用“系统分区+主分区+逻辑分区”的方式(如全盘重装,包括划分分区表(重要),安装系统),本节可能并不适合,请参看下一节“EasyBCD方法。”
  2. 操作步骤

  3. 主要是Windows XP下,大家通过在boot.ini里增加“C:\wubildr.mbr="Ubuntu"”就可以启动Ubuntu。 而Windows 7则使用了BCD规则(也可以使用EasyBCD工具来运行(相见下文))。

    下面的规则则较为简单:

    1. 打开XP下的ntldr,以及boot.ini,将其拷贝到Windows 7下(参考附件)
    2. 拷贝wubildr.mbr以及wubildr至主分区(C盘)(这个即便在XP下你也需要完成)
    3. 重新启动系统即可。

    下载文件:ntldr.zip (32bit)

EasyBCD方法

  1. 前提条件

    1. 你的Ubuntu是通过wubi的方式安装的,Grub的方式也类似,只不过是设置的时候选择别的选项,这里不做描述。
    2. 本节基本适合于Windows(Vista/7)(x86/x64),(XP以及之前的系统适合下面步骤2)对于步骤2中提及的方法可以实现的建议按旧方案。
    3. 本节不适合即将在当前Windows环境下全新安装Ubuntu的用户。
    4. Windows系统是System分区+主分区+逻辑分区 构成的。(例如:不是从XP或者其它系统升级而成的,而是全新安装的)

  2. 操作步骤

    1. 我在旧系统(WinXP)下安装Ubuntu(by wubi)。
    2. 我将Ubuntu放在我的移动硬盘里面,并与我的另一台电脑共享(Win7 x86),共享方法:http://forum.ubuntu.org.cn/viewtopic.php?f=48&t=248205
    3. 我购置了新电脑,并安装了Win7 x64,但步骤2中所提及的适用于Win7 x86的共享方法失效了。(不知是否需要将ntldr替换为x64版的方可生效?因为我没有,所以不确定)
    4. 我下载了EasyBCD,选择了“Add/Remove Entries”,选择了“Linux”选项卡,并选择Type为“wubi”,于是它在我的C:\下放置了\Device\HarddiskVolume1文件夹以及其中的内容,想必就是一个wubildr.mbr吧?查看menu.lst,主要内容如下:
    5. find --set-root --ignore-floppies \ubuntu\winboot\menu.lst
      configfile \ubuntu\winboot\menu.lst
    6. 通过EasyBCD的“ViewSettings”,看到:
    7. Entry #2

      Name: NeoSmart Linux
      BCD ID: {46ca74c9-fdd2-11de-914a-a89213a2f2bd}
      Drive: \Device\HarddiskVolume1
      Bootloader Path: \NST\NeoGrub.mbr
    8. 因为步骤5中的Drive不是盘符(对应Windows7的项,你很容易分辨出来),因此在“Change Settings”中的“Entity-Based Setting”,选择你刚设置的Ubuntu,并选择Drive为C盘(对应步骤2中所提及的共享方法)
    9. 在步骤2中所提及的共享方法里含有两个文件wubildr.mbr以及wubildr。我们在Win7x64所遭遇的问题就是无法通过启动项目的设置,让系统启动的时候找到这个wubildr.mbr文件,但在EasyBCD中看来,也似乎没有地方可以改变NeoGrub.mbr的名字,那么一个折中的方案就是将wubildr.mbr的名字修改成NeoGrub.mbr,并将其放在C盘(在之前步骤的Drive中设置)下的NST目录中,并将旧的wubildr文件拷贝至C盘(同样等同于Drive的盘符设置)
    10. 至此,重新启动电脑,应该就会出现你所熟悉的场景了。

posted @ 2010-01-16 20:16 volnet 阅读(199) | 评论 (0)编辑 收藏

2009年12月30日 #

C语言位域

这个概念显然是知道的,不过刚才忘了,然后写了个程序就明白了,记录一下,也许以后又健忘了:
 1 #include <stdio.h>
 2 typedef struct _SimpleType1 {
 3     int Variable1;    //4bytes(32bits)
 4     int Variable2;    //4bytes(32bits)
 5     int Variable3;    //4bytes(32bits)
 6     int Variable4;    //4bytes(32bits)
 7 } SimpleType1;
 8 
 9 typedef struct _ComplexType1 {
10     int Variable1 : 8;    //1bytes(8bits)
11     int Variable2 : 8;    //1bytes(8bits)
12     int Variable3 : 8;    //1bytes(8bits)
13     int Variable4 : 8;    //1bytes(8bits)
14 } ComplexType1;
15 
16 typedef struct _ComplexType2 {
17     int Variable1 : 8;    //1bytes(8bits)
18     int Variable2 : 8;    //1bytes(8bits)
19     int Variable3 : 8;    //1bytes(8bits)
20     int Variable4 : 8;    //1bytes(8bits)
21     int Variable5 : 1;    //0.125bytes(1bits) but the it also hold 32bits 
22 } ComplexType2;
23 
24 int main(void){
25     printf("sizeof SimpleType1 = %d\n"sizeof(SimpleType1));
26     printf("sizeof ComplexType1 = %d\n"sizeof(ComplexType1));
27     printf("sizeof ComplexType2 = %d\n"sizeof(ComplexType2));
28 }
结果:
sizeof SimpleType1 = 16
sizeof ComplexType1 = 4
sizeof ComplexType2 = 8

posted @ 2009-12-30 01:02 volnet 阅读(254) | 评论 (0)编辑 收藏

2009年12月26日 #

关于线程同步的一些总结(用户模式/内核模式)

自旋锁同步

  1. 一般是为了内核态下各个派遣函数之间做同步作用的。
  2. 原理是(单CPU)将IRQL从软件中断提升到硬件中断。PASSIVE_LEVEL->DISPATCH_LEVEL。因为在DISPATCH_LEVEL中是不会出现线程切换的(只有高级别能打断低级别,而低级别不能打断高级别)。
  3. 因为分页内存将导致如果线程切换的时候会引起分页数据交换,数据交换是通过引发页故障来实现的,而页故障是不允许出现在DISPATCH_LEVEL中的,否则将引起系统崩溃(PASSIVE_LEVEL则允许)。驱动程序的StartIO例程、DPC例程、中断服务例程都运行在DISPATCH_LEVEL或者更高的IRQL。因此这些例程不能使用分页内存,否则将导致系统崩溃。
  4. 自旋锁在不同IRP之间同步的时候,则需要放在DeviceExtension中传递。

互锁

  1. 类似于number++; //汇编后将不止一条语句,非原子操作number--; //同上因为语句会变成多句,在线程切换的时候,两个线程下的该例程将会交织在一起执行,导致错误。可以:
    先加锁
    number++;
    解锁
    再加锁
    number--;
    解锁
    来实现两句话的同步(按指定顺序执行,而不受到线程切换的影响)加锁解锁可以使用自旋锁
  2. 在系统中提供了Interlocked***/ExInterlocked***实现

信号灯同步

  1. 线程1关闭信号灯,以至于使用Wait****的时候,当前线程处于暂停状态。
  2. 线程2的作用就是在执行结束后,点亮信号灯(增加计数器)。当线程切换回来的时候,线程1就因为计数器不是0而使信号灯处于激活状态,从而继续执行线程1。

事件的同步

(不能递归获取互斥体)
  1. 主线程在辅助线程上设置了事件,如果不使用Wait**等待事件返回,则主线程可能直接执行完毕了,而导致辅助线程还在执行。
  2. 使用Wait****可以使主线程等待事件执行完成。

互斥体同步

(允许递归获取互斥体(得到互斥体的线程还可以再次获得这个互斥体,或者说互斥体对于已经获得互斥体的线程不产生“互斥”关系))
  1. 创建一个互斥体对象,将互斥体对象传递给多个线程。
  2. 在每个线程操作的步骤中,调用Wait*****,如果互斥体处于激活(内部维护一个计数器),则继续执行后续代码,并在调用结束后恢复互斥体Release****,这样当别的线程试图使用互斥体后面的代码的时候,因为互斥体状态未激活,则无法继续执行代码。

快速互斥体同步

  1. 与互斥体同步类似,唯一区别是不允许递归获取互斥体

posted @ 2009-12-26 05:53 volnet 阅读(308) | 评论 (0)编辑 收藏

2009年12月23日 #

如何编译TrueCrypt源码

相关配置

       
  • Intel x86 Core 2 Duo
  •    
  • Windows 7 Ultimate x86 version
  •    
  • Windows Driver Develop Kit 7600.16385.0
  •    
  • TrueCrypt 6.3a Source.zip
  •    
  • Microsoft Visual Studio 2008 SP1 (VC++ 2008)
  •    
  • Microsoft Visual Studio VC++ 1.52
  •    
  • NASM version 2.07 compiled on Jul 19 2009
  •    
  • gzip 1.2.4 Win32 (02 Dec 97)
  •    
  • ......

配置TrueCrypt

  1. 下载MSVC++ 1.52,安装在C盘下:C:\MSVC
  2. 下载NASM,也安装在C盘下:C:\NASM
    http://www.nasm.us/pub/nasm/releasebuilds/2.07/win32/
  3. 下载GZIP,也安装在C盘下:C:\gzip
  4. 下载并安装WINDDK,http://www.microsoft.com/whdc/DevTools/WDK/WDKpkg.mspx
    我将它们安装在D盘,路径:D:\WinDDK
  5. 设置系统变量((WIN7)控制面板\所有控制面板项\系统\高级系统设置\环境变量):系统变量中,新增:
    变量1:MSVC16_ROOT    值:C:\MSVC
    变量2:WINDDK_ROOT    值:D:\WinDDK\7600.16385.0
    其中7600.16385.0为WinDDK的第二级目录名,同时也是版本号,7600是Windows7的发行版本号。
    双击变量:PATH,在其值的末尾补上:C:\NASM;C:\gzip
    目的是为了让我们可以直接在命令行实用nasm以及gzip作为命令行。
  6. 下载PKCS11,三个文件,右键另存为即可。
    http://svn.openvpn.net/projects/openvpn/test/time/openvpn/pkcs11-headers/
    将三个文件(pkcs11.h、pkcs11f.h、pkcs11t.h)拷贝到源码下的Common文件夹下,我的源码放在D盘根目录,三个源码我就放在“D:\TrueCrypt\Common”文件夹中。
  7. 编译,会发现有两个错误。
    CKR_NEW_PIN_MODE和CKR_NEXT_OTP未定义,补充定义一下即可。
    在d:\TrueCrypt\Common\pkcs11t.h文件里(请根据您自己的路径进行复制)
    这里将它们设置为:
    #define CKR_NEW_PIN_MODE      0x000001B0
    #define CKR_NEXT_OTP          0x000001B1
    我的方法是找到实用它的语句附近的同类语句,找到相同的定义,在其下方添加。
    比如:
                TC_TOKEN_ERR (CKR_MUTEX_NOT_LOCKED)
                TC_TOKEN_ERR (CKR_NEW_PIN_MODE)
                TC_TOKEN_ERR (CKR_NEXT_OTP)
    这三句话放在一起,后两句有问题,但第一句正常,则查找CKR_MUTEX_NOT_LOCKED的存放位置,在其下方添加如上两句,其中定义的值参考
    http://www.cryptsoft.com/pkcs11doc/STANDARD/include/v220/otp-pkcs11.h,这里的值只不过是一种错误码,只要它不重复,就可以了。
  8. 再编译,可能会遇到一些警告:
    1. nasm.exe正在停止,而因为没有正确执行,又导致“fatal error LNK1136: invalid or corrupt file”错误。
      遇到这个可能是因为你的nasm正在试图编译ase_amdX64.asm文件,而nasm对64位的asm编译对你可能意义不大,起码对我而言是这样的,于是,我就将它转成编译x86体系架构的,也许是因为参数配置的问题,你可以尝试别的方案,如果有更好的话,请告诉我。
      这里我搜索:x64、asm等关键字,修改d:\TrueCrypt\Crypto\Makefile.inc文件为下面这样即可:
      行1    !if "$(TC_ARCH)" == "x86"
      行2    TC_OBJ_FORMAT = win32
      行3    !else
      行4    #TC_OBJ_FORMAT = win64
      行5    #edit by gocool, if the x64 system need the nasm.exe use the x64 format parameters for executing.
      行6    #abort the x64 system here for building.
      行7    #2009/12/23
      行8    TC_OBJ_FORMAT = win32
      行9    TC_ARCH = x86
      行10    !endif
      行11   
      行12    "$(OBJ_PATH)\$(O)\Aes_$(TC_ARCH).obj": Aes_$(TC_ARCH).asm
      行13        nasm.exe -Xvc -f $(TC_OBJ_FORMAT) -Ox -D DLL_EXPORT -o "$@" -l "$(OBJ_PATH)\$(O)\Aes_$(TC_ARCH).lst" Aes_$(TC_ARCH).asm
      其中,为了减少变化量,也利于以后恢复,第4-7行为注释,第8、9行我将非x86的情况也定义成x86的情况,这样无论如何下面第13行的语句都将执行以x86体系为结构的设置,而这样的设置通常是正确的。
    2. fatal error LNK1000: Internal error during IncrBuildImage
      据说是Microsoft Visual Studio 2008的一个BUG。http://blog.csdn.net/just_one_two/archive/2009/10/05/4634391.aspx
      听说有两种方法,一种是方法1,需要下载补丁,我没有尝试。第二种通过修改配置的方法我成功了,步骤如下:
      方法:项目->属性->链接器->常规   下面的“启用增量链接”,将“是(/INCREMENTAL)”改为“否(/INCREMENTAL:NO)”。
      不过这又引入了另外一个警告:3>FormatCom.obj : warning LNK4075: 忽略“/EDITANDCONTINUE”(由于“/INCREMENTAL:NO”规范)
      选择项目,属性->配置属性->C/C++,修改“调试信息格式”为“程序数据库(/Zi)”即可。
    3. 警告:未找到下列环境变量
      4>项目 : warning PRJ0018 : 未找到下列环境变量:
      4>$(PKCS11_INC)
      由于项目属性里设置有附加包含目录“$(PKCS11_INC)”,因此编译的时候会在系统变量里寻找PKCS11_INC项目,如果找不到,则给出警告,因此,我们需要手动补充这个项。方法同步骤5,增加一个变量为PKCS11_INC,值:D:\TrueCrypt\Common,其中,值就是之前我们拷贝三个文件(pkcs11.h、pkcs11f.h、pkcs11t.h)的目录。
    4. 如果不出意外的话,你可能还会得到一个使用了PKEY_AppUserModel_ID未定义的声明符的错误。这个是用于标识用户态应用程序的唯一标识。你可以在Setup.h文件中定义:
      /*---region add by gc---*/
      #include "wtypes.h"
          const PROPERTYKEY PKEY_AppUserModel_ID = {
              {
                  (unsigned long)2009,/*unsigned long  Data1;*/
                  (unsigned short)12,/*unsigned short Data2;*/
                  (unsigned short)23,/*unsigned short Data3;*/
                  0x44,0x55,0x55,0x55,0x55,0x55,0x55,0x55
              },/*GUID fmtid;*/
              (DWORD)PID_FIRST_USABLE /*DWORD pid;*/
          };
      /*---endregion---*/
      其中,这个结构体是由GUID和PID共同组成的。

下载链接

  • TrueCrypt下载:http://www.sfr-fresh.com/windows/misc/TrueCrypt-6.2a-Source.zip:a/Boot/Windows/Makefile
  • WinDDK下载:http://www.microsoft.com/whdc/DevTools/WDK/WDKpkg.mspx
  • PKCS11下载:http://svn.openvpn.net/projects/openvpn/test/time/openvpn/pkcs11-headers/
  • GZip下载:http://www.gzip.org/ 或者 http://www.gzip.org/gz124src.zip
  • Nasm下载:http://www.nasm.us/pub/nasm/releasebuilds/2.07/win32/
  • MSVC1.52下载:http://download.csdn.net/source/620960 (15.02MB)(似乎网上很多人都在找1.52(最后一个可以编译16bit程序的VC编译器),但官方网站上没有公开下载的链接,实在非常郁闷,我从MSDN订阅下载(收费的噢,杯具)则有67.6MB),如果大家实在找不到下载或者15.02MB的不可用,可以联系我。

  • 参考链接

    • http://blog.csdn.net/skyremember/archive/2009/09/17/4562090.aspx
    • http://blog.sina.com.cn/s/blog_4758691d0100d8mc.html
    • http://lll332.blog.163.com/blog/static/1553692220093404635752/
    • http://msdn.microsoft.com/en-us/library/aa373931%28VS.85%29.aspx
    • http://hi.baidu.com/hhacker/blog/item/2fc5b3fb0b24132a4f4aea1d.html
    • http://blog.csdn.net/just_one_two/archive/2009/10/05/4634391.aspx
    • http://blog.csdn.net/liufei_learning/archive/2009/12/21/5047632.aspx
    • http://msdn.microsoft.com/zh-cn/library/958x11bc%28VS.80%29.aspx
    • http://bbs.xiakexing.com/cgi-bin/topic.cgi?forum=22&topic=498

    posted @ 2009-12-23 23:47 volnet 阅读(324) | 评论 (0)编辑 收藏

    2009年12月13日 #

    我的Linux小记(1)

    1、如何启动命令行?
    答:GNOME环境下,按Alt+F2,在随后出现的运行窗口中输入:gnome-terminal,回车。如图所示:

    2、如何以管理员权限运行命令?
    答:Ubuntu下:
    方法1:在命令行下,输入su,在提示下输入密码(不会明文显示),回车,接下来的命令都将以管理员权限运行。
    方法2:在命令行下,需要用到管理员权限运行的语句前加sudo(仅对单条语句有效),如果是当前命令行窗口下第一次用此命令,则需要输入密码,按提示输入密码,回车,之后只需要用sudo,将不再输入密码。
    3、如何在Ubuntu下禁用触摸板?
    答:在命令行中(参考1、2),输入以下命令,禁用触摸板:
    sudo rmmod psmouse

     

    重新启用,则在命令行下(Alt+F2启动后输入gnome-terminal),输入以下命令,启用触摸板:
    sudo modprobe psmouse
    如图所示:

    4、如何用head和tail命令?
    head命令用于显示文件的前10行,而tail命令则用于显示文件的后10行。
    支持的参数有:
    -c N:显示前(后)N字节的内容
    -n N:显示前(后)N行的内容
    5、如何用shell为文件重命名呢?
    在Linux中似乎没有rename这样一个方法,但是可以通过mv(移动文件)来为文件重命名:
    mv ./file-name1 ./file-name2
    通过在同级目录下移动文件,并指定文件名即可为文件重命名。

    6、fortune命令
    听说是个传奇的命令,听名字就是吧?那就试试,结果Ubuntu并没有自带,那么输入下面语句安装fortune。

    sudo apt-get install fortune-mod

    posted @ 2009-12-13 19:01 volnet 阅读(163) | 评论 (0)编辑 收藏

    2009年10月28日 #

    QuickSort快速排序法(2009-10-28)

    在本博客,之前有一个版本的QuickSort,其实网友很早就提出了相关的BUG,但是我一直没有时间进行修正,今天又有朋友提出了这个BUG,我刚好抽空看了一下,看来自己是相当不严谨以至于犯下了如此大错。

    原文链接:http://www.cppblog.com/mymsdn/archive/2009/03/06/quicksort.aspx (未修复)

    关于修复BUG后的代码,将在本文中提供,原文中不进行修改,但会有提示指明是错误的,有兴趣的朋友也可以直接在原文的代码中寻找错误来锻炼自己排错的能力。

    下面就是几段代码:

    Algorithms.cpp

    #include "StdAfx.h"
    #include "Algorithms.h"
    Algorithms::Algorithms(void)
    {
    }
    
    Algorithms::~Algorithms(void)
    {
    }
    

    Algorithms.h

    #pragma once
    
    #include <iostream>
    
    class Algorithms
    {
    public:
        Algorithms(void);
        ~Algorithms(void);
    
    public:
        template <typename T>
        static void QuickSort(T* arr, size_t min, size_t max);
    private:
        template <typename T>
        static size_t qsort_helper_partition(T* arr, size_t min, size_t max);
        template <typename T>
        static inline void swap(T* arr, size_t x, size_t y);
        // helper
        template <typename T>
        static inline void helper_show_all(T* arr, size_t min, size_t max, char *msg);
    };
    
    template <typename T>
    void Algorithms::QuickSort(T* arr, size_t min, size_t max)
    {
        if(min >= max || max == 0 - 1) return;
        helper_show_all(arr, min, max, "before qsort_helper_partition");
        size_t p = qsort_helper_partition(arr, min, max);
        helper_show_all(arr, min, max, "after qsort_helper_partition");
    
        QuickSort(arr, min, p - 1);
        QuickSort(arr, p + 1, max);
    }
    /*
    *    @BUG:    bug200910280001
    *    @DESC:    由于在循环while(true)中,假设原代码
                01    while(true)
                02    {
                03        while(cmp < arr[i])
                04            ++i;
                05        while(arr[j] < cmp)
                06            --j;
                07        if(i >= j) break;
                08
                09        swap(arr, i, j);
                10    }
                中,前两段(行号,行号)中的代码均返回false,
                则无法进入++i或者--j,那么在这个while(true)中,
                i和j的值将无法发生变化,从而导致死循环。
        @LINK:http://www.cppblog.com/mymsdn/archive/2009/03/06/quicksort.aspx#99606
    */
    template <typename T>
    size_t Algorithms::qsort_helper_partition(T* arr, size_t min, size_t max)
    {
        T cmp = arr[min];
        int i = min, j = max; // bug200910280001:修正i = min+1,将+1的动作放在循环内。
        while(true)
        {
            while(cmp < arr[++i]) // bug200910280001:将原本在循环外的min+1,移进循环内,并首先+1
                ; // bug200910280001:将++1移至while条件中。
            while(arr[j] < cmp)
                --j;
            if(i >= j) break;
    
            helper_show_all(arr, min, max, "before swap(arr, i, j)");
            swap(arr, i, j);
            helper_show_all(arr, min, max, "after swap(arr, i, j)");
        }
        swap(arr, min, j);
        return j;
    }
    
    template <typename T>
    void Algorithms::swap(T* arr, size_t x, size_t y)
    {
        T tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }
    
    template <typename T>
    void Algorithms::helper_show_all(T* arr, size_t min, size_t max, char *msg)
    {
        std::cout << "current array :\t";
        for(int i = min; i < max; ++i)
        {
            std::cout << arr[i] << " ";
        }
        std::cout<<"\t//"<<msg;
        std::cout<<std::endl;
    }

    cpp_quickSort.cpp

    // cpp_quickSort.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "Algorithms.h"
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int arr_begin = 0;
        int arr_length = 12; //The length will instead of the magic numbers 
        int arr[] = {8, 3, 7, 1, 5, 6, 2, 1, 9, 9, 1, 1};
        
        // Test for : 20091028bug0001
        // int arr[] = {1, 1, 1};
    
        std::cout << "input array :\t";
        for(size_t i = arr_begin; i != arr_length; ++i)
        {
            std::cout<<arr[i]<<" ";
        }
        std::cout<<std::endl;
    
        Algorithms::QuickSort(arr, arr_begin, arr_length);
    
        std::cout << "result array :\t";
        for(size_t i = arr_begin; i != arr_length; ++i)
        {
            std::cout<<arr[i]<<" ";
        }
        std::cout<<std::endl;
    
        std::cout << "--------------------" << std::endl;
        std::cout << "input array :\t";
    
        std::vector<int> vec;
        vec.push_back(3);
        vec.push_back(1);
        vec.push_back(4);
        vec.push_back(1);
        vec.push_back(7);
        vec.push_back(6);
    
        for(std::vector<int>::iterator iter = vec.begin();
            iter != vec.end(); ++ iter)
        {
            std::cout<<*iter<<" ";
        }
        std::cout<<std::endl;
    
        std::sort(vec.begin(), vec.end());
    
        for(std::vector<int>::iterator iter = vec.begin();
            iter != vec.end(); ++ iter)
        {
            std::cout<<*iter<<" ";
        }
        std::cout<<std::endl;
    
        return 0;
    }
    
    

    posted @ 2009-10-28 00:38 volnet 阅读(295) | 评论 (0)编辑 收藏

    2009年8月7日 #

    公共字符串匹配矩阵(max_match)

    image

    晚上在跟同事聊到字符串匹配的时候,同事提到了矩阵,觉着是个很牛的东西,它的结果如果出现连续的斜线的话,则说明有匹配了,这可以用于做快速搜索,而且也可以搜索最大公共字符串。据说有个很牛的算法专门用来做“最大公共字符串”的,但我还没去找,先将这个矩阵输出试试。

    本程序提供两种输出方式,以参数-w和-wo进行区分,分别用于显示人眼识别的信息,以及可供后续程序进行处理的纯净信息。本程序同时提供通用的帮助查看方式,参数为-h或/?等。

    通过矩阵,我们就很容易得出最大匹配结果。至于如何分析,暂时还没有做考虑,有空会进行分析。

    // max_match.cpp : 定义控制台应用程序的入口点。
    //
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define BOOL int
    #define TRUE 1
    #define FALSE 0
    typedef char element_t;
    #define ele_space ' '
    #define ele_break '\n'
    #define ele_placeholder '*'
    #define ele_end '\0'
    
    element_t *malloc_space(element_t* str1, element_t* str2, BOOL with_illuminate);
    element_t *max_match_matrix(element_t* str1, element_t* str2, BOOL with_illuminate, element_t *matrix);
    /*display the help informations*/
    void man_this(void);
    /*
    purpose:
        Generate a matrix for max match with str1 & str2.
        e.g:
            str1 = "abcdefghijk";
            str2 =    "define";
        The str2 with the same substring "def" in str1.
        Show the relationship between the two string by matrix.
        You can use the pipe for dealing with the result.
    invoke:
        max_match {options} str1 str2
    options:
        @-w(default): Display the result infomations for view.
        @    --with_illuminate: The same function as option -w;
        @-wo: Don't display the result infomations for view.
        @    --without_illuminate: The same function as option -wo;
        @-h: Display the help info for users.
        @    --help: The same function as option -h;
        @    /h: The same function as option -h;
        @    /help: The same function as option -h;
        @    /?: The same function as option -h;
        @    -?: The same function as option -h;
    */
    int main(int argc, char* argv[])
    {
        char *str1, *str2;
        BOOL with_illuminate = TRUE;
        if(argc == 3)    
        {
            str1 = argv[1];
            str2 = argv[2];
        }
        else if(argc == 4)
        {
            if(strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--with_illuminate") == 0)
                with_illuminate = TRUE;
            else if(strcmp(argv[1], "-wo") == 0 || strcmp(argv[1], "--without_illuminate") == 0)
                with_illuminate = FALSE;
            else
            {
                printf("ERROR: error paramaters in!\n");
                return -2;
            }
    
            str1 = argv[2];
            str2 = argv[3];
        }
        else if(argc == 2)
        {
            if(strcmp(argv[1], "-h") == 0
                || strcmp(argv[1], "/h") == 0
                || strcmp(argv[1], "--help") == 0
                || strcmp(argv[1], "/help") == 0
                || strcmp(argv[1], "/?") == 0
                || strcmp(argv[1], "-?") == 0)
            {
                man_this();
                return 0;
            }
        }
        else
        {
            printf("ERROR: No enough paramaters!\n");
            return -1;
        }
        
        if(with_illuminate)
        {
            printf("str1:\t|%s\n", str1);
            printf("str2:\t|%s\n", str2);
            printf("\nresult:\n");
        }
    
        element_t *matrix = malloc_space(str1, str2, with_illuminate);
        printf("%s", max_match_matrix(str1, str2, with_illuminate, matrix));
        free(matrix);
        return 0;
    }
    
    element_t *max_match_matrix(element_t* str1, element_t* str2, BOOL with_illuminate, element_t *matrix)
    {
        int curr = with_illuminate ? 0 : -1;
        if(with_illuminate)
        {
            matrix[curr] = ele_space;
            int i = -1;
            while(str1[++i] != ele_end)
            {
                matrix[++curr] = str1[i];
            }
            matrix[++curr] = ele_break;
        }
        int j = -1;
        while(str2[++j] != ele_end)
        {
            if(with_illuminate)    matrix[++curr] = str2[j];
            int z = -1;
            while(str1[++z] != ele_end)
            {
                if(str1[z] == str2[j])
                    matrix[++curr] = ele_placeholder;
                else
                    matrix[++curr] = ele_space;
            }
            matrix[++curr] = ele_break;
        }
    
        matrix[++curr] = ele_end;
        return matrix;
    }
    
    element_t *malloc_space(element_t* str1, element_t* str2, BOOL with_illuminate)
    {
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        int len = 0;
        if(with_illuminate)
            len = (len1 + 2) * (len2 + 1) + 1;
        else
            len = (len1 + 1) * len2 + 1;
    
        element_t* result;
        if((result = (element_t*)malloc((size_t)(len * sizeof(element_t)))) == NULL)
        {
            printf("ERROR: No enough space!");
            return NULL;
        }
        return result;
    }
    
    void man_this(void)
    {
        printf("%s", "purpose:\n");
        printf("%s", "    Generate a matrix for max match with str1 & str2.\n");
        printf("%s", "    e.g:\n");
        printf("%s", "        str1 = \"abcdefghijk\";\n");
        printf("%s", "        str2 =    \"define\";\n");
        printf("%s", "    The str2 with the same substring \"def\" in str1.\n");
        printf("%s", "    Show the relationship between the two string by matrix.\n");
        printf("%s", "    You can use the pipe for dealing with the result.\n");
        printf("%s", "invoke:\n");
        printf("%s", "    max_match {options} str1 str2\n");
        printf("%s", "options:\n");
        printf("%s", "    @-w(default): Display the result infomations for view.\n");
        printf("%s", "    @    --with_illuminate: The same function as option -w;\n");
        printf("%s", "    @-wo: Don't display the result infomations for view.\n");
        printf("%s", "    @    --without_illuminate: The same function as option -wo;\n");
        printf("%s", "    @-h: Display the help info for users.\n");
        printf("%s", "    @    --help: The same function as option -h;\n");
        printf("%s", "    @    /h: The same function as option -h;\n");
        printf("%s", "    @    /help: The same function as option -h;\n");
        printf("%s", "    @    /?: The same function as option -h;\n");
        printf("%s", "    @    -?: The same function as option -h;\n");
    }
    
    

    posted @ 2009-08-07 00:47 volnet 阅读(185) | 评论 (1)编辑 收藏

    2009年8月2日 #

    Aside: Unix and Posix.

    Copy from “Computer Systems A Programmer’s Perspective(CS:APP)” P12


    The 1960s was an era of huge, complex operating systems, such as IBM’s OS/360 and Honeywell’sMultics systems.
    While OS/360 was one of the most successful software projects in history, Multics dragged on for years and never
    achieved wide-scale use. Bell Laboratories was an original partner in the Multics project, but dropped out in 1969
    because of concern over the complexity of the project and the lack of progress. In reaction to their unpleasant
    Multics experience, a group of Bell Labs researchers — Ken Thompson, Dennis Ritchie, Doug McIlroy, and Joe
    Ossanna — began work in 1969 on a simpler operating system for a DEC PDP-7 computer, written entirely in
    machine language. Many of the ideas in the new system, such as the hierarchical file system and the notion of a
    shell as a user-level process, were borrowed from Multics, but implemented in a smaller, simpler package. In 1970,
    Brian Kernighan dubbed the new system “Unix” as a pun on the complexity of “Multics.” The kernel was rewritten
    in C in 1973, and Unix was announced to the outside world in 1974 [61].
    Because Bell Labs made the source code available to schools with generous terms, Unix developed a large following
    at universities. The most influential work was done at the University of California at Berkeley in the late 1970s and
    early 1980s, with Berkeley researchers adding virtual memory and the Internet protocols in a series of releases called
    Unix 4.xBSD (Berkeley Software Distribution). Concurrently, Bell Labs was releasing their own versions, which
    become known as System V Unix. Versions from other vendors, such as the Sun Microsystems Solaris system, were
    derived from these original BSD and System V versions.
    Trouble arose in the mid 1980s as Unix vendors tried to differentiate themselves by adding new and often incompatible
    features. To combat this trend, IEEE (Institute for Electrical and Electronics Engineers) sponsored an effort
    to standardize Unix, later dubbed “Posix” by Richard Stallman. The result was a family of standards, known as
    the Posix standards, that cover such issues as the C language interface for Unix system calls, shell programs and
    utilities, threads, and network programming. As more systems comply more fully with the Posix standards, the
    differences between Unix version are gradually disappearing. End Aside.

    posted @ 2009-08-02 21:28 volnet 阅读(121) | 评论 (0)编辑 收藏

    Fedora 10 -> Fedora 11 (tentative)

    将Fedora10升级至Fedora11(试验性的)

    只能从Fedora10升级到Fedora11。更早的版本则需要先升级至Fedora10。RPM格式已经为Fedora11做了改变,因此首先必须升级旧的rpmlib。否则进程将出现rpmlib(FileDigests)依赖问题。对新格式的支持已经是Fedora10升级的一部分了。

    yum update rpm

    Fedora 10 -> Fedora 11 (tentative)

    阅读全文

    • New initrd built when installing a new kernel while running Fedora 10 might fail. To solve that boot with an old kernel (to get the new userspace) and (re)install the new kernel.
    • Systems with PAE support (indicated by pae in /proc/cpuinfo) should use kernel-PAE.i686. The new kernel must be changed/installed manually: Set DEFAULTKERNEL=kernel-PAE in /etc/sysconfig/kernel and yum install kernel-PAE. Refer to Dave Jones' blog post for details.
    • The yum update step should NOT be run inside a gnome desktop session/gnome-terminal.
    1. REDIRECT Archive:Template:Bz could result in a unusable install when gnome-terminal segfaults during the upgrade. Update should be run in a vty, runlevel 3, or a screen session.
    • fedora-release-11-1.noarch changes the yum mirrorlist URL so that it uses a "metalink", but the version of yum currently in F10 doesn't understand this syntax, leading to yum downloads failing with this error message:
    YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
     Eg. </metalink>/
    

    This is RHBZ #498720. Workaround is to manually edit the URL in /etc/yum.repos.d/fedora.repo as described at https://www.redhat.com/archives/fedora-list/2009-June/msg00783.html

    • Some packages in Fedora 10 are regarded as newer than those supplied by Fedora 11 and its updates repository. These include ntpd, ntpdate (RHBZ #506040, RHBZ #504980), unique, unique-devel, eclipse-changelog, eclipse-svnkit and svnkit. You may wish to remove these before performing the upgrade, then reinstall them afterwards. Doing so may require --nodeps.
    • Some i386 packages in Fedora 10 are replaced with i586, i686 or x86_64 packages in Fedora 11. These include gpm.i386, glibc-2.9-3.i386. You may wish to remove these before performing the upgrade, then reinstall them afterwards. Doing so may require --nodeps.
    • mplayer-1.0-0.104.20090204svn.fc10 from the RPM Fusion repository has a dependency on libfaad.so.0 that the depsolve doesn't find, but rpm_check_debug does. You may wish to remove mplayer before performing the upgrade, then reinstall them afterwards. Doing so may require --nodeps.

    posted @ 2009-08-02 10:14 volnet 阅读(278) | 评论 (0)编辑 收藏

    2009年6月3日 #

    DOS命令subst

    C:\Documents and Settings\a>help subst
    Associates a path with a drive letter.

    SUBST [drive1: [drive2:]path]
    SUBST drive1: 
    /D

      drive1:        Specifies a 
    virtual drive to which you want to assign a path.
      [drive2:]path  Specifies a physical drive and path you want to assign to
                     a 
    virtual drive.
      
    /D             Deletes a substituted (virtual) drive.

    Type SUBST with no parameters to display a list of current 
    virtual drives.
    可以将一个文件夹虚拟成一个驱动器。
    记录一下。

    posted @ 2009-06-03 15:44 volnet 阅读(205) | 评论 (0)编辑 收藏

    仅列出标题  下一页