你可能不知道Windows系統下有一個UNIX子系統

來源:互聯網
上載者:User

作者:朱金燦

來源:http://www.cnblogs.com/clever101

 

 請看下面一段代碼:

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
    struct _stat buf;
    int result;
    char timebuf[26];
    char* filename = "F:\\MyTest\\MyTest\\src\\TestUnix\\stdafx.h";
    errno_t err;

    // Get data associated with "crt_stat.c": 
    result = _stat( filename, &buf );

    // Check if statistics are valid: 
    if( result != 0 )
    {
        perror( "Problem getting information" );
        switch (errno)
        {
        case ENOENT:
            printf("File %s not found.\n", filename);
            break;
        case EINVAL:
            printf("Invalid parameter to _stat.\n");
            break;
        default:
            /* Should never be reached. */
            printf("Unexpected error in _stat.\n");
        }
    }
    else
    {
        // Output some of the statistics: 
        printf( "File size     : %ld\n", buf.st_size );
        printf( "Drive         : %c:\n", buf.st_dev + 'A' );
        err = ctime_s(timebuf, 26, &buf.st_mtime);
        if (err)
        {
            printf("Invalid arguments to ctime_s.");
            return 1;
        }
        printf( "Time modified : %s", timebuf );
    }

    getchar();
    return 0;
}

 

      

熟悉unix或linux平台開發的朋友可能以為它是在unix或linux平台開發的。實際上它是MSDN Library for Visual Studio 2005上的一個例子,可以在VS C++ 2005上成功編譯(多位元組字元集編譯)。

 

開始我也不太明白為何Windows系統有unix的標頭檔。最近看了《Windows作業系統原理》才徹底明白了。究其原因,Windows 2000/XP有三種環境子系統:POSIX/UNIX、OS/2和Win32(OS/2隻能用於x86系統)。因為有了POSIX/UNIX這個子系統,自然也就有了一個UNIX SDK,所以出現上面的標頭檔就不足為奇了。事實上微軟的發展和unix系統是有一些淵源的:微軟曆史上曾推出過unix作業系統;windows系統的很多核心開發人員都是資深的unix作業系統的設計和開發人員。

 

那麼這個POSIX/UNIX這個子系統有什麼用呢?POSIX代表了UNIX類型的作業系統的國際標準集,它鼓勵製造商實現相容的UNIX風格介面,以使編程者很容易地將他們的應用程式從一個系統移到另一個系統。正因為具有這個特點,很多軟體底層(想實現跨平台)、跨平台庫喜歡調用UNIX SDK中的函數。我在著名的開源映像庫GDAL就見過UNIX SDK中的函數。不過Windows 2000/XP只實現了POSIX.1標準(ISO/IEC 9945-11990或IEEE POSIX 1003.1-1990)。所需的POSIX一致性文檔位於Platform SDK中的"HELP目錄中。

 

參考文獻:

1.     MSDN Library for Visual Studio 2005,Microsoft Corporation

2.     《Windows作業系統原理》,尤晉元 史美林 陳向群 向勇 王雷 馬洪兵 鄭扣根 馬洪兵 編著

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.