Archive

Posts Tagged ‘Linux’

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: , , , ,

Google发布免费DNS解析服务

December 4th, 2009 BianJiang No comments

本文来自: Solidot.
Google宣布 推出免费的DNS解析服务Google Public DNS

DNS协议是Web的重要组成部分,它就如同是互联网的“电话簿”。每次访问一个网站,计算机都须执行一次DNS查询。复杂的网页在载入之前需执行多次DNS查询,其结果是普通网民每天必须执行数百次DNS查询,这会降低用户的浏览体验。Google推出Public DNS便是试图改进浏览体验,让互联网变得更快(在TTL记录过期之前执行prefetching,更新缓存),更安全(支持新的DNS安全扩展DNSSEC)。Google Public DNS托管在Google分布在全世界的数据中心,它使用选播路由算法(Anycast Routing)把用户的DNS解析请求发送到地理位置最近的数据中心。Google Public DNS遵循DNS标准,向用户的计算机提供正确的响应,不会有任何屏蔽、过滤,或重新定向等妨碍用户浏览体验的行为。Google Public DNS的IP地址为:8.8.8.8以及8.8.4.4。

修改TCP/IP的DNS服务器地址:

Microsoft Windows

DNS settings are specified in the TCP/IP Properties window for the selected network connection.

Example: Changing DNS server settings on Microsoft Windows Vista

  1. Go the Control Panel.
  2. Click Network and Internet, then Network and Sharing Center, then Manage network connections.
  3. Select the connection for which you want to configure Google Public DNS. For example:
    • To change the settings for an Ethernet connection, right-click Local Area Connection, and click Properties.
    • To change the settings for a wireless connection, right-click Wireless Network Connection, and click Properties.

    If you are prompted for an administrator password or confirmation, type the password or provide confirmation.

  4. Select the Networking tab. Under This connection uses the following items, clickInternet Protocol Version 4 (TCP/IPv4), and then click Properties.
  5. Click Advanced and select the DNS tab. If there are any DNS server IP addresses listed there, write them down for future reference, and remove them from this window.
  6. Click OK.
  7. Select Use the following DNS server addresses. If there are any IP addresses listed in the Preferred DNS server or Alternate DNS server, write them down for future reference.
  8. Replace those addresses with the IP addresses of the Google DNS servers: 8.8.8.8 and 8.8.4.4.
  9. Restart the connection you selected in step 3.
  10. Test that your setup is working correctly; see Testing your new settings below.
  11. Repeat the procedure for additional network connections you want to change.

Mac OS X

DNS settings are specified in the Network window.

Example: Changing DNS server settings on Mac OS 10.5

  1. From the Apple menu, click System Preferences, then click Network. If you are prompted for an administrator password or confirmation, type the password or provide confirmation.
  2. Select the connection for which you want to configure Google Public DNS. For example:
    • To change the settings for an Ethernet connection, select Built-In Ethernet, and click Advanced.
    • To change the settings for a wireless connection, select Airport, and clickAdvanced.
  3. Select the DNS tab.
  4. Click + to replace any listed addresses with, or add, the Google IP addresses at the top of the list: 8.8.8.8 and 8.8.4.4.
  5. Click Apply and OK.
  6. Test that your setup is working correctly; see Testing your new settings below.
  7. Repeat the procedure for additional network connections you want to change.

Linux

DNS settings are specified in /etc/resolv.conf in most distributions.

Example: Changing DNS server settings on Ubuntu

  1. Edit /etc/resolv.conf:
    sudo vi /etc/resolv.conf
  2. If any nameserver lines appear, write down the IP addresses for future reference.
  3. Replace the nameserver lines with, or add, the following lines:
    nameserver 8.8.8.8
    nameserver 8.8.4.4
  4. Save and exit.
  5. Restart any Internet clients you are using.
  6. Test that your setup is working correctly; see Testing your new settings below.


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

Categories: Others 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: , , , , ,

[Kernel] Debian-ubuntu-kernel-install

