發表文章

目前顯示的是 6月, 2014的文章

C語言,大數運算,階層筆記

如果今天要用C語言計算50!,int都是不夠用的。 要解這樣的問題,必需要用陣列。  32位元系統unsigned int 最大 4,294,967,295 (10位數字) 64位元 uint64_t 18,446,744,073,709,551,615 (20 位數字) /*從array的高位元開始存值*/ void Big_factorial_iteration(int n) { int i=1; /*init array*/ int a[SIZE_A]= {0}; int tail=SIZE_A-1; a[tail]=1; for(i=1; i <= n ; i++) { int j; /* multiply the value */ for(j= tail ; j >= 0 ; j--) { a[j]=a[j]*i; } /*進位留下個位數,其餘的加點前一位 */ for(j= tail ; j >= 0 ; j--) { if (a[j] >= 10) a[j-1]=a[j-1]+(a[j]/10); a[j]=a[j]%10; } } for(i=0; i< SIZE_A ; i++) { printf("%d",a[i]); } printf("\n"); } int main() { printf(" %d \n",factorial_recursion(10)); printf(" %d \n",factorial_iteration(10)); Big_factorial_iteration(20); 計算20階層 } result: [carlos@localhost c_examples]$ ./factorial_2  3628800  3628800  000000000002432902008176640000  原始碼:   https://github.com/tzuCarlo...

安裝Cowday到你的linux歡迎訊息,message of day linux

圖片
1.先安裝 sudo apt-get install fortune cowsay 2接著加入以下指令到 bash.bashrc中,這樣每次開啟terminal 或 ssh 就會有隻牛跟你說話囉 sudo vim    / etc / bash.bashrc   # Spicing up Terminal fortune | cowsay -n echo source: http://xtremediary.blogspot.tw/2010/04/spice-up-ubuntulinux-terminal-with.html

C語言階層的遞迴與迴圈 - example factorial recursion and iteration.

筆記一下C語言階層的遞迴與迴圈寫法,這個範例不考慮溢位的問題。 #include /* * example for recursion and iteration * I know it should return error if n < 0, but this just a simple example for study. * Do not care about overflow issues this example. */ unsigned int factorial_recursion(int n) { /* return n>=1 ? n * factorial_recursion(n-1) : 1; */ if(n==0) return 1; /*Base case */ else return n * factorial_recursion(n-1); /*General case */ } unsigned int factorial_iteration(int n) { int i=1; int result=1; for(i=1; i <= n ; i++) { /*result *= i */ result=result * i; } return result; } int main() { printf(" %d \n",factorial_recursion(10)); printf(" %d \n",factorial_iteration(10)); } 原始碼: https://github.com/tzuCarlos/linux_C/blob/master/c_examples/factorial.c

Strace cross compile PowerPC

今天遇到一個bug Web daemon隨機會segmentation fault 想用Strace來debug看看 在這抓 - http://sourceforge.net/projects/strace/files/strace/ ./configure --host=powerpc-linux CC=powerpc-linux-gcc LD=powerpc-linux-ld  make之後就可以放上機器上囉。 在機器上執行:Strace -f -o web.trace webs 神奇的事發生了,這時候我的web daemon就不會掛掉了。 只好,慢慢trace code了,最後是發現有人做這樣的行為,寫C時要小心,別亂copy亂指的。 UCHAR old_pass[32]; UCHAR *passphrase; strcpy(passphrase, old_pass);

objdump gcc Assembly debug筆記

gcc -s xxxx 可以篇成組合語言 gcc -g xxxx 之後可以用 objdump -S a.out 看到組語、C code 遇到要debug kernel panic時很好用! :) example: [carlos@localhost testC]$ objdump -S ./a.out ./a.out:     file format elf32-i386 080483d4 : int func1() {  80483d4:       55                      push   %ebp  80483d5:       89 e5                   mov    %esp,%ebp  80483d7:       83 ec 08                sub    $0x8,%esp    printf("WAT WAT WAT \n");  80483da:       c7 04 24 10 85 04 08    movl   $0x8048510,(%esp)  80483e1:       e8 02 ff ff ff          call   80482e8 }  80483e6:       c9                      leav...

備分Beagleboard sd卡

a.首先執行fdisk -l, 可以看到我的sd卡是在/dev/sdb1跟/dev/sdb2,基本上只要備份sdb2的file system就夠了。 karose@karose-VirtualBox:~$ sudo fdisk -l Disk /dev/sda: 21.8 GB, 21802237952 bytes 255 heads, 63 sectors/track, 2650 cylinders, total 42582496 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00079d59    Device Boot      Start         End      Blocks   Id  System /dev/sda1   *        2048    39186431    19592192   83  Linux /dev/sda2        39188478    42582015     1696769    5  Extended /dev/sda5        39188480    42582015     1696768   82  Linux swap / Solaris Disk /dev/sdb: 7860 MB, 7860125696 bytes 242 heads, 62 sectors/track, 1023 cylinders, t...

Beagleboard-XM 更改時區+開啟audio+ Lxde中文

1.更改時區為台灣時區 (To change timezone to Taiwan.) $ sudo tzselect $ sudo cp /usr/share/zoneinfo/Aisa/Taipei /etc/localtime $ sudo hwclock -w 2.開啟音效,預設是沒聲音的 (enable audio, seems it's disabled by default) https://developer.ridgerun.com/wiki/index.php/Getting_Started_Guide_for_DM3730_Beagleboard-xM amixer sset 'DAC1 Digital Fine' 40 amixer sset 'Headset' 2 amixer sset 'HeadsetL Mixer AudioL1' on amixer sset 'HeadsetR Mixer AudioR1' on 3.Install Chinese font packet. 安裝中文字型sudo apt-get install ttf-arphic-uming 4. Install pdf reader sudo apt-get install evince apt-get install poppler-data 5.Wireshark 權限給不是root的user sudo dpkg-reconfigure wireshark-common http://ask.wireshark.org/questions/7976/wireshark-setup-linux-for-nonroot-user 6.Network-Manager權限 sudo vi /etc/polkit-1/localauthority/50-local.d/org.freedesktop.NetworkManager.pkla [nm-applet] Identity=unix-group:netdev Action=org.freedesktop.NetworkManager.* ResultAny=yes ResultIna...

ipcs 指令

Linux常見的IPC方式有下面幾種方式, ipcs指令可以幫助查看ipc資訊 Different Types of IPCS There are various IPC’s which allows a process to communicate with another processes, either in the same computer or different computer in the same network. Pipes  – Provides a way for processes to communicate with each another by exchanging messages. Named pipes provide a way for processes running on different computer systems to communicate over the network. Shared Memory  – Processes can exchange values in the shared memory. One process will create a portion of memory which other process can access. Message Queue  – It is a structured and ordered list of memory segments where processes store or retrieve data. Semaphores  – Provides a synchronizing mechanism for processes that are accessing the same resource. No data is passed with a semaphore; it simply coordinates access to shared resources.  source: http://www.thegeekstuff.com/2010/08/ipcs-command-examples/