Archive

Archive for the ‘Tech.Notes’ Category

Google Code 与 Github代码同步

January 26th, 2010 BianJiang No comments
git config --global user.name "Bian Jiang"
git config --global user.email borderj@gmail.com
git svn clone https://golang-china.googlecode.com/svn/trunk -s
git remote add origin git@github.com:border/golang-china.git
git push origin master
git svn rebase // 本地与Goolge Code 代码同步
git push // 上传本地的代码到github
git svn dcommit // 上传本地的代码到Google Code 

在Github上创建ssh授权参考: http://wifihack.net/blog/2008/12/permission-denied-on-github/

–EOF–
http://www.wifihack.net

Categories: Tech.Notes Tags: , , , ,

Pthread创建线程后必须使用join或detach释放线程资源

December 26th, 2009 BianJiang No comments

这两天在看Pthread 资料的时候,无意中看到这样一句话(man pthread_detach):

Either pthread_join(3) or pthread_detach() should be called for each thread
       that an application creates, so that system resources for the thread can be
       released.  (But note that the resources of all threads are freed when the
       process terminates.)

也就是说:每个进程创建以后都应该调用pthread_join 或 pthread_detach 函数,只有这样在线程结束的时候资源(线程的描述信息和stack)才能被释放.

之后又查了pthread_join 但是没有明确说明必须调用pthread_join 或 pthread_detach.

但是再查了 Pthread for win32 pthread_join

When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore, pthread_join must be called  once  for each joinable thread created to avoid memory leaks.


才知道如果在新线程里面没有调用pthread_join 或 pthread_detach会导致内存泄漏, 如果你创建的线程越多,你的内存利用率就会越高, 直到你再无法创建线程,最终只能结束进程。

解决方法有三个:
1.   线程里面调用 pthread_detach(pthread_self()) 这个方法最简单
2在创建线程的设置PTHREAD_CREATE_DETACHED属性
3. 创建线程后用 pthread_join() 一直等待子线程结束。

