Archive

Archive for December, 2009

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

December 26th, 2009 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 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: , , ,

Google发布免费DNS解析服务

December 4th, 2009 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 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: , , ,