2015年6月14日 星期日

Compile QEMU in ubuntu 14.04

To compile QEMU in ubuntu 14.04, you need to download the needed libraries first
sudo apt-get install zlib1g-dev libglib2.0-dev autoconf automake libtool
If you need to browse the execution screen via a window in host instead of using VNC, you will need to install gtk.
sudo apt-get install build-essential libgtk2.0-dev libvte-dev
Then command ./configure --target-list="What you desire" --enable-gtk.
ex: ./configure --target-list="x86_64-softmmu" --enable-gtk
Then command make, and it will make the files in some directory, ex:"x86_64-softmmmu".
Btw, you can use -j8 to accelerate with 8 threads,' there is no space between 'j' and8', unlike compiling linux kernel.

pthread

除了要#include <pthread.h>
編譯的時候還要下-pthread指令
ex:gcc test.c -o test -pthread

pthread_create(pid,NULL,*void,(*void)arg);
第三項是pthread要跑的function位置,這個function只能接受一個argument傳入,限定為void type,有需求傳入多個arguments時,定一個struct把需要的arguments都包起來。可以回傳值,所以也限定為void*,因此function定義時要定成void* mydeffunc(void* arg)

ex:


void *doit(void *arg);

/*define a struct to wrap needed arguments*/
struct myDefStruct
{
struct sockaddr_in servaddr;
int sockfd;
};

/*call pthread_create的時候*/
/*when call pthread_create*/
struct myDefStruct *myDefPtr;
for ( ; ; ) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (__SOCKADDR_ARG) &cliaddr, &clilen);
myDefPtr= (struct myDefStruct*)malloc(sizeof(struct myDefStruct));
myDefPtr->servaddr = cliaddr;
myDefPtr->sockfd = connfd;
pthread_create(&tid, NULL, &doit, (void*)myDefPtr);
 }


/*如何取出void*型別的資訊*/
/*How to retrieve the data in void* type by casting*/
void *doit(void *arg)
{
int sockfd = ((struct myDefStruct*)arg)->sockfd;
struct sockaddr_in servaddr = ((struct myDefStruct*)arg)->servaddr;
...............
}

用gcc編譯c++的檔案

要加-lstdc++指令
ex:gcc test.cc -lstdc++ -o test

unix下include math.h、cmath

在compile的時候要加上-lm的指令
ex: gcc test.c -o test -lm

2015年2月7日 星期六

2014年10月26日 星期日

Dynamic Loading、Static Linking、Dynamic Linking

Dynamic Loading:可在執行階段遇到需要的程式碼再load,好處是error handling的code不用被全部塞在memory裡面
Static Linking:沒什麼特別,假設好幾個程式都include stdio.h,就會有好幾份程式碼分布在記憶體當中,很冗
Dynamic Linking:假設好幾個程式都include stdio.h,只load一份,大家共用。程式內指向Lib的部分稱為stub,當作出對Library的要求,會檢查這個Library有沒有被loaded在memory中,如果沒有就load,Windows下常見的Dll(Dynamic link library)檔就是這個

Dynamic Loading無法解決Static Linking重複code的問題!Loadin和Linking分清楚

疑問:寫好的程式第一次執行總是慢很多跟這個有沒有關聯?

Memory Addressing-1 Address Binding:Compile Time、Load time、Execution Time

Compile Time
assembly code

.BASE 0x1000
.START
PUSH AX
MOVE AX, 3
MULT AX, 7
MOVE (0x1018), AX
CALL print, (0x1018)
POP AX
.END
.SPACE (4)

#在硬碟中的情況
PUSH AX
MOVE AX, 3
MULT AX, 7
MOVE (0x1018), AX
CALL print, (0x1018)
POP AX

#Memory中的內容

要改變記憶體位置就得重新compile

Load Time
assembly code

.START
PUSH AX
MOVE AX, 3
MULT AX, 7
MOVE (.BS+0x18), AX
CALL print, (.BS+0x18)
POP AX
.END
.SPACE (4)

#在硬碟中的情況
PUSH AX
MOVE AX, 3
MULT AX, 7
MOVE (0x2018), AX
CALL print, (0x2018)
POP AX

#Memory中的內容

要改變記憶體位置要重開程式

Execution Time
assembly code

.START
PUSH AX
MOVE AX, 3
MULT AX, 7
MOVE (0x18), AX
CALL print, (0x18)
POP AX
.END
.SPACE (4)

#在硬碟中的情況
PUSH AX
MOVE AX, 3
MULT AX, 7
MOVE (0x18), AX
CALL print, (0x18)
POP AX

#Memory中的內容

不論在實體記憶體中的哪個部分都沒問題,會透過MMU把虛擬的位置轉成實體


Logical Address、Physical Address
在Compile Time和Load Time:Logical Address=Physical Address
在Execution Time:Logical Address!=Physical Address

疑問:如果MMU可以吃超過32個bit,整個OS是不是能調度超過4GB呢?