下面是几个简单的例子
1. 调用  pthread_detach(pthread_self())
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
pthread_detach(pthread_self());
int stack[1024 * 20] = {0,};
//sleep(1);
long tid = 0;
//printf(”Hello World! It’s me, thread #%ld!\n”, tid);
//pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(”In main: creating thread %ld\n”, t);
rc = pthread_create(&pid, NULL, PrintHello, NULL);
if (rc){
printf(”ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}
2在创建线程的设置PTHREAD_CREATE_DETACHED属性

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
int stack[1024 * 20] = {0,};
//pthread_exit(NULL);
//pthread_detach(pthread_self());
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(”In main: creating thread %ld\n”, t);
pthread_attr_t attr;
pthread_t thread;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&pid, &attr, PrintHello, NULL);
pthread_attr_destroy (&attr);
if (rc){
printf(”ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}

3. 创建线程后用 pthread_join() 一直等待子线程结束。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
int stack[1024 * 20] = {0,};
//sleep(1);
long tid = 0;
//pthread_exit(NULL);
//pthread_detach(pthread_self());
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(”In main: creating thread %ld\n”, t);
rc = pthread_create(&pid, NULL, PrintHello, NULL);
if (rc){
printf(”ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
pthread_join(pid, NULL);
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}
2. Linux 多线程应用中如何编写安全的信号处理函数 http://www.ibm.com/developerworks/cn/linux/l-cn-signalsec/index.html?ca=drs-cn-0618
Categories: Kernel, Tech.Notes Tags: , , , ,

Sleeping in a .BAT script

December 14th, 2009 BianJiang No comments

这里有列了两个方法:

1. Ping 实现

@ECHO OFF

rem –#——————————————————————

rem –# Script : sleep.BAT

rem –# Tested : Microsoft Windos XP [Version 5.1.2600]

rem –# Purpose : Sleep for number of seconds

rem –# Every 2 pings to localhost takes about 1 second

rem –#

rem –# Usage : sleep.BAT {# of seconds to sleep}

rem –#——————————————————————

ECHO %TIME%

FOR /l %%a IN (%1,-1,1) do (ECHO 1 >NULL %%as&ping -n 2 -w 1 127.0.0.1>NUL)

ECHO %TIME%

2. VBScript 实现

@echo off

echo Wscript.Sleep WScript.Arguments(0) >%tmp%\delay.vbs

cscript //b //nologo %tmp%\delay.vbs 5000

del %tmp%\delay.vbs > nul

当然你也可以用C自己写个。

参考: http://it.toolbox.com/blogs/database-solutions/sleeping-in-a-bat-script-22111

Categories: Tech.Notes Tags: , , ,

vim 快速查找

December 3rd, 2009 BianJiang No comments

在用vim的时候要经常搜索,但是有的时候会忘记*和 # 这两个快捷键,所以记录如下

在通常模式下:

/ 向下查找

? 向上查找

* 向下查找当前光标下的单词

# 向上查找当前光标下的单词.

f{char} 跳到当前行,当前位置右边的{char}的位置. 如

using namespace std;

用f; 将会跳到;所在的位置.

F{char} 类似f{char},不过,是左边.

t{char} 类似f{char},不过,把光标放在{char}的前一个位置.

T{char} 类似t{char},不过,是左边.

; 重复t T f T 指令

, 朝相反方向重复t T f T 指令


Bian Jiang
Blog:  http://www.wifihack.net/

Categories: Tech.Notes Tags: , , ,

安装tftp,ftp,samba,nfs

November 24th, 2009 BianJiang No comments

tftp,ftp和nfs是在嵌入式linux开发环境中经常要用到的传输工具,samba则是在linux和windows下的文件传输工具。

一. tftp安装

1.安装

sudo apt-get install tftpd tftp openbsd-inetd

2.建立tftp主目录
其中用户名和组是你平时使用的主要用户,请不要用root用户作为你的主要操作用户,因为那样很容易把系统搞乱。

sudo mkdir /tftpboot
sudo chown -R tan:tan /tftpboot

3.配置
修改文件:/etc/inetd.conf, 确认文件中包含有类似下面这一行内容:

#emacs /etc/inetd.conf
tftp dgram udp wait nobody /usr/sbin/tcpd /usr/sbin/in.tftpd /tftpboot

4.启动tftp
可以重启动电脑,或者执行:

sudo /etc/init.d/openbsd-inetd reload

5.测试
测试从Tftp服务器传入/传出文件:

tftp localhost

二. ftp安装

1.安装

sudo apt-get install vsftpd

2.配置 修改文件“etc/vsftpd.conf”

#不允许匿名登录
anonymous_enable=NO

#本地用户可用
local_enable=YES

#可写
write_enable=YES

#不需要显示某目录下文件信息
#dirmessage_enable=YES

#登录消息
ftpd_banner=Hello~~

#最大连接用户数
max_clients=10

#限制每个IP的进程
max_per_ip=5

#最大传输速率(bit/s)
local_max_rate=256000

#隐藏帐号
hide_ids=YES

3.启动ftp

sudo /etc/init.d/vsftpd restart

4.安装ftp client
系统自带的ftp client中文乱码。

sudo apt-get install filezilla

三. samba安装

1.安装

sudo apt-get install samba smbfs

2.配置
修改文件“/etc/samba/smb.conf”
配置例子(建立一个共享名称为“share”的samba资源):

[share]
comment = linux share
#共享目录
path = /opt
guest ok = yes

#写权限用户名
write list = tan

printable = no
directory mask 0775
create mask 0775
wide links = no

3.写权限用户口令
如果定义了写特权用户,则该用户必须有samba口令,samba口令与系统用户口令是不同的。

smbpasswd -a 用户名

4.启动

sudo /etc/init.d/samba restart

5.在xp上的共享连接问题处理
在XP上连接samba资源时,出现以下提示的处理:

指定的网络文件夹目前是以其它用户名和密码进行映射的。
要用其他用户名和密码进行连接,首先请断开所有现有连接到该网络共享的映射。

处理:

  1. 断开连接
    找到该共享连接名称

    net use

    删除该连接(共享连接名可以是“\\”“开头的全名,也可以是映射的盘符)

    net use /delete <共享连接名>

    然后重新连接即可。

四. NFS

NFS(Network File System, 网络文件系统)可以通过网络将分享不同主机(不同的OS)的目录——可以通过NFS挂载远程主机的目录, 访问该目录就像访问本地目录一样!

一般而言, 使用nfs能够方便地使各unix-like系统之间实现共享. 但如果需要在unix-like和windows系统之间共享, 就得使用samba了!

NFS运行在SUN的RPC(Remote Procedure Call, 远程过程调用)基础上, RPC定义了一种与系统无关的方法来实现进程间通信. 由此, NFS server也可以看作是RPC server.

正因为NFS是一个RPC服务程序, 所以在使用它之前, 先要映射好端口——通过portmap设定. 比如: 某个NFS client发起NFS服务请求时, 它需要先得到一个端口(port). 所以它先通过portmap得到port number. (不仅NFS, 所有的RPC服务程序启动之前, 都需要设定好portmap)

与NFS相关的几个文件, 命令:
1. /etc/exports

对NFS卷的访问是由exports来批准, 它枚举了若干有权访问NFS服务器上文件系统的主机名.

2. /sbin/exportfs

维护NFS的资源共享. 可以通过它重新设定 /etc/exports 的共享目录, 卸载NFS Server共享的目录或者重新共享等.

3. /usr/sbin/showmount

用在 NFS Server 端,而 showmount 则主要用在 Client 端. showmount 可以用來查看 NFS 共享的目录资源.

4. /var/lib/nfs/xtab

NFS的记录文档: 通过它可以查看有哪些Client 连接到NFS主机的记录.

下面这几个并不直接负责NFS, 实际上它们负责所有的RPC
5. /etc/default/portmap

实际上, portmap负责映射所有的RPC服务端口, 它的内容非常非常之简单(后面详述)

6. /etc/hosts.deny

设定拒绝portmap服务的主机

7. /etc/hosts.allow

设定允许portmap服务的主机

安装NFS
Debian/Ubuntu上默认是没有安装NFS服务器的,首先要安装NFS服务程序:

sudo apt-get install nfs-kernel-server

安装nfs-kernel-server时,apt会自动安装nfs-common和portmap,服务端和客户程序都一块安装了。

客户端需要安装客户端程序。如果是Debian/Ubuntu系统,客户端包名称是“nfs-common”:

sudo apt-get install nfs-commmon

nfs-common和nfs-kernel-server都依赖于portmap!

配置NFS
配置portmap

方法1: 编辑/etc/default/portmap, 将 -i 127.0.0.1 去掉.

方法2: 执行

sudo dpkg-reconfigure portmap

对Should portmap be bound to the loopback address? 选N.

配置/etc/hosts.deny
(禁止任何host(主机)能和你的NFS服务器进行NFS连接),加入:

### NFS DAEMONS
portmap:ALL
lockd:ALL
mountd:ALL
rquotad:ALL
statd:ALL

配 置/etc/hosts.allow
允许那些你想要的主机和你的NFS服务器建立连接。下列步骤将允许任何IP地址以192.168.2开头的主机(连 接到NFS服务器上),也可以指定特定的IP地址。参看man页 hosts_access(5), hosts_options(5)。加入:

### NFS DAEMONS
portmap: 192.168.2.
lockd: 192.168.2.
rquotad: 192.168.2.
mountd: 192.168.2.
statd: 192.168.2.

/etc/hosts.deny 和 /etc/hosts.allow 设置对portmap的访问. 采用这两个配置文件有点类似”mask”的意思. 现在/etc/hosts.deny中禁止所有用户对portmap的访问. 再在/etc/hosts.allow 中允许某些用户对portmap的访问. 运行

sudo /etc/init.d/portmap restart

重启portmap daemon.

配置/etc/exports
NFS挂载目录及权限由/etc/exports文件定义

比如我要将将我的home目录中的/home/zp/share目录让192.168.2.*的IP共享, 则在该文件末尾添加下列语句:

/home/zp/share 192.168.2.*(rw,sync,no_root_squash)

或者:

/home/zp/share 192.168.2.0/24(rw,sync,no_root_squash)

192.168.2.* 网段内的NFS客户端能够共享NFS服务器/home/zp/share目录内容.且有读,写权限, 并且该用户进入/home/zp/share目录后的身份为root

最好加上sync, 否则 “sudo exportfs -r” 时会给出警告, sync是NFS的默认选项.

运行

showmount -e

查看NFS server的export list.

若更改了/etc/exports, 运行

sudo exportfs -r

更新

运行

sudo /etc/init.d/nfs-kernel-server restart

重启nfs服务

/etc/exports实际上就是nfs服务器的核心配置文件了.

测试NFS
可以尝试一下挂载本地磁盘(假设NFS服务器IP地址为:192.128.2.1,NFS目录为“/home/zp/share”)

sudo mount 192.168.2.1:/home/zp/share /mnt

运行

df

看看结果.

卸载NFS目录

sudo umount /mnt

注意被拷贝文件的读/写权限!
另外, 可以使用一定的参数:
使用加参数的办法:

mount -o nolock,rsize=1024,wsize=1024,timeo=15 192.168.2.130:/tmp/ /tmp/

--EOF--
Categories: Embedded, Tech.Notes Tags: , , , , ,

IGMPv3 Multicasting Linux MRouter with XORP How-to on Ubuntu

July 31st, 2009 BianJiang 4 comments

今天在wikipedia看IGMP的时候,无意中发现了XORP, 简单适用了一下非常好用,并且官方的资料真理的比较全,使用也比较方便,还提供了一个基本的shell xorpsh 来配置管理信息。刚好可以省去买支持IGMP的路由器,支持IGMP的路由器最少也要1.5W…

XORP, or Extensible Open Router Platform, is an open source routing software suite, aimed at being both stable and fully featured enough for production use and also extensible to support networking research.

Request:
OS: ubuntu 9.04 (Linux Kernel 2.6.28-11-generic) with GCC 4.3.3
SW: XORP (multicast routing daemon)
HW: PC with 2 or 3 ethernet card (default gw is itself) !

Step 1: Check Linux Kernel Functions!
CONFIG_IP_MULTICAST=y
CONFIG_IP_ADVANCED_ROUTER=y
CONFIG_IP_MROUTE=y
After this configurations, the linux kernel support IGMP, DVMRP and MOSPF. If want support PIM-SM , do below settings
CONFIG_IP_PIMSM_V2=y
PS: you can use vi view /boot/CONFIG-(kernel version) to confirm !

Step 2: edit /etc/sysctl.conf to enable ip_forward
net.ipv4.conf.default.forwarding=1
PS: reboot or use command “sysctl -p” to enable !

Step 3: Install XORP(To compile XORP requires nearly 1.4GB of free disk space)
Step 3.1: Download xorp-1.6.tar.gz
Step 3.2: untar the xorp-1.6.tar.gz
Step 3.3: ./configure
Step 3.4: make
Step 3.4-1: make check (just to check)(it is not necessary)

Step 4: run  rtrmgr/xorp_rtrmgr  rtrmgr/xorpsh (A sample xorp command shell)
Step 4.1: create user group named “xorp”

sudo addgroup xorp

Step 4.2: cp or edit config.boot in xorp-1.6/rtrmgr/config.boot
config.boot for our environment

protocols {
fib2mrib {
disable: false
}
igmp {
disable: false
interface eth0 {
vif eth0 {
disable: false
version: 3
enable-ip-router-alert-option-check: false
query-interval: 125
query-last-member-interval: 1
query-response-interval: 10
robust-count: 2
}
}
}
}
fea {
unicast-forwarding4 {
disable: false
}
}
interfaces {
restore-original-config-on-shutdown: false
interface eth0 {
disable: false
discard: false
description: “”
default-system-config {
}
}
}

Step 4.3: xorp-1.6/rtrmgr/xorp_rtrmgr -b xorp-1.6/rtrmgr/config.boot

Step 5: Check Mrouter work OK
Step 5.1: Connect eth0 to VLC Player server PC and connect eth1 to VLC Player client PC
Step 5.2: Use VLC Player server PC run multicast streaming
Step 5.3: Use VLC Player client PC to play multicast streaming by multicast URL

Step6: run rtrmgr/xorpsh (A sample xorp command shell)

xorp-1.6$ rtrmgr/xorpsh
Welcome to XORP on wifihack

border@wifihack> show igmp ?
Possible completions:
group                Display information about IGMP group membership
interface            Display information about IGMP interfaces

border@wifihack> show igmp group
Interface    Group           Source          LastReported Timeout V State
eth0         224.0.0.2       0.0.0.0         192.168.1.134     240 3     E
eth0         224.0.0.22      0.0.0.0         192.168.1.134     240 3     E
eth0         224.0.0.251     0.0.0.0         192.168.1.134     232 3     E
eth0         224.0.0.252     0.0.0.0         192.168.1.30     232 3     E
eth0         239.255.255.250 0.0.0.0         192.168.1.104     240 3     E

参考:
1. http://www.linuxdiyf.com/bbs/redirect.php?fid=55&tid=69157&goto=nextnewset
2. http://www.xorp.org/getting_started.html
3. “User Manual” http://www.xorp.org/releases/current/docs/user_manual/user_manual.pdf
4. http://en.wikipedia.org/wiki/XORP
5. http://en.wikipedia.org/wiki/IGMP

Categories: Kernel, OpenSource, Tech.Notes Tags: , , ,