2016年8月19日 星期五

雙指標

附上程式碼:

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    char *str1 = "apple";
    char *str2 = "blue";
    char *str3 = "cat";
    
    char **mystr;
    
    mystr = (char **)malloc(sizeof(char *) * 3);
    mystr[0] = (char *)malloc(sizeof(char) * strlen(str1));
    mystr[1] = (char *)malloc(sizeof(char) * strlen(str2));
    mystr[2] = (char *)malloc(sizeof(char) * strlen(str3));
    strcpy(mystr[0], str1);
    strcpy(mystr[1], str2);
    strcpy(mystr[2], str3);
    
    printf("mystr的記憶體位置       : %x \t mystr存放的值           : %x\n\n", &mystr, mystr);
    
    printf("mystr[0]的記憶體位置    : %x \t mystr[0]存放的值        : %x\n", &mystr[0], mystr[0]);
    printf("mystr[1]的記憶體位置    : %x \t mystr[1]存放的值        : %x\n", &mystr[1], mystr[1]);
    printf("mystr[2]的記憶體位置    : %x \t mystr[2]存放的值        : %x\n\n", &mystr[2], mystr[2]);
    
    printf("mystr[0][0]的記憶體位置 : %x \t mystr[0][0]存放的值     : %c\n", &mystr[0][0], mystr[0][0]);
    printf("mystr[0][1]的記憶體位置 : %x \t mystr[0][1]存放的值     : %c\n", &mystr[0][1], mystr[0][1]);
    printf("mystr[0][2]的記憶體位置 : %x \t mystr[0][2]存放的值     : %c\n", &mystr[0][2], mystr[0][2]);
    printf("mystr[0][3]的記憶體位置 : %x \t mystr[0][3]存放的值     : %c\n", &mystr[0][3], mystr[0][3]);
    printf("mystr[0][4]的記憶體位置 : %x \t mystr[0][4]存放的值     : %c\n\n", &mystr[0][4], mystr[0][4]);
    
    printf("mystr[1][0]的記憶體位置 : %x \t mystr[1][0]存放的值     : %c\n", &mystr[1][0], mystr[1][0]);
    printf("mystr[1][1]的記憶體位置 : %x \t mystr[1][1]存放的值     : %c\n", &mystr[1][1], mystr[1][1]);
    printf("mystr[1][2]的記憶體位置 : %x \t mystr[1][2]存放的值     : %c\n", &mystr[1][2], mystr[1][2]);
    printf("mystr[1][3]的記憶體位置 : %x \t mystr[1][3]存放的值     : %c\n\n", &mystr[1][3], mystr[1][3]);
    
    printf("mystr[2][0]的記憶體位置 : %x \t mystr[2][0]存放的值     : %c\n", &mystr[2][0], mystr[2][0]);
    printf("mystr[2][1]的記憶體位置 : %x \t mystr[2][1]存放的值     : %c\n", &mystr[2][1], mystr[2][1]);
    printf("mystr[2][2]的記憶體位置 : %x \t mystr[2][2]存放的值     : %c\n", &mystr[2][2], mystr[2][2]);
    
    return 0;
}

輸出結果:


畫成大家比較看得懂的架構:


2016年7月27日 星期三

linux 修改檔案的權限

chmod:設定檔案及目錄讀取屬性及狀態/Change Mode of File or Direcotry



語法 : chmod [-cfvR] [--help] [--version] mode file... 

說明 : Linux/Unix 的檔案存取權限分為三級 : 檔案擁有者、群組、其他。利用 chmod 可以藉以控製檔案如何被他人所存取。 

參數: 
mode : 權限設定字串,格式如下 : [ugoa...][+-=][rwxX]...][,...],其中u 表示該檔案的擁有者,g 表示與該檔案的擁有者屬於同一個群體(group)者,o 表示其他以外的人,a 表示這三者皆是。 

+ 表示增加權限、- 表示取消權限、= 表示唯一設定權限。 
r 表示可讀取,w 表示可寫入,x 表示可執行,X 表示只有當該檔案是個子目錄或者該檔案已經被設定過為可執行。 
-c : 若該檔案權限確實已經更改,才顯示其更改動作 
-f : 若該檔案權限無法被更改也不要顯示錯誤訊息 
-v : 顯示權限變更的詳細資料 
-R : 對目前目錄下的所有檔案與子目錄進行相同的權限變更(即以遞回的方式逐個變更) 
--help : 顯示輔助說明 
--version : 顯示版本 

