顯示具有 linux 標籤的文章。 顯示所有文章
顯示具有 linux 標籤的文章。 顯示所有文章

2016年8月3日 星期三

linux share memory用法

http://www.ecst.csuchico.edu/~beej/guide/ipc/shmem.html

可以用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
void *shmat(int shmid, const void *shmaddr, int shmflg);
  • attach會把個shmid的share memory的位址放到shmaddr
  • 若失敗則shmaddr為-1
int shmdt(const void *shmaddr);
  • 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}'

debug USB - use usbmon, usbfs

dump usb data
  1. 編kernel時,在driver/usb/下選擇usb monitor
  2. mount -t debugfs debugfs /sys/kernel/debug
  3. 之後在此目錄下就會看到usbmon目錄
  4. 可以在此dump出urb的資料(ascii) 

  • 可以使用/dev/usbmon[XX]來dump出raw data,之後自己寫程式去parse
  • 詳細文件參考Documentation/usb/usbmon.txt 

查看linux上所有USB裝置的詳細資訊



  1. mount -t usbfs usbfs /proc/bus/usb/
  2. cat /proc/bus/usb/devices
  3. 各攔位介紹
    1. http://www.linux-usb.org/USB-guide/c607.html
  4. 詳細文件: Documentation/usb/proc_usb_info.txt

/proc/bus/usb/devices各欄位資料

  • T開頭(topology)
    • Bus: which bus the device is on
    • Lev: the level of the device
      • level 00 for the root hub
      • level 01 for any device attached to the root hub
      • level 02 for devices attached to hubs at level 01, and so on.
    • Prnt: the parent device for this device 
      • always 00 for the root hub
      • 01 for the devices attached to the root hub
    • Port: the port on the parent device
      • starting at 00 for the first port on each device.
      • Prnt/Port is unique per bus.
    • Cnt: what number device this is, based on the enumeration order within that level of the topology, starting at 01 for the first device.
    • Dev#: what number device this is, irrespective of level, based on the bus enumeration order. This is unique per bus
    • Spd: what speed this device is running at, in Mbps (either 1.5 or 12 with the current version of USB).
    • MxCh: how many devices can be connected to this device
      • 00 for anything except a hub. 
    • Driver: which device driver is being used for this device 
      • an entry of (none) indicates that no driver is being used.
  • D開頭(device descriptor)
    • Ver: which USB specification version the device claims to meet. 
    • Cls: which device class the device is claiming to meet, in both hexadecimal and as a string. 
      • Cls entry of 00(>ifc) indicates that the device class specification compliance is interface dependent, and the interface descriptor should be read for device class information.
    • Sub: which sub-class (within the Cls entry), the device meets.
    • Prot: which protocol within a class or sub-class the device claims to meet.
    • MxPS: how big the packets from Endpoint 0 are.
    • #Cfgs: how many configurations this device has.
  • P開頭(pid/vid)
    • Vendor: the Vendor Identification code for the device
    • ProdID: the Product Identification code for the device. 
    • Rev: the product revision number.
  • S開頭: vendor and product strings that the device returned.
  • C開頭(configuration descriptor)
    • the number of C:lines per device is given by #Cfgs, and the entry followed by an asterisk is the current configuration. 
    • #If: how many interfaces the device has.
    • Cfg#: which configuration is being described. 
    • Atr: hexadecimal indication of the device attributes 
      • 0x80 for bus-powered
      • 0x40 for self-powered
      • 0x20 for remote wake-up capable
    • MxPwr: maximum power draw for this device configuration, in milliamps(mA). 
  • I開頭(interface descriptor)
    • If#: which interface is being described within a given device configuration.
    • Alt: which alternate setting of this interface is being described.
    • #EPs: how many endpoints there are within the alternate setting for this endpoint.
    • Cls: which class the alternate setting of the interface corresponds to, in both hexadecimal and as a character string. 
    • Sub: which sub-class the alternate setting of the interface belongs to. 
    • Prot: which interface protocol (within a class and sub-class tuple) the alternate setting of the interface conforms to. 
    • Driver: which of the various USB drivers has claimed this interface. 
  • E開頭(endpoint descriptor)
    • Endpoint 0 is not displayed. 
    • Ad: endpoint address, with a letter to indicate whether the endpoint is an In or Out endpoint.
    • Atr: the attribute (transfer type) associated with the endpoint, followed by a string translating the transfer type. 
    • MxPS: maximum packet size this endpoint is capable of sending or receiving, as appropriate.
    • Ivl: the interval, in milliseconds, between polling of interrupt endpoints.
      • ignored for bulk and control transfers, and is set to 1 for isochronous transfers.






================================================= 

example

root@ocgod-ubuntu9:/proc/bus/usb# cat devices

T: Bus=05 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 0/900 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.3
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=04 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 0/900 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.2
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=03 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 0/900 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.1
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 34/900 us ( 4%), #Int= 3, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.0
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=13ba ProdID=0017 Rev= 0.01
S: Product=Generic USB K/B
C:* #Ifs= 2 Cfg#= 1 Atr=a0 MxPwr=100mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=01 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=24ms
I:* If#= 1 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=usbhid
E: Ad=82(I) Atr=03(Int.) MxPS= 5 Ivl=10ms

T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 3 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=04d9 ProdID=0499 Rev= 2.90
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=10ms

T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8
B: Alloc= 0/800 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:10.4
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 4 Ivl=256ms

linux 上使用蜂鳴器通知某指令完成

1.先寫一隻能夠使蜂鳴器發出聲音的程式
  //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) 即可