2016年8月5日 星期五
2016年8月3日 星期三
linux share memory用法
http://www.ecst.csuchico.edu/~beej/guide/ipc/shmem.html
可以用man 2 shmget查詢相關資料
需要include
主要有四個function:
int shmget(key_t key, size_t size, int shmflg);
若要移除已經建立的share memory,須使用shmctl:
先拿到shmid之後,用shmctl即可移除
shmctl(shmid,IPC_RMID,0);
可以用man 2 shmget查詢相關資料
需要include
sys/shm.h
主要有四個function:
- shmget() - 建立共享記憶體
- shmat() - attach 共享記憶體
- shmdt() - detach 共享記憶體
- shmctl() - 設定共享記憶體
int shmget(key_t key, size_t size, int shmflg);
- key可以這樣用:(key_t)1234
- size:以byte為單位的大小
- shmflag:
IPC_CREATE | 0666
(0666是權限) - 若shmflag內有IPC_EXCL 則表示:
若之前用同一個key建立過share memory,則此次建立會失敗(防止用到已創立過的)
若沒用這flag就會連接到同一個地方 - 回傳值是shmid,錯誤時則會回傳-1
- attach會把個shmid的share memory的位址放到shmaddr
- 若失敗則shmaddr為-1
- detach
若要移除已經建立的share memory,須使用shmctl:
先拿到shmid之後,用shmctl即可移除
shmctl(shmid,IPC_RMID,0);
寫個簡單範例.
#include#include int main(int argc, char* argv[]){ int data[100]; int shmid; void *shared_memory = (void *)0; shmid = shmget ((key_t) 123, sizeof (int) * 100, 0666 | IPC_CREAT); if (shmid == -1) { fprintf (stderr, "shmget failed\n"); exit (1); } shared_memory = shmat (shmid, (void *)0, 0); if (shared_memory == (void *)-1) { fprintf (stderr, "shmat failed\n"); exit (1); } printf ("Memory attached at %X\n", shared_memory); if(argc == 2 && strcmp(argv[1], "-d") == 0){ printf("reset data to 0!\n"); ((int *)shared_memory)[0] = 0; } printf("old data:%d\n", ((int *)shared_memory)[0]++); printf("new data:%d\n", ((int *)shared_memory)[0]); exit(0); }
取得LINUX系統實際記憶體剩餘空間
在linux下,沒用到的記憶體空間會被拿來當作buffer/cache使用,
以加速I/O存取
所以會感覺記憶體占用很多,
其實實際上如果程式需要記憶體,
就會釋放buffer/cache的空間,所以通常記憶體幾乎都是保持在很高的使用量。
使用free指令,
"-/+ buffers/cache"那一行的free就是實際上系統剩下來沒被其他程式吃掉的記憶體大小
可以用以下的指令取得真實的剩餘空間
echo -n free memory:;free -m | grep buffers/cache | awk '{print $4}'
以加速I/O存取
所以會感覺記憶體占用很多,
其實實際上如果程式需要記憶體,
就會釋放buffer/cache的空間,所以通常記憶體幾乎都是保持在很高的使用量。
使用free指令,
"-/+ buffers/cache"那一行的free就是實際上系統剩下來沒被其他程式吃掉的記憶體大小
可以用以下的指令取得真實的剩餘空間
echo -n free memory:;free -m | grep buffers/cache | awk '{print $4}'
debug USB - use usbmon, usbfs
linux 上使用蜂鳴器通知某指令完成
1.先寫一隻能夠使蜂鳴器發出聲音的程式
2.beep:一個shell script使b2在指令完成時執行
3.把b2 and beep放至/bin
4.由於b2發出聲音要有root的權限,所以b2擁有者要設為root,然後對beep作setuid
http://www.google.com/url?q=http%3A%2F%2Flinux.vbird.org%2Flinux_basic%2F0220filemanager.php%23suid&sa=D&sntz=1&usg=AFrqEzfx6HcqK_4BpogqAlU2s7Ts5ttsug
//b2.c #include#include void play(unsigned int* freq, unsigned int* delay); int main(int argc, char* argv[]) { speaker(330, 5); speaker( 0, 5); speaker(330, 5); return 0; } int speaker(unsigned int freq,unsigned int delay) { static int flag=0,bit; if(flag==0) { flag=1; iopl(3); } outb(0xb6,0x43); outb((freq & 0xff),0x42); outb((freq >> 8),0x42); bit=inb(0x61); outb(3 | bit,0x61); usleep(10000*delay); outb(0xfc | bit,0x61); return; }
2.beep:一個shell script使b2在指令完成時執行
$* sudo ./b2
3.把b2 and beep放至/bin
4.由於b2發出聲音要有root的權限,所以b2擁有者要設為root,然後對beep作setuid
http://www.google.com/url?q=http%3A%2F%2Flinux.vbird.org%2Flinux_basic%2F0220filemanager.php%23suid&sa=D&sntz=1&usg=AFrqEzfx6HcqK_4BpogqAlU2s7Ts5ttsug
2015年1月11日 星期日
linux bash function for switch tmux session(若不存在則重新建立)
可以直接切換到Tmux 不同session
加在.bashrc內
tmux_goto_branch ()
{
P="$1"
if [ "$P" == ""]
then
echo "$0: must enter branch name..."
exit
fi
tmux has -t $PROJ
if [[ "$?" == "0" ]];
then
echo "$0: Session exist, switch to it";
tmux attach -t $PROJ
else
echo "$0: Session not exist, create a new one"
tmux new -s $PROJ -n $PROJ
fi
}
用法: tmux_goto_branch BRANCH_TEST
設定linux shell 提示字元與ssh 登入的title
查看ubuntu 的.bashrc, 可以發現下列設定:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($ debian_chroot)}\[\033[01;32m\] \u@\h\[\033[00m\]:\[\033[01; 34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($ debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+( $debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
其中可以看到如果要改顏色,可以使用顏色標示包起來
登入shell時,提示符號預設長這樣 username@server:~$
使用putty等軟體經由SSH 連入時,可以看到登入視窗的title也會跟著變,由此可知這個title是從linux內拿來的,就是上述最後一組PS1變數的設定
所以經由上述分析,可以寫一個function, 放在.bashrc內,之後就可以簡單的手動設定想要的提示符號與title
set_title ()
{
PS1="${debian_chroot:+($ debian_chroot)}\[\033[01;32m\] $*\[\033[00m\]:\[\033[01;34m\] \w\[\033[00m\]\$ "
PS1="\[\e]0;${debian_chroot:+( $debian_chroot)}$*: \w\a\]$PS1"
}
使用方法:
set_title "test title"
2014年7月5日 星期六
android 廣告阻擋原理
android 底層是linux kernel
在linux 上面,系統要做domain name->IP的查找時,會先去檢查/etc/hosts裡面是否有寫靜態的對應,如果沒有才會真正去做DNS。
故要阻擋廣告,只要有一份廣告網址的列表,將其加到/etc/hosts內,並對應至127.0.0.1(localhost) 即可
訂閱:
意見 (Atom)