範例 :將檔案 file1.txt 設為所有人皆可讀取 : 
chmod ugo+r file1.txt 

將檔案 file1.txt 設為所有人皆可讀取 : 
chmod a+r file1.txt 

將檔案 file1.txt 與 file2.txt 設為該檔案擁有者,與其所屬同一個群體者可寫入,但其他以外的人則不可寫入 : 
chmod ug+w,o-w file1.txt file2.txt 

將 ex1.py 設定為只有該檔案擁有者可以執行 : 
chmod u+x ex1.py 

將目前目錄下的所有檔案與子目錄皆設為任何人可讀取 : 
chmod -R a+r * 

此外 chmod 也可以用數字來表示權限 
例如: chmod 777 file 

語法為﹕chmod abc file 

其中a,b,c各為一個數字,分別表示User、Group、及Other的權限。 

r=4,w=2,x=1 
若要rwx屬性則4+2+1=7; 
若要rw-屬性則4+2=6; 
若要r-x屬性則4+1=7。 

範例﹕ 
chmod a=rwx file 

和 
chmod 777 file 

效果相同 
chmod ug=rwx,o=x file 

和 
chmod 771 file 

效果相同 


若用chmod 4755 filename可使此程式具有root的權限 

進階用法:只對「檔案」或只對「目錄」chmod (recursively) 
find -type d -print0 |xargs -0 chmod 755 
find -type f -print0 |xargs -0 chmod 644 

chgrp 或 chown 的用法亦同。



chown: 設定檔案或目錄之「擁有者」 / Change Owner



語法 : chown [-cfhvR] [--help] [--version] user[:group] file... 

說明 : 
Linux/Unix 是多人多工作業系統,所有的檔案皆有擁有者。利用 chown 可以將檔案的擁有者加以改變。一般來說,這個指令只有是由系統管理者(root)所使用,一般使用者沒有權限可以改變別人的檔案擁有者,也沒有權限可以自己的檔案擁有者改設為別人。只有系統管理者(root)才有這樣的權限。 

參數 : 
user : 新的檔案擁有者的使用者 ID 
group : 新的檔案擁有者的使用者群體(group) 

-c : 若該檔案擁有者確實已經更改,才顯示其更改動作-f : 若該檔案擁有者無法被更改也不要顯示錯誤訊息 
-h : 只對於連結 (link) 進行變更,而非該 link 真正指向的檔案 
-v : 顯示擁有者變更的詳細資料 
-R : 對目前目錄下的所有檔案與子目錄進行相同的擁有者變更(即以遞回的方式逐個變更) 
--help : 顯示輔助說明 
--version : 顯示版本 

範例 : 
將檔案 file1.txt 的擁有者設為 users 群體的使用者 jessie : 
chown jessie:users file1.txt 

將目前目錄下的所有檔案與子目錄的擁有者皆設為 users 群體的使用者 lamport : 
chmod -R lamport:users * 


來源:http://mepopedia.com/forum/read.php?135,420#chmod.EF.BC.9A.E8.A8.AD.E5.AE.9A.E6.AA.94.E6.A1.88.E5.8F.8A.E7.9B.AE.E9.8C.84.E8.AE.80.E5.8F.96.E5.B1.AC.E6.80.A7.E5.8F.8A.E7.8B.80.E6.85.8B.2FChange+Mode+of+File+or+Direcotry

2016年6月23日 星期四

利用Linux的Traffic Control做網路的Scheduling

查看 tc rule指令
tc qdisc show
tc class show dev eth0
tc filter show dev eth0

刪除原本的tc rule
tc disc del dev eth0 root

1. pfifo_fast

當interface啟動的時候,系統自動預設的

2. TBF

tc disc add dev eth0 root tbf rate 200kbit latency 50ms burst 1540

latency : 每個Packet待在Queue的最大時間 (queue delay).
burst : Queue的大小,以bytes為單位.

3. SFQ

tc disc add dev eth0 root sfq perturb 10

perturb : 多久重新產生隨機配置 (散列算法),通常以10秒為一個單位.

4. HTB

//handle是Tag,格式為major:major
//如果是一條queueing discipline,minor需要一直為0
tc qdisc add dev eth0 root handle 1:0 htb