November 18th, 2009 BianJiang No comments

第一步 安装必要的工具

首先要安装必要的包。
包有:libncurses5-devmenuconfig需要的)和essential

sudo apt-get install build-essential kernel-package
sudo apt-get install make
sudo apt-get install gcc

另外,查看系统是否有这样的两个命令

mkinitramfs mkisofs

这两个工具在编译内核时用来生成 *.img文件的。如果没有就需安装。

第二步 下载内核

www.kernel.org下载新内核到/usr/src

wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.30.5.tar.bz2

我下载的是linux-2.6.30.5.tar.bz2(原来的内核是2.6.24-24-generic)

第三步 编译前的准备

察看当前内核的版本

border@ubuntu:/usr/src$ uname -a
Linux ubuntu 2.6.24-24-generic #1 SMP Tue Jun 30 20:28:53 UTC 2009 i686 GNU/Linux

建议最好下载比当前已安装版本高的内核

解压linux-2.6.30.5.tar.gzlinux-2.6.30.5

cd /usr/src
sudo tar xjvf linux-2.6.30.5.tar.bz2
cd linux-2.6.30.5/


第四步 开始编译

cd /usr/src/linux-2.6.30.5/ //以下所有的工作都在/usr/src/linux-2.6.30.5/下完成

sudo make menuconfig  //menuconfig的话还需要Ncurses,或者用
sudo make xconfig
sudo make menuconfig  //一般是用menuconfig

配置完以后保存(系统中保存的一份内核配置文件是在/usr/src/linux-2.6.30.5下名为.config,你也可以自己在别的地方另存一份)
也可以cp原来在/boot目录下的config-2.6.xx 到当前目录下,在make menuconfig是使用这个配置文件。

sudo make clean //清除旧数据 ,新解压的内核源码就不需要这一步了
sudo make –j4 可以分四个线程来进行编译工作
sudo make bzImage //编译内核,将保存到/usr/src/linux-2.6.30.5/arch/i386/boot/
sudo make modules //编译模块
sudo make modules_install //安装模块,执行完后会显示DEPMOD 2.6.30.5
sudo mkinitramfs -o /boot/initrd.img-2.6.30.5 2.6.30.5      // 2.6.30.5为modules_install 执行完成后显示的DEPMOD 2.6.30.5, 注意: 2.6.30.5 前面有空格
sudo make install //安装内核

如果你想把编译的结果打包为Deb包,可以参考这里 和 这里 .

sudo make-kpkg clean
sudo make-kpkg –revision eee701 kernel_image
sudo dpkg -i linux-image-2.6.30_eee701_i386.deb

安装完后/boot下将增加以下几个文件(用ls -l *30*查看)

border@ubuntu:/boot$ ls -l *30*
-rw-r–r– 1 root root   96237 2009-08-25 17:41 config-2.6.30.5
-rw-r–r– 1 root root 7896051 2009-08-25 17:38 initrd.img-2.6.30.5
-rw-r–r– 1 root root 1095789 2009-08-25 17:41 System.map-2.6.30.5
-rw-r–r– 1 root root 2324720 2009-08-25 17:41 vmlinuz-2.6.30.5

/boot/grub/menu.lst中添加一个新的启动项,如我的menu.lst增加了如下一段文字

title           Ubuntu kernel 2.6.30.5
root            (hd0,0)
kernel          /boot/vmlinuz-2.6.30.5 vga=794 root=/dev/sda1 ro
initrd          /boot/initrd.img-2.6.30.5
quiet


ps: 上面在kernel一行后面的 “
vga=794 root=/dev/sda1 ro” 是从你之前的启动项取得的。
重新启动即可。

参考:
1.   http://ubuntuforums.org/showthread.php?t=311158
2.   “creating a kernel 2.6.30 deb file” http://www.naumann.cc/?p=107


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

Categories: Kernel Tags: , , ,

The Kernel Newbie Corner 系列文章 By: Robert P. J. Day

August 28th, 2009 BianJiang 2 comments