2/07/2017

Processes and Threads

相信這兩者差別,只要是做系統的人應該都會很了解。但對於我而言,卻相當模糊。記得有次在跟老外同學閒聊的時候,他問我這兩者的差別是在哪 ? 在當下我突然發現,要講給人懂是一件很困難的事,尤其是在對某個東西半理解狀態,更難解釋。所以這篇文章就來淺淺的探討有關這兩者間的區別。

在現今每個人幾乎都有智慧型手機,而且可以同時執行超多程式,像是你在玩遊戲的同時,能夠接收到朋友的訊息、郵件或其它通知訊息。就跟在用桌機或筆電一樣,可以同時開啟好幾個應用程式。簡單來說,Process 就是 Program,就是一個應用程式,像是在手機上的 App,電腦上的 MicroSoft Word;而  Thread 就是這個 Process 裡面的一個單位執行緒 ( Unit of execution) ,如果打開了 MicroSoft Word (Process),裡面的就有很多個Threads 在跑,像是 keyboard, print, save ...etc,就這是所謂的 Multiple-thread application。

Background
在談到Process和thread,或是說運行一個App,有三個非常重要的東西來負責管理:CPU, Memory (RAM), Memory controller。Memory controller 是一個介於CPU和RAM的界面,負責傳輸Data到CPU。當我們寫好一個程式,然後compile成binary code,這些binary code就會存放在RAM,接著透過 controller 把 data 傳到CPU。CPU內部有一個 queue line,也就是pipe line,依照FILO的原則放進這line當中來等待執行。

Multiple-thread application
那到底是如何實現multiple thread和multiple process ? 以Linux為例子,每一個process都會有PID, memory space, priority 和 schedule,依據priority和schedule來切換所有processes,這切換時間就叫 time slot,通常是在 1ms。

而一個process又有多個threads,這些threads 一般都是來靠 "鎖" (Lock) 來管理。如果這鎖沒管理好,很容易造成這個process當掉,更嚴重則是系統崩潰。像是我們常聽到 "死鎖" (Dead Lock) 就是因為 Thread 1 等待 Thread 2,而 Thread 2 又等待 Thread 1 釋放鎖而造成死循環。所以如何設計好這些threads 對於一個program or process 是非常重要的。

2/02/2017

What are advantages of knowing C language if you don't even know it ?

今天在Quora上面看到有人提問說 : 到底了解C語言有什麼好處 ?

這問題讓我印像滿深刻的。在我就讀期間,為了要找一份實習工作,我不得不從embedded system領域轉換成Web development。剛開始的確有點不太適應,總覺得很多寫法都不一樣。到後面才了解其實不是對Javascript不適應,正確來說是對Web和DOM概念不熟悉才是。

不管是Java或是Javascript,如果對C有一定程度熟悉的話,掌握它們的基本語法其實用不到二天的時間。就像上面有人舉了這個例子 :

試著用Javascript去run下這行code :


1
2
3
4
5
6
7
8
9
var myNumber = 4;
var myArray = [4];
makeChanges( myNumber, myArray );
console.log( myNumber + " and " +  myArray); 

function makeChanges( aNumber, anArray ) {
 aNumber++;
 anArray[0]++;
}

你會得到 "4 and 5" 的結果。如果了解C,一定馬上就知道為什麼一個任意數值並不會因為call function 被改變,而array會。

因為C有pointer,而pointer的作用其實跟array是一樣的。如果它們被當作某一個function的參數時,這時就等同於把這個值(被指向或是一個陣列)的地址一起傳送過去。所以這個function真正做的事 : 訪問這個變數的地址,改變這個地址的值。

可是任意數值並沒有。雖然一開始它們己經被賦予了某個值,存放在某個記憶體位址,可是當它們被當作做某個function的參數時,這function是沒辦法訪問這個變數的地址,它能改變的值只能作用於這個function的執行緒裡面,並不能影響其它執行緒的訪問。所以在跳出該function的時候,另一個執行緒訪問同樣的變數其實並不受任何影響。簡單來說,makeChanges所訪問aNumber的地址和外部訪問的myNumber的記憶體位址其實是完全不一樣的。

所以大家才會說,pointer其實是無所不在的,只是被藏起來罷了,因為它真的不好理解。但就如上面有人說 :

電腦硬體是很難以理解的。我們越不去看它,寫出一個程式去叫電腦做些事是越容易。 但我們越深入了解它,我們就更加理解真正要處理問題和原因是什麼。


1/27/2017

Distributed Databases - Bond Energy Algorithm

這個演算法算是在去唸書期間,以第一學期來說,算是最麻煩的,即使公式都給了。這門課是講關於分散式系統資料庫,對我來說跟天書沒什麼兩樣,因為那時連資料庫碰都沒碰過,一點概念都沒有。而且現在才紀錄下來好像太晚了點.....都忘了差不多了XDD

總而言之,這門課的第一作業就是叫我們實現Bond Energy Algorithm。在講它之前,有必要先講一些分散式資料庫的簡單觀念說一下。

分散式資料庫是一群資料庫系統(或是所謂的節點)的集合,它們都是透過網路來連結,而且彼此互相之間是有關聯的。它被應用在非常廣的範圍,像是Google 就是用分散式系統管理資料庫,不然一個資料點掛了就全部陣亡。舉例來說,小明在台北市查找一本書。實際上這連線是不止會傳回台北所在的資料,也會向其它縣市的資料庫進行查尋(如果沒有或是全部查找)。

在分散式資料庫系統中,我們通常須將存放資料的表格進行分割,並放在多個資料庫節點上。而這分割又被分為Horizontal / Vertical fragmentation。而BEA就是在設計Vertical Fragmentation Algorithm時,基於Attribute affinity values,找出一組Attirbutes的關聯的意義。

