2015年10月19日 星期一

C language, measure program run time


======
#define time_define() clock_t clock_time_begin, clock_time_end
#define time_begin() clock_time_begin = clock()
#define time_end() clock_time_end = clock()
#define time_spend() (double)(clock_time_end - clock_time_begin)/CLOCKS_PER_SEC
======
OR
======
#include <sys/time.h>
#define time_define() struct timeval sys_time_begin, sys_time_end
#define time_begin() gettimeofday( &sys_time_begin, NULL)
#define time_end()   gettimeofday( &sys_time_end  , NULL)
#define time_spend() (double)(sys_time_end.tv_sec - sys_time_begin.tv_sec) + (sys_time_end.tv_usec - sys_time_begin.tv_usec)/1000000.0
======

example
======
int main(...)
{
    time_define();
    
    time_begin();

    <do something you want to measure time>

    time_end();

    printf("cost %f sec\n", time_spend());
}
======

Rename linux command 'reboot' and 'poweroff'

有時候要關 vm 時沒注意到而在 host 上下了 poweroff 或 reboot 的 command,超的超不爽的,很多次了 QQ

所以想到稍微改一下 host 端 commnad 字首大小寫,就樣比較不會下太快導致悲劇。

=====
sudo mv /sbin/reboot /sbin/Reboot
sudo ln -sf /sbin/Reboot /sbin/poweroff # poweroff is symbolic link to reboot
=====

這邊有個小地方要改,這個 script 會 call reboot,但是 reboot 被我們改成 Reboot 了
=====
sudo sed -i 's/reboot/Reboot/g' /etc/init.d/reboot
=====

2015年9月30日 星期三

ssh config

由於實驗室與自己的 server 眾多,除了 ssh keygen 外,每個 server 我都有寫個 shell script 連去 server,裡面就只有2行
======
#!/bin/bash
ssh <ID>@<IP>  <-p port>
======
所以我的 home 裡面有8個左右的 shell script,雖然很方便,但是數量有點多。

之前才知道在 .ssh 資料夾裡可以有一個 config 檔案,用來記錄 server,可以自動補齊喔


$HOME/.ssh/config
======
Host     <server name    , ex: nthuServer1>
HostName <IP address     , ex: 140.114.256.256>
Port     <ssh server port, ex: 5566>
User     <user name      , ex: teacher8>
ForwardX11 yes #相當於 ssh -X 的作用
======
有以上 config 後,只要在 terminal 中打
======
ssh bthuServer1  # 可按 tab 會自動補齊
======
相當於
======
ssh -X teacher8@140.114.256.256 -p 5566
======


以上是我常用到的,如果還需要而外功能的話就去問問 google大神吧!!


自動ssh keygen shell script
https://github.com/CJacky/linux_env_setup/blob/master/ssh-keygen.sh

2015年9月16日 星期三

Dell Inspiron 14 3000 ubuntu 14.04.3 touchpad

之前買的 Dell Inspiron 14 3000,詳細型號是 3458

因為買了個SSD升級,所以要重灌成 win10 + ubuntu 14.04.3 雙系統。

雖然 dell 筆電有提供 legacy bios 和 uefi bios 選擇,但在過程中碰到以下2點問題。

1. 在 legacy bios 中
dell 官方的 touchpad driver 在 win10 中有 bug,完全無法運作,只能用 windows 內建的 driver,只有單點觸控

2. 在 uefi bios 中
ubuntu 的 touchpad 會完全無法用做,雖然有偵測到,但是完全沒有作用


由於 bios 類別只能擇一,本來是想說無魚蝦也好,用 legacy bios好了,起碼 windows 還有 touchpad 可以用,雖然只有單點功能,總比 uefi 下的 ubuntu touchpad 完全沒有作用好。

但是天無絕人之路啊,我終於 google 到 ubuntu touchpad under uefi bios 解法了,偉哉 ubuntu 社群

所以最後我是用 uefi bios去灌,安裝步驟都一樣,先windows 後 ubuntu


所以關鍵的 ubuntu touchpad 修復就是
======
echo "blacklist i2c_hid" | sudo tee -a /etc/modprobe.d/blacklist.conf
======

what the fxck i2c_hid driver


ref: http://askubuntu.com/questions/607425/ubuntu-14-04-does-not-see-a-touchpad-on-my-dell-inspiron-14-3000-laptop

2015年9月11日 星期五

C language debug macro

當寫程式需要debug 時我會利用 printf 之類的 print 出一些資訊
但是要關掉時又不想一行一行去刪掉

這時候 define 就派上用場啦

======
#define PRINT(fmt, arg...) printk(fmt, ##arg)

ex:
#ifdef debug
#define PRINT(fmt, arg...) printk("debug " fmt, ##arg)
#else
#define PRINT(fmt, arg...)
#endif
======

只是 function name 要換成 PRINT

2015年9月7日 星期一

舊版 ubuntu 下載

有的時候有些舊的程式不支援新版 kernel,所以需要舊版的 os

ubuntu 舊版的在以下連結裡都有
http://old-releases.ubuntu.com/releases/

以下列出 ubuntu 14.04 LTS 系列 kernel 版本號,wiki上只有14.04.3 的
前 3 組跟 www.kernel.org 是同步的,減號後面的應該是 ubuntu 內部修改的版號

ubuntu 14.04    kernel 3.13.0-24
ubuntu 14.04.1 kernel 3.13.0-32
ubuntu 14.04.2 kernel 3.16.0-30
ubuntu 14.04.3 kernel 3.19.0-25

2015年8月28日 星期五

windows Internet Connection Sharing(ICS) setup

桌機有2張網卡,一張對外,一張接AP提供wifi
所以要把主要網卡的網路分享給另一張網卡。

還好現在網卡很聰明,不用去買跳線了 XDD

1.
開始 -> 控制台 -> 網路和網際網路 -> 網際網路選項
彈出一個視窗,切換選單到"連線",下面有個"LAN設定"
取消勾選"自動偵測設定"

2.
開始 -> 控制台 -> 網路和網際網路 -> 網路和共用中心
畫面切換,左邊"變更介面卡設定"
彈出視窗,在主網卡圖示右鍵 -> 內容,切換選單到"共用"
勾選"允許其他網路使用者透過這台電腦的網際網路連線來連線"