Sean's Note

2010年11月28日 星期日

Context Switch

當發生中斷時,作業系統必須暫存目前程序的 context,載入新程序的

context,而 context 是以 PCB (Process Control Block) 來表示。

PCB 包含了許多資訊如 :

Process state : new, ready, running, waiting, halted 等等。

Program counter : 存放下一行要執行的指令的位址。

CPU registers : 依不同的電腦架構而有不同,如 accumulators, stack pointers 等等。

CPU-scheduling information : 程序的優先權和排程的參數等等。

Memory-management information : page tables, segment tables 等等。

Accounting information : CPU 數量、程序數量等等。

I/O status information : 配置的 I/O 裝置和開啟的檔案。

什麼是 IP 分享器? Hub? Switch Hub?

IP 分享器
通常和電信業者申請 ADSL 的時候,只會給一個固定 IP,這時候家裡若想要多台
電腦同時上網,就必須透過 IP 分享器來分配虛擬 IP 給其他電腦,192.168.0.0 ~
192.168.255.255,而這些電腦都會統一透過 IP分享器上的固定 IP 與外界的 Internet
溝通。

Hub
這是假設 IP 分享器後面的埠只有 8 個,但想要連接 8 台以上的電腦,這時就可以
用 Hub ,就好像電腦主機只有兩三個 USB 埠,但想要接很多裝置時,就可以買一
個 USB Hub 來擴充。Hub 屬於半雙工,只收或送。


Switch Hub
與 Hub 相似,不同的是 Switch 的 port 會記錄每台電腦的 MAC,在傳輸封包時,
只會傳給擁有該 MAC 的電腦,不像單純的 Hub 是用廣播的方式。而且 Switch 是
屬於全雙工,可以同時傳送和接收資料,所以價格也比單純的 Hub 來的貴一點。



2010年11月24日 星期三

[C/C++] 常見的型態運算錯誤

char a = 200;
char b = 200;
int c;

c = a + b;
c 會是多少呢?

答案是 -112 而不是 400
因為 a 和 b 實際上都為 -56,所以 -56 + -56 = -112

2010年10月17日 星期日

[C/C++] structure and class

Class is equal to structure plus member function, but a C++ structure can do any thing a class can do!
Only their default access level are different.
Structure's fields are public by default.
Class fields are private by default.

2010年10月16日 星期六

[C/C++] Default Arguments

在 C++ 裡,函式中的參數是可以在定義的時候給初始值的 (C 不支援)。
// definition
void a (int b, int c = 1, int d = 1);

int main ()
{
    a(1, 2, 3);
    a(1, 2);
    a(1);
}

// implementation
void a (int b, int c, int d)
{
    ...
}

2010年10月15日 星期五

[BCB] 擷取檔案路徑與副檔名的函式

String str = "C:\\Program Files\\Borland\\CBuilder6\\Projects\\Project1.exe";

Label1->Caption = ExtractFileName(str);
// Project1.exe


Label2->Caption =  ExtractFilePath(str);   
 // C:\Program Files\Borland\CBuilder6\Projects\


Label3->Caption = ExtractFileDir(str);     
 // C:\Program Files\Borland\CBuilder6\Projects


Label4->Caption =  ExtractFileDrive(str);  
 // C:


Label5->Caption = ExtractFileExt(str);      
// .exe



2010年10月6日 星期三

[C/C++] Data structure alignment

通常 structure 中都會以 size 最大的成員作為 alignment 的單位,以提高運算效率。
以 struct A1 來說,最大為 double 所以 alignment 的單位為 8 bytes,
sizeof( struct A1) 就會是 4 + 4 + 8 + 8 = 24 bytes。
sizeof( struct A2) 就會是 4 + 4 + 8 + 1 + p(7)  + 8 = 32 bytes,
padding 了 7 bytes。
struct A1
{                            
    int a;  
    char b[12];
    double c;        
};

struct A2
{                              
    int a;    
    char b[13]; 
    double c;          
};
使用虛擬指令 #pragma pack(n) : 編譯器將按照 n bytes 對齊。
使用虛擬指令 #pragma pack() : 取消自定義對齊方式。
#pragma pack(2)
struct D
{                            
    int a;  
    char b;        
};
#pragma pack()
如果指定的 n 大於 structure 中成員的最大 size 將不起作用,仍依 size 最大的成員
進行對齊。sizeof( struct D) 就會是 4 + 1 + p(1) = 6 bytes。