BEA的演算法公式如下 :




1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Bond Energy algorithm
        input: AA (Attribute Affinity matrix)
        output: CA (Clustered Affinity matrix)
     */

     //copy old AA matrix to new AA in order to avoid index mistake
     AAnew = new double[MATRIX_SIZE+1][MATRIX_SIZE+1];
     CA = new double[MATRIX_SIZE+2][MATRIX_SIZE+2];
     tempColumn = new double[MATRIX_SIZE+1];

     for(int i = 1; i < MATRIX_SIZE+1; i++) {
       for(int j = 1; j < MATRIX_SIZE+1; j++) {
         AAnew[i][j] = AA[i-1][j-1];
        // System.out.println("AAnew["+i+"]["+j+"] = "+AAnew[i][j]);
       }
     }

     //fill columns and rows
     for(int i = 0; i < MATRIX_SIZE+1; i++) {
       CA[0][i] = i;
      for(int j = 0; j < MATRIX_SIZE+1; j++) {
         CA[j][0] = j;
       }
     }

     for(int i = 0; i < MATRIX_SIZE+1; i++) {
       AAnew[0][i] = i;
       for(int j = 0; j < MATRIX_SIZE+1; j++) {
         AAnew[j][0] = j;
       }
     }


     // Copy the first and second columns from AA
     for(int i = 1; i < MATRIX_SIZE+1; i++) {
       CA[i][1] = AAnew[i][1];
       CA[i][2] = AAnew[i][2];
     }

     index = 3; // start from third index
     RightMostIndex = 2; //current rightmost index of AA.
     while(index <= MATRIX_SIZE) {
       double contrib = 0;
       double maxContrib = 0;
       //System.out.println("------ index = "+index);

       for(int i = 1; i < index; i++) {
          contrib = cont(CA[0][i-1], index, CA[0][i], AAnew);
          //if found the highest contribution, record it
          if(contrib > maxContrib) {
            maxContrib = contrib;
            record(CA[0][i-1], index, CA[0][i]);
          }
        }
        contrib = cont(CA[0][index-1], index, index+1, AAnew);

        if(contrib > maxContrib) {
          maxContrib = contrib;
          record(CA[0][index-1], index, index+1);
        }
       /* place the highest contribution */
       CAplacement();
       index++;
     }
     CArowplace();
     CAprint();
   }

   /* This function is to order rows according the placement of columns */
   public static void CArowplace() {
     //double[][] temp;
     CAfinal =  new double[MATRIX_SIZE+2][MATRIX_SIZE+2];

     for(int i = 0; i <= MATRIX_SIZE; i++) {
       double row = CA[0][i];
       CAfinal[0][i] = row;

       for(int j = 0; j <= MATRIX_SIZE; j++) {
         //temp[i][j] = CA[(int)row][j];
         //System.out.println("row ="+(int)row);
         CAfinal[i][j] = CA[(int)row][j];
       }
     }
   }


看起來雖然滿複雜,但實現這公式是滿簡單的。麻煩的是要算出AA matrix。當時老師不打算用課本上的演算法,改用一個叫做 Cosine-based similarity 的算法。所以AA matrix 的算法變成如下 :




1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Start - Attribute Affinity algorithm */
     AA = new double[MATRIX_SIZE][MATRIX_SIZE];
     DecimalFormat df = new DecimalFormat("0.0000");

     for(int i = 0; i < att.size(); i++) {
       for(int j = 0; j < att.size(); j++) {
         double Aik = 0;
         double Ajk = 0;
         double AffNumerator = 0;
         double AffDenominator = 0;
         double temp = 0;
         double temp1 = 0;
         double TempSqrt = 0;
         double Temp1Sqrt = 0;
         for(int k = 0; k < query.size(); k++) {
           double SumAcc = 0;
           /* sum all accMatrix then multiply use(qk,Ai)*/
           for(int site = 0; site < 3; site++) {
             SumAcc = SumAcc + accMatrix[k][site];
           }
           Aik = beQueried[k][i] * SumAcc;
           Ajk = beQueried[k][j] * SumAcc;

           AffNumerator = AffNumerator + (Aik*Ajk);
           temp = temp + Math.pow(Aik,2);
           temp1 = temp1 + Math.pow(Ajk,2);
         }
         TempSqrt = Math.sqrt(temp);
         Temp1Sqrt = Math.sqrt(temp1);
         AffDenominator = TempSqrt * Temp1Sqrt;
         //System.out.println("Affn = "+AffNumerator);
         //System.out.println("Affd = "+AffDenominator);
         AA[i][j] = Double.parseDouble(df.format(AffNumerator / AffDenominator));
         System.out.println("AA["+i+"]["+j+"] = "+AA[i][j]);
       }
     }


這個程式都放在Github,需要的人請自取

整個過程都有註解,所以應該還滿好理解,只是執行速度太慢就是了...而且當時寫這作業沒想其它的,看起來真有點亂...

1/17/2017

I'm back

On 17th of November, I finally completed my degree of Master in Australia. This three-year journey took me a lot of efforts on assignments and projects, but really benefited from those activities during the study. To be honest, I'd never thought that one day I would have been able to graduate from foreign schools and even worked on there. I've truly appreciated my parents who give me such an opportunity to study abroad, and also thankful my girlfriend company with me in the long distance.  Of course, there are many people who care about me that I really appreciate as well.

Although I'm still studying IELTS test in order to apply for PR visa after the graduation ( under 0.5 in part of listening...), updating what I've learnt from the university on this blog is my next step! I hope that by writing the blog, I could be able to organise my thoughts and review the things I learnt.

By the way, if anyone would like to look for a partner for practising IELTS speaking or something else, you can just simply send an email to me :)