//parent為新增一個class到handle上
//classid為在handle上的一個id,minor需要為非0值
//ceil為要限制的rate
tc qdisc add dev eth0 parent 1:1 classid 1:6 htb rate 256kbit ceil 512kbit

//新建一個classid為1:1,rate為100kbps
tc class add dev eth0 parent 1: classid 1:1 htb rate 100kbps ceil 100kbps

//建立兩個classid為1:10和1:11
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 40kbps ceil 100kbps
tc class add dev eth0 parent 1:1 classid 1:11 htb rate 60kbps ceil 100kbps

//建立哪些packet要進入哪個class
tc filter add dev eth0 protocol ip parent 1:0 prior 1 u32 match ip src 1.2.3.4 match ip sport 80 0xffff flowed 1:10
tc filter add dev eth0 protocol ip parent 1:0 prior 1 u32 match ip src 1.2.3.4 flow 1:11

//將class加入到queueing discipline
tc qdisc add dev eth0 parent 1:10 handle 20: fifo limit 5
tc qdisc add dev eth0 parent 1:11 handle 30: sfq perturb 10

針對output的部分

eth0網卡Download速度為200Kbps
class 10:10 prio 0 for 優先使用者或伺服器
class 10:20 prio 1 for 一般
class 10:30 prio 2 for 濫用
# 刪除現有的規則
tc qdisc del dev eth0 root
# root
tc qdisc add dev eth0 root handle 10: htb default 20
# root 10:1 class
tc class add dev eth0 parent 10: classid 10:1 htb rate 200kbps ceil 200kbps
# 10:10 class
tc class add dev eth0 parent 10:1 classid 10:10 htb rate 100kbps ceil 200kbps prio 0
tc qdisc add dev eth0 parent 10:10 handle 101: pfifo
# 將 MARK為 10的歸類於 class 10:10
tc filter add dev eth0 parent 10: protocol ip prio 100 handle 10 fw classid 10:10
# 10:20 class
tc class add dev eth0 parent 10:1 classid 10:20 htb rate 70kbps ceil 150kbps prio 1
tc qdisc add dev eth0 parent 10:20 handle 102: pfifo
# 10:30 class
tc class add dev eth0 parent 10:1 classid 10:30 htb rate 30kbps ceil 100kbps prio 2
tc qdisc add dev eth0 parent 10:30 handle 103: pfifo
# 將 MARK為 30的歸類於 class 10:30
tc filter add dev eth0 parent 10: protocol ip prio 100 handle 30 fw classid 10:30
iptables -t mangle -A POSTROUTING -d 192.168.0.135 -j MARK –set-mark 10             目的端IP為192.168.0.135優先權為0
iptables -t mangle -A POSTROUTING -d 192.168.0.32 -j MARK –set-mark 30               目的端IP為192.168.0.32優先權為2
來源:http://ssorc.tw/274



針對input的部分

首先需要加載內核模塊,啟用虛擬網卡設備ifb0
  1. [ root@test ~] # modprobe ifb
  2. [ root@test ~] # ip link set dev ifb0 up
然後對入站流量做設置,鏡像到ifb0上
  1. [ root@test ~] # tc qdisc add dev eth0 ingress
  2. [ root@test ~] # tc filter add dev eth0 parent ffff: protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0
然後我們就可以對ifb0接口的out方向流量做各種限制了
  1. [ root@test ~] # tc qdisc add dev ifb0 root handle 1: htb default 1
  2. [ root@test ~] # tc class add dev ifb0 parent 1: classid 1:1 htb rate 800kbit ceil 800kbit burst 80k
  3. [ root@test ~] # tc filter add dev ifb0 parent 1: prio 1 protocol ip u32 match ip dst 1.1.1.1/32 match ip dport 80 0xffff flowid 1:1
來源:http://chunchaichang.blogspot.tw/2016/07/tc-ingress.html

2016年4月27日 星期三

編譯linux kernel

1.將linux kernel source code放置/usr/src/
2.清除暫存檔輸入指令sudo make mrproper
3.使用menuconfig產生.config,安裝menuconfig指令sudo apt-get install libncurse-dev
4.產生.config指令sudo make menuconfig
5.編譯kernel指令sudo make -j 4 all
6.產生belmage指令ll arch/x86/boot/bzlmage
7.安裝kernel指令make modules_install
8.再輸入make install
9.檢查kernel是否安裝指令ll /lib/modules/
10.重新開機選擇kernel

2016年4月26日 星期二