colorful

zc qq:1337220912

 

linux 僵尸进程

http://blog.csdn.net/zhuying_linux/article/details/7336662
http://blog.csdn.net/fengwei321123/article/details/9301409

如何查看并杀死僵尸进程?

最近工作过程中,发现好几台服务器出现僵死进程(如图)。

点击查看原图 

  用下面的命令找出僵死进程

  ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]'

  命令注解:
  -A 参数列出所有进程
  -o 自定义输出字段 我们设定显示字段为 stat(状态), ppid(进程父id), pid(进程id),cmd(命令)这四个参数

  -C 用来指定所执行的命令名称,你这里也就是让ps仅仅显示php命令所产生的进程的信息
    ps -C java -o lstart,pid,cmd【不过貌似打印的不全~】
       ps -A -o lstart,pid,args |grep java【这个可以的~】 
  因为状态为 z或者Z 的进程为僵尸进程,所以我们使用grep抓取stat状态为zZ进程
  运行结果参考如下

点击查看原图

  这里一共出现了9个僵死进程,我们需要把它们都干掉,执行下面的命令

  kill -9 8310

  这时你再执行查找僵死的进程,发现所有僵死进程都没了.

  补充:

  最近又遇到了个问题,一台服务器上产生了100多少僵死进程,而且每一僵死进程的父进程都不一样,如果用上面的方法,一条一条的杀,那还不得累死我啊。
  那么就应该想一条简单的命令,直接查找僵死进程,然后将父进程杀死~

   ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' | awk '{print $2}' | xargs kill -9


----------------------------------------------------------------------------------------------------------

不能随便杀父进程.
ps -aux 先找到僵死进程ID,如5031
lsof -p 5031看看僵死在什么地方,一般地讲死锁在某个文件或关联在某个进程
去除死锁文件或杀死相关联进程先
-----------------------------------------------------------------------------------------------------------

 

用ps和grep命令寻找僵尸进程
ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]'
命令注解:
-A 参数列出所有进程
-o 自定义输出字段 我们设定显示字段为 stat(状态), ppid(进程父id), pid(进程id),cmd(命令)这四个参数
因为状态为 z或者Z的进程为僵尸进程,所以我们使用grep抓取stat状态为zZ进程
运行结果参考如下
Z 12334 12339 /path/cmd
这时,我们可以使用 kill -HUP 12339来杀掉这个僵尸进程
运行后,可以再次运行ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]'来确认是否已经将僵尸进程杀死
如果kill 子进程的无效,可以尝试kill 其父进程来解决问题,例如上面例子父进程pid是 12334,那么我们就运行
kill -HUP 12334
来解决问题

 


posted @ 2013-08-12 10:31 多彩人生 阅读(548) | 评论 (0)编辑 收藏

linux signal量

转自:http://www.dbafree.net/?p=870
http://news.cnblogs.com/n/146220/

我们可以使用kill -l查看所有的信号量解释,但是没有看到SIGNAL 0的解释。

[root@testdb~]# kill -l  1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL  5) SIGTRAP      6) SIGABRT      7) SIGBUS       

8)

 SIGFPE  9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2 13) SIGPIPE     14) SIGALRM     15) SIGTERM     17) SIGCHLD 18) SIGCONT     19) SIGSTOP     20) SIGTSTP     21) SIGTTIN 22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ 26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO 30) SIGPWR      31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1 36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4  39) SIGRTMIN+5 40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8  43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6  59) SIGRTMAX-5 60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2  63) SIGRTMAX-1 64) SIGRTMAX

网上搜了下,在这篇文档中找到了signal 0的解释,很不错:

http://www.linuxjournal.com/content/monitoring-processes-kill-0

“signal 0″ is kind of like a moral equivalent of “ping”.

Using “kill -0 NNN” in a shell script is a good way to tell if PID “NNN” is alive or not:

signal 0 is just used to check process is exists or not.

在关其它的信号量说明,原文:http://hi.baidu.com/syqust/blog/item/cd8f6c633b8a617c0d33fa35.html

信号本质
信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的。信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知道信号到底什么时候到达。

信号是进程间通信机制中唯一的异步通信机制,可以看作是异步通知,通知接收信号的进程有哪些事情发生了。信号机制经过POSIX实时扩展后,功能更加强大,除了基本通知功能外,还可以传递附加信息。

使用kill -l就会显示出linux支持的信号列表。
其中列表中,编号为1 ~ 31的信号为传统UNIX支持的信号,是不可靠信号(非实时的),编号为32 ~ 63的信号是后来扩充的,称做可靠信号(实时信号)。不可靠信号和可靠信号的区别在于前者不支持排队,可能会造成信号丢失,而后者不会。

下面我们对编号小于SIGRTMIN的信号进行讨论(下面的编号 依次对应信号 的数值为1 – 31)。

1) SIGHUP
本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联。

登录Linux时,系统会分配给登录用户一个终端(Session)。在这个终端运行的所有程序,包括前台进程组和后台进程组,一般都属于这个 Session。当用户退出Linux登录时,前台进程组和后台有对终端输出的进程将会收到SIGHUP信号。这个信号的默认操作为终止进程,因此前台进 程组和后台有终端输出的进程就会中止。不过可以捕获这个信号,比如wget能捕获SIGHUP信号,并忽略它,这样就算退出了Linux登录,wget也 能继续下载。

此外,对于与终端脱离关系的守护进程,这个信号用于通知它重新读取配置文件。

2) SIGINT
程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程。

3) SIGQUIT
和SIGINT类似, 但由QUIT字符(通常是Ctrl-\)来控制. 进程在因收到SIGQUIT退出时会产生core文件, 在这个意义上类似于一个程序错误信号。

4) SIGILL
执行了非法指令. 通常是因为可执行文件本身出现错误, 或者试图执行数据段. 堆栈溢出时也有可能产生这个信号。

5) SIGTRAP
由断点指令或其它trap指令产生. 由debugger使用。

6) SIGABRT
调用abort函数生成的信号。

7) SIGBUS
非法地址, 包括内存地址对齐(alignment)出错。比如访问一个四个字长的整数, 但其地址不是4的倍数。它与SIGSEGV的区别在于后者是由于对合法存储地址的非法访问触发的(如访问不属于自己存储空间或只读存储空间)。

8) SIGFPE
在发生致命的算术运算错误时发出. 不仅包括浮点运算错误, 还包括溢出及除数为0等其它所有的算术的错误。

9) SIGKILL
用来立即结束程序的运行. 本信号不能被阻塞、处理和忽略。如果管理员发现某个进程终止不了,可尝试发送这个信号。

10) SIGUSR1
留给用户使用

11) SIGSEGV
试图访问未分配给自己的内存, 或试图往没有写权限的内存地址写数据.

 信号 11,即表示程序中可能存在特定条件下的非法内存访问。

12) SIGUSR2
留给用户使用

13) SIGPIPE
管道破裂。这个信号通常在进程间通信产生,比如采用FIFO(管道)通信的两个进程,读管道没打开或者意外终止就往管道写,写进程会收到SIGPIPE信号。此外用Socket通信的两个进程,写进程在写Socket的时候,读进程已经终止。

14) SIGALRM
时钟定时信号, 计算的是实际的时间或时钟时间. alarm函数使用该信号.

15) SIGTERM
程序结束(terminate)信号, 与SIGKILL不同的是该信号可以被阻塞和处理。通常用来要求程序自己正常退出,shell命令kill缺省产生这个信号。如果进程终止不了,我们才会尝试SIGKILL。

17) SIGCHLD
子进程结束时, 父进程会收到这个信号。

如果父进程没有处理这个信号,也没有等待(wait)子进程,子进程虽然终止,但是还会在内核进程表中占有表项,这时的子进程称为僵尸进程。这种情 况我们应该避免(父进程或者忽略SIGCHILD信号,或者捕捉它,或者wait它派生的子进程,或者父进程先终止,这时子进程的终止自动由init进程 来接管)。

18) SIGCONT
让一个停止(stopped)的进程继续执行. 本信号不能被阻塞. 可以用一个handler来让程序在由stopped状态变为继续执行时完成特定的工作. 例如, 重新显示提示符

19) SIGSTOP
停止(stopped)进程的执行. 注意它和terminate以及interrupt的区别:该进程还未结束, 只是暂停执行. 本信号不能被阻塞, 处理或忽略.

20) SIGTSTP
停止进程的运行, 但该信号可以被处理和忽略. 用户键入SUSP字符时(通常是Ctrl-Z)发出这个信号

21) SIGTTIN
当后台作业要从用户终端读数据时, 该作业中的所有进程会收到SIGTTIN信号. 缺省时这些进程会停止执行.

22) SIGTTOU
类似于SIGTTIN, 但在写终端(或修改终端模式)时收到.

23) SIGURG
有”紧急”数据或out-of-band数据到达socket时产生.

24) SIGXCPU
超过CPU时间资源限制. 这个限制可以由getrlimit/setrlimit来读取/改变。

25) SIGXFSZ
当进程企图扩大文件以至于超过文件大小资源限制。

26) SIGVTALRM
虚拟时钟信号. 类似于SIGALRM, 但是计算的是该进程占用的CPU时间.

27) SIGPROF
类似于SIGALRM/SIGVTALRM, 但包括该进程用的CPU时间以及系统调用的时间.

28) SIGWINCH
窗口大小改变时发出.

29) SIGIO
文件描述符准备就绪, 可以开始进行输入/输出操作.

30) SIGPWR
Power failure

31) SIGSYS
非法的系统调用。

在以上列出的信号中,程序不可捕获、阻塞或忽略的信号有:SIGKILL,SIGSTOP
不能恢复至默认动作的信号有:SIGILL,SIGTRAP
默认会导致进程流产的信号有:SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGIOT,SIGQUIT,SIGSEGV,SIGTRAP,SIGXCPU,SIGXFSZ
默认会导致进程退出的信号有:SIGALRM,SIGHUP,SIGINT,SIGKILL,SIGPIPE,SIGPOLL,SIGPROF,SIGSYS,SIGTERM,SIGUSR1,SIGUSR2,SIGVTALRM
默认会导致进程停止的信号有:SIGSTOP,SIGTSTP,SIGTTIN,SIGTTOU
默认进程忽略的信号有:SIGCHLD,SIGPWR,SIGURG,SIGWINCH

此外,SIGIO在SVR4是退出,在4.3BSD中是忽略;SIGCONT在进程挂起时是继续,否则是忽略,不能被阻塞

终止程序的时候在不得已的情况下不能用SIGKILL,因为SIGKILL不会对子进程进行处理,只是把对自己进行处理

 

 

Linux支持POSIX标准信号和实时信号。下面给出Linux Signal的简表,详细细节可以查看man 7 signal。

 

默认动作的含义如下:

Term    终止进程

 

信号 取值 默认动作 含义(发出信号的原因)
SIGHUP 1 Term 终端的挂断或进程死亡
SIGINT 2 Term 来自键盘的中断信号
SIGQUIT 3 Core 来自键盘的离开信号
SIGILL 4 Core 非法指令
SIGABRT 6 Core 来自abort的异常信号
SIGFPE 8 Core 浮点例外
SIGKILL 9 Term 杀死
SIGSEGV 11 Core 段非法错误(内存引用无效)
SIGPIPE 13 Term 管道损坏:向一个没有读进程的管道写数据
SIGALRM 14 Term 来自alarm的计时器到时信号
SIGTERM 15 Term 终止
SIGUSR1 30,10,16 Term 用户自定义信号1
SIGUSR2 31,12,17 Term 用户自定义信号2
SIGCHLD 20,17,18 Ign 子进程停止或终止
SIGCONT 19,18,25 Cont 如果停止,继续执行
SIGSTOP 17,19,23 Stop 非来自终端的停止信号
SIGTSTP 18,20,24 Stop 来自终端的停止信号
SIGTTIN 21,21,26 Stop 后台进程读终端
SIGTTOU 22,22,27 Stop 后台进程写终端
SIGBUS 10,7,10 Core 总线错误(内存访问错误)
SIGPOLL Term Pollable事件发生(Sys V),与SIGIO同义
SIGPROF 27,27,29 Term 统计分布图用计时器到时
SIGSYS 12,-,12 Core 非法系统调用(SVr4)
SIGTRAP 5 Core 跟踪/断点自陷
SIGURG 16,23,21 Ign socket紧急信号(4.2BSD)
SIGVTALRM 26,26,28 Term 虚拟计时器到时(4.2BSD)
SIGXCPU 24,24,30 Core 超过CPU时限(4.2BSD)
SIGXFSZ 25,25,31 Core 超过文件长度限制(4.2BSD)
SIGIOT 6 Core IOT自陷,与SIGABRT同义
SIGEMT 7,-,7 Term
SIGSTKFLT -,16,- Term 协处理器堆栈错误(不使用)
SIGIO 23,29,22 Term 描述符上可以进行I/O操作
SIGCLD -,-,18 Ign 与SIGCHLD同义
SIGPWR 29,30,19 Term 电力故障(System V)
SIGINFO 29,-,- 与SIGPWR同义
SIGLOST -,-,- Term 文件锁丢失
SIGWINCH 28,28,20 Ign 窗口大小改变(4.3BSD, Sun)
SIGUNUSED -,31,- Term 未使用信号(will be SIGSYS)

 

说明:

一些信号的取值是硬件结构相关的(一般alpha和sparc架构用第一个值,i386、ppc和sh架构用中间值,mips架构用第三个值, - 表示相应架构的取值未知)。

 

蓝色的是POSIX.1-1990标准信号。

SIGKILL和SIGSTOP信号不能被挂钩、阻塞或忽略。

 

青色的是SUSv2和POSIX.1-2001定义的信号。

在Linux 2.2(包括)内核之前,SIGSYS、SIGXCPU、SIGXFSZ和SIGBUS (SPARC和MIPS架构除外)的默认动作是终止进程,但没有 core dump。Linux 2.4遵循POSIX.1-2001要求,这些信号的默认动作改为:终止进程同时做core dump。

 

橙色的是其他常见的信号。

信号29在Alpha上为SIGINFO / SIGPWR ,在Sparc上为SIGLOST。

SIGEMT没有在POSIX.1-2001中说明,但是在大多数的Unices中仍然能见到,典型的默认动作是终止进程并做core dump。

SIGPWR没有在POSIX.1-2001中说明,在使用它的一些Unices中典型的默认动作是忽略。

SIGIO没有在POSIX.1-2001中说明,在使用它的一些Unices中典型的默认动作是忽略。

 

进程可以通过使用sigaction和signal系统调用来改变信号的默认处理方式(使用signal的可移植性差)。进程可以选择下列3种信号处理方式中的一种:

1、执行默认操作;

2、忽略该信号;

3、捕获该信号,但是通过信号句柄来调用自定义的处理函数。

 

信号可能被阻塞。进程中的每个线程拥有独立的信号掩码,用来表示本线程的信号被阻塞。线程通过pthread_sigmask来设置它的信号掩码。单线程程序可以用sigprocmask来操作信号掩码。在多线程程序中,所有线程处理一个指定信号的默认行为都是一样的。

 

 

补充:

SIG_DFL,SIG_IGN 分别表示无返回值的函数指针,指针值分别是0和1,这两个指针值逻辑上讲是实际程序中不可能出现的函数地址值。

SIG_DFL:默认信号处理程序
SIG_IGN:忽略信号的处理程序

posted @ 2013-08-12 09:09 多彩人生 阅读(430) | 评论 (0)编辑 收藏

wxWidgets中wxString各类型转换

http://www.cnzui.com/archives/290#std::string_to_wxString

目录

   附加(数据库ADO类型):

文本

A literal is a string written in code with "quotes around it". A literal is not a wxString, and (in wxWidgets 2.8) will not be implicitly converted to one. This means that you can never pass in a raw literal into a wxWidget function or method (unless you don't care about your app not building with Unicode-enabled wxWidgets builds)
文本是一个在代码中被引号包围的串,文本它不是一个单纯的wxString类型,并且(在wxWidgets 2.8中)不能被隐含的转换为一个wxString类型。这意味着你不能试图将光秃秃的将一段文本放到wxWidget函数或方法中通过编译(除非你不在 意你的应用程序是需要在Unicode编码环境中通过编译的)。

MessageBox("I'm a mistake!")  // WRONG in WxWidgets 2.8 (OK in 2.9) 

Instead, wxWidgets (prior to wxWidgets 2.9) requires you to use one of these macros to turn literals into wxString-compatible characters:

_("text that can be translated") wxT("text that won't be translated") _T("same as wxT")   char* c = "sometext"; wxT(c) // WRONG, not a literal 

Rather than being a nuisance, the _(), wxT(), and _T() macros take care of some unicode issues and help with internationalization.

char* to wxString

const char* chars = "Hello world"; // assuming your string is encoded as UTF-8, change the wxConv* parameter as needed wxString mystring(chars, wxConvUTF8);

wxString to char*

void my_function(const char* foo) { } ... wxString mystring(wxT("HelloWorld")); // you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8) my_function( mystring.mb_str() );

mb_str() 返回一个临时的指针,如果你需要通过函数得到更多的返回结果(就和上面的情况一样),你可以临时保存一下这个字符数据流:

wxString s( wxT("some string") ); wxCharBuffer buffer=s.ToUTF8(); foo( buffer.data() );  // data() returns const char * bar( buffer.data(), strlen(buffer.data()) );  // in case you need the length of the data 

当你真的需要将它复制为char*类型时:

wxString mystring(wxT("HelloWorld")); char cstring[1024]; // assuming you want UTF-8, change the wxConv* parameter as needed strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023);

你也可以用ToUTF8(), 因为你得到的编码比用mb_str()函数从const char*转换成char*更加清楚。

wxString mystring(wxT("HelloWorld")); (const_cast<char*>((const char*)mystring.mb_str()))

在可变参数的函数 (如printf)中用mb_str()函数将无效,但按以下的方法是有效的:

wxString mystring(wxT("HelloWorld")); printf("%s",mystring.mb_str().data());

做为选择,使用Potential Unicode Pitfalls中推荐的方法:

printf("%s", (const char*)mystring.mb_str())

wchar_t* to wxString

const wchar_t* chars = L"Hello world"; wxString mystring(chars);

wxString to wchar_t*

请翻阅官方文档的以下方法:

wxString::wc_str() wxString::wchar_str()

 

wxString to TCHAR

TCHAR tCharString[255]; wxString myString(_T("Hello World")); const wxChar* myStringChars = myString.c_str(); for (int i = 0; i < myString.Len(); i++) {    tCharString[i] = myStringChars [i]; } tCharString[myString.Len()] = _T('\0');

int to wxString

wxString mystring = wxString::Format(wxT("%i"),myint);

或者

wxString mystring; mystring << myint;

float to wxString

wxString mystring = wxString::Format(wxT("%f"), myfloat);

或者

wxString mystring; mystring << myfloat;

wxString to integer number

wxString number(wxT("145")); long value; if(!number.ToLong(&value)) { /* error! */ }

或者

wxString str = _T("123"); int num;   num = wxAtoi(str);

wxString to floating-point number

wxString number(wxT("3.14159")); double value; if(!number.ToDouble(&value)){ /* error! */ }

std::string to wxString

std::string stlstring = "Hello world"; // assuming your string is encoded as UTF-8, change the wxConv* parameter as needed wxString mystring(stlstring.c_str(), wxConvUTF8);

从wxWidgets 2.9开始, 你可以用适当的构造函数:

std::string stlstring = "Hello world"; // assuming your string is encoded as the current locale encoding (wxConvLibc) wxString mystring(stlstring);

wxString to std::string

在wxWidgets 2.8 :

wxString mystring(wxT("HelloWorld")); std::string stlstring = std::string(mystring.mb_str());

在wxWidgets 2.9, 你可以用这个方法

wxString::ToStdString()

std::wstring to wxString

从wxWidgets 2.9开始, 你可以用适当的构造函数:

std::sstring stlstring = L"Hello world"; // assuming your string is encoded as the current locale encoding (wxConvLibc) wxString mystring(stlstring);

wxString to std::wstring

在wxWidgets 2.9, 你可以用这个方法

wxString::ToStdWstring()


附:(数据库类型)

" src="/CuteSoft_Client/CuteEditor/Images/anchor.gif">wxString 转 _bstr_t
 

wxString str(wxT("Hello")); _bstr_t mystring=_bstr_t(str.wc_str()); 

" src="/CuteSoft_Client/CuteEditor/Images/anchor.gif">_bstr_ 转 wxString
_bstr_t bstr="hello"; wxString mystring = wxString(static_cast<const wchar_t *>(bstr)); 

" src="/CuteSoft_Client/CuteEditor/Images/anchor.gif">_variant_t 转 wxString

_variant_t  varstr=_variant_t("Hello"); wxString mystring=wxString(static_cast<const wchar_t *>(_bstr_t(varstr))); 

" src="/CuteSoft_Client/CuteEditor/Images/anchor.gif">wxString 转 _variant_t

wxString str(wxT("Hello")); _variant_t myvar=_variant_t(str.wc_str()); 

 

本文翻译自wxWidgets官方:http://wiki.wxwidgets.org/Converting_everything_to_and_from_wxString

posted @ 2013-08-10 09:29 多彩人生 阅读(4271) | 评论 (1)编辑 收藏

make makefile 学习

http://www.cnblogs.com/napoleon_liu/archive/2011/05/06/2039316.html
http://www.iteye.com/topic/774919
http://blog.csdn.net/liangkaiming/article/details/6267357
http://blog.jobbole.com/44891/


gcc是C编译器;g++是C++编译器;linux下cc一般是一个符号连接,指向gcc;gcc和g++都是GUN(组织)的编译器。而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Linux/Unix都是大小写敏感的系统,这点一定要注意。

cc是Unix系统的C Compiler,而gcc则是GNU Compiler Collection,GNU编译器套装。gcc原名为Gun C语言编译器,因为它原本只能处理C语言,但gcc很快地扩展,包含很多编译器(C、C++、Objective-C、Ada、Fortran、 Java)。因此,它们是不一样的,一个是古老的C编译器,一个是GNU编译器集合,gcc里面的C编译器比cc强大多了,因此没必要用cc。

下载不到cc的原因在于:cc来自于昂贵的Unix系统,cc是商业软件。

Linux下的cc是gcc符号连接,可以通过$ls –l /usr/bin/cc来简单察看,该变量是make程序的内建变量,默认指向gcc。cc符号链接和变量存在的意义在于源码的移植性,可以方便的用 gcc来编译老的用cc编译的Unix软件,甚至连makefile都不用改在,而且也便于Linux程序在Unix下编译。

 

误区一:gcc只能编译C代码,g++只能编译C++代码。

两者都可以,但请注意:

(1)后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是C++程序,注意,虽然C++是C的超集,但是两者对语法的要求是有区别的。C++的语法规则更加严谨一些。

(2)编译阶段,g++会调用gcc,对于C++代码,两者是等价的,但是因为gcc命令不能自动和C++程序使用的库联接,所以通常用g++来完成链接,为了统一起见,干脆编译/链接统统用g++了(编译阶段g++会反过来再调用gcc),这就给人一种错觉,好像cpp程序只能用g++似的。

 

误区二:gcc不会定义__cplusplus宏,而g++会

实际上,这个宏只是标志着编译器将会把代码按C还是C++语法来解释,如上所述,如果后缀为.c,并且采用gcc编译器,则该宏就是未定义的,否则,就是已定义。

 

误区三:编译只能用gcc,链接只能用g++

严格来说,这句话不算错误,但是它混淆了概念,应该这样说:编译可以用gcc/g++,而链接可以用g++或者gcc -lstdc++。因为gcc命令不能自动和C++程序使用的库联接,所以通常使用g++来完成联接。但在编译阶段,g++会自动调用gcc,二者等价。

 

C++的编译器肯定可以编译C的代码,注意除了C++对C的语法扩充之外,编译和链接C和C++的标准库通常也不一样呢,用gcc而非g++也编译了C++的程序就证明了这一点。

 

注:符号链接是一种特殊类型的文件,它的内容只是一个字符串。它可能指向一个存在的文件也可能什么都不指向。当您在命令行或程序里提到符号链接的时候,您实际上进入了它指向的文件,前提是这个文件是存在的。

posted @ 2013-08-07 20:04 多彩人生 阅读(334) | 评论 (0)编辑 收藏

jsoncpp简单示例

http://www.cnblogs.com/logicbaby/archive/2011/07/03/2096794.html

1、 编译jsoncpp
  从(http://jsoncpp.sourceforge.net/)下载源码包“jsoncpp-src-0.5.0.tar.gz”,解压后在其解压后目录中运行
$ scons platform=linux-gcc
   编译出来的库文件在其libs/ linux-gcc-4.4.2目录下,有libjson_linux-gcc-4.4.2_libmt.so和libjson_linux-gcc- 4.4.2_libmt.a。头文件在解压目录下的include中。我的jsoncpp安装在$HOME/usr/jsoncpp下。

$ mkdir ~/usr/jsoncpp

$ cp -r include ~/usr/jsoncpp

$ cp -r libs ~/usr/jsoncpp

2、 Jsoncpp简单实例

1) 反序列化Json对象
  比如一个Json对象的字符串序列如下,其中”array”:[...]表示Json对象中的数组:

{“key1”:”value1”,”array”:[{“key2”:”value2”},{“key2”:”value3”},{“key2”:”value4”}]}

  那怎么分别取到key1和key2的值呢,代码如下所示:

#include <iostream>
#include <string>
#include "json/json.h"
 
int main(void)
{
     std::string strValue = "{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}";
 
     Json::Reader reader;
     Json::Value value;
 
     if (reader.parse(strValue, value))
     {
      std::string out = value["key1"].asString();
      std::cout << out << std::endl;
      const Json::Value arrayObj = value["array"];
      for (int i=0; i<arrayObj.size(); i++)
      {
           out = arrayObj[i]["key2"].asString();
           std::cout << out;
           if (i != arrayObj.size() - 1)
            std::cout << std::endl;
      }
     }
     return 0;
}
编译连接
$ g++ jscpp1.cpp -I$HOME/usr/jsoncpp/include/ ~/usr/jsoncpp/libs/linux-gcc-4.5.2/libjson_linux-gcc-4.5.2_libmt.a
$ ./a.out
value1
value2
value3
value4

2)序列化Json对象
  先构建一个Json对象,此Json对象中含有数组,然后把Json对象序列化成字符串,代码如下:
#include <iostream>
#include <string>
#include "json/json.h"
 
int main(void)
{
     Json::Value root;
     Json::Value arrayObj;
     Json::Value item;
 
     for (int i = 0; i < 10; i ++)
     {
      item["key"] = i;
      arrayObj.append(item);
     }
 
     root["key1"] = "value1";
     root["key2"] = "value2";
     root["array"] = arrayObj;
     //root.toStyledString();
     std::string out = root.toStyledString();
     std::cout << out << std::endl;
     return 0;
}

编译连接
$ g++ jscpp2.cpp -I$HOME/usr/jsoncpp/include/ ~/usr/jsoncpp/libs/linux-gcc-4.5.2/libjson_linux-gcc-4.5.2_libmt.a
$ ./a.out
{
   "array" : [
      {
         "key" : 0
      },
      {
         "key" : 1
      },
      {
         "key" : 2
      },
      {
         "key" : 3
      },
      {
         "key" : 4
      },
      {
         "key" : 5
      },
      {
         "key" : 6
      },
      {
         "key" : 7
      },
      {
         "key" : 8
      },
      {
         "key" : 9
      }
   ],
   "key1" : "value1",
   "key2" : "value2"
}


  注:以上两个代码来自网上,这两个实例很好,故在此借用。

posted @ 2013-08-05 16:07 多彩人生 阅读(1740) | 评论 (0)编辑 收藏

curl 多线程

http://fool.is-programmer.com/2011/3/26/libcurl-signal-bug.25603.html
http://blog.csdn.net/balderfan/article/details/7599554
http://blog.chinaunix.net/uid-20692625-id-3203258.html

posted @ 2013-08-04 19:15 多彩人生 阅读(484) | 评论 (0)编辑 收藏

让CentOS默认生成core dump

CentOS中默认设置了ulimit -c 0也就是默认程序不生成core dump,这样很麻烦,每次启动程序前都要让用户先执行一次ulimit -c ulimited,有时候用户忘记则不生成core,很不利于错误分析,所以研究了下如何让系统默认就生成core.

在网上找了部分资料,看到了

CentOS 5中ulimit值调整并永久生效的方法

http://www.sunchis.com/html/os/linux/2011/0609/338.html

这篇文章,其中说通过编辑/etc/security/limits.conf文件可以实现ulimit的值设定,测试了nofile后的确可行,但是写入了

* soft core unlimited
* hard core unlimited


重新登陆却没有效果  // ps by zc, 我到这里就有效果了

[root@localhost ~]# ulimit -a
core file size          (blocks, -c) 0

通过研究,发现/etc/profile文件中有一句

# No core files by default
ulimit -S -c 0 > /dev/null 2>&1

二话不说,把第二行用#注释掉后,即

# No core files by default
#ulimit -S -c 0 > /dev/null 2>&1

保存重新登陆用户,再次查看,一切正常!

[root@localhost ~]# ulimit -a
core file size          (blocks, -c) ulimited


-----------------------------------------------------------------

1:设置系统允许生产core文件
在 ~/.bash_profile 中增加
ulimit -c unlimited
使用ulimit -a可以查看系统core文件的大小限制;
使用ulimit -c [kbytes]可以设置系统允许生成的core文件大小;
ulimit -c 0 不产生core文件
ulimit -c 100 设置core文件最大为100k
ulimit -c unlimited 不限制core文件大小

posted @ 2013-07-31 10:34 多彩人生 阅读(7542) | 评论 (0)编辑 收藏

apache 开启多站点


http://zhangyong333revice.blog.163.com/blog/static/1131518832011101811237149/
http://hi.baidu.com/dspace/item/68b67705a9c53bd21ff046a1
http://zhb1208.iteye.com/blog/1432957
http://zhidao.baidu.com/question/285759208.html
http://michaelkang.blog.51cto.com/1553154/1065251
http://blog.sina.com.cn/s/blog_7e56997901016bw7.html
http://wenku.baidu.com/view/95b2361ca8114431b90dd8c0.html
http://www.cnblogs.com/see7di/archive/2011/06/15/2239756.html
http://zhidao.baidu.com/question/472396507.html
http://blog.csdn.net/yongshiok/article/details/6936960
http://www.xker.com/page/e2010/0925/98279.html
http://www.codesky.net/article/201101/149128.html

如何更改linux下的Apache端口号

一、修改/etc/httpd/conf/httpd.conf文件中的监听端口号

Listen 80

把80修改成需要的号,如8000,即

Listen 8000
二、查看SELinux下http相关端口

# semanage port -l|grep http
http_cache_port_t              tcp      3128, 8080, 8118, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      80, 443, 488, 8008, 8009, 8443
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989

发现8000不在其范围之内,所以需要另外添加,方法如下:

# semanage port -a -t http_port_t -p tcp 8000

再次查看,

# semanage port -l|grep http
http_cache_port_t              tcp      3128, 8080, 8118, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      8000, 80, 443, 488, 8008, 8009, 8443
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989

三、在防火墙中开放新添加的端口

修改/etc/sysconfig/iptables文件,在文件中添加如一行:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8008 -j ACCEPT

四、重启防火墙和Apache

# service iptables restart

# service httpd restart

五、正常情况下,应该可以通过新端口访问WEB服务了。

注:

1、第二、三、四步骤是在系统已经开启SELinux和防火墙的情况下设置的,如果已经关闭此两个服务,修改端口后直接重启Apache即可;

2、修改的端口号可以是执行#semanage port -l|grep http后,默认已经有的端口,如8443,这样可以省略额外添加SELinux端口操作;

3、第三步操作可以图形界面下完成。

参考资料

1、Permission denied: make_sock: could not bind to address

 http://emmune.blogspot.com/2009/07/permission-denied-makesock-could-not.html

不熟悉python、plone、zope,想用apache。80端口已经不在,就征用81端口凑合吧。修改httpd.conf后apachectl start,结果:
(13)Permission denied: make_sock: could not bind to address [::]:81
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:81

查一下SELinux下http相关端口 semanage port -l|grep http,结果:
http_cache_port_t tcp 3128, 8080, 8118, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 443, 488, 8008, 8009, 8443
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989

直接用man semanage最后例子中的一句
# Allow Apache to listen on port 81
semanage port -a -t http_port_t -p tcp 81
然后再apachectl start,OK。使用域名:81能够访问啦。

注:semanage
semanage is used to configure certain elements of SELinux policy without requiring modification to or recompilation from policy sources. This includes the mapping from Linux usernames to SELinux user identities (which controls the initial security context assigned to Linux users when they login and bounds their authorized role set) as well as security context mappings for various kinds of objects, such as network ports, interfaces, and nodes(hosts) as well as the file context mapping. See the EXAMPLES section below for some examples of common usage. Note that the semanage login command deals with the mapping from Linux usernames (logins) to SELinux user identities, while the semanage user command deals with the mapping from SELinux user identities to authorized role sets. In most cases, only the former mapping needs to be adjusted by the administrator; the latter is principally defined by the base policy and usually does not require modification.

2、linux 下apche无法监听端口解决办法

http://www.zzxj.net/blog/fxs_2008/archive/2010/07/05/187.html

想建立一个测试用的虚拟主机,遇到了这个问题:
[root@localhost html]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.termwikidev for ServerName
(13)Permission denied: make_sock: could not bind to address [::]:81
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:81
no listening sockets available, shutting down
Unable to open logs

解决办法:

semanage port -l|grep http
semanage port -a -t http_port_t -p tcp 81

这个两个命令一是查看,一个是添加,添加完再查看一遍,如果有81,则成功。另可能要以root用户运行。

此外,如果要外网访问,还要打开linux的防火墙:

[root@localhost html]# vim /etc/sysconfig/iptables
[root@localhost html]# service iptables restart

重启apache.

相关资料:

starting httpd 13 permission denied make_sock could not bind to address2010年01月19日 星期二 11:33In Fedora Core 5/6 and RHEL 5. We have made it easier to customize certain common parts of SELinux. In previous releases of SELinux if you wanted to change simple things like which port a daemon could listen to, you would need to write policy. Now we have the semanage utility.

SELinux assigns types to all network ports on a system. By default all ports are less then 1024 are labeled reserved_port_t and all ports > 1024 are labeled port_t. If a port is assigned to a particular type
say the http port 80, it has an assigned type of http_port_t. If you want to look at all the assigned ports in SELinux, you can use the semanage tool, semanage port -l.

So if you executed

semanage port -l | grep http
http_cache_port_t tcp 3128, 8080, 8118
http_cache_port_t udp 3130
http_port_t tcp 80, 443, 488, 8008, 8009, 8443
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989

Here we see http_port_t is assigned to ports 80, 443, 488, 8008, 8009, 8443

The policy is written to allow httpd_t http_port_t:tcp_socket name_bind;

This means the apache command can "bind" to an port that is labeled http_port_t.

So lets say you want to run httpd on port 81.

So you edit /etc/httpd/http.conf

and change this line
Listen 80
to
Listen 81


Now restart the daemon.
service httpd restart
Stopping httpd: [ OK ]
Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:81
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:81
no listening sockets available, shutting down
Unable to open logs
[FAILED]

Now the daemon fails to start because it can not bind to port 81.

This generates an AVC that looks like

----
time->Tue Dec 12 17:37:49 2006
type=SYSCALL msg=audit(1165963069.248:852): arch=40000003 syscall=102 success=no exit=-13 a0=2 a1=bf96a830 a2=b5b1e8 a3=9e58b68 items=0 ppid=21133 pid=21134 auid=3267 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts10 comm="httpd" exe="/usr/sbin/httpd" subj=user_u:system_r:httpd_t:s0 key=(null)
type=AVC msg=audit(1165963069.248:852): avc: denied { name_bind } for pid=21134 comm="httpd" src=81 scontext=user_u:system_r:httpd_t:s0 tcontext=system_u:object_r:reserved_port_t:s0 tclass=tcp_socket

To fix this you can use semanage to add the port

semanage port -a -t http_port_t -p tcp 81

service httpd start
Starting httpd: [ OK ]

posted @ 2013-07-30 18:41 多彩人生 阅读(2155) | 评论 (0)编辑 收藏

php socket connect Permission denied :13

Hi there!

For the TCP connections: socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
In case you're having problems in socket_connect() with socket_strerror() = "Permission denied", you may be having a SELinux config issue.

Check if SELinux is enabled:
# /usr/sbin/sestatus -v
In case it is, you can just type the command:
# setsebool httpd_can_network_connect=1

That's it... I read you had to reboot, but I didn't and it worked fine anyway. More info, you may check:
http://arkiv.netbsd.se/?ml=squirrelmail-users&a=2005-11&t=1523021

posted @ 2013-07-29 17:27 多彩人生 阅读(1903) | 评论 (0)编辑 收藏

PHP的ntohl网络字节序函数及相关知识

http://n3yang.com/archives/2011/12/16/php-function-ntohl/
http://www.w3school.com.cn/php/func_misc_unpack.asp    // php unpack 函数


最近项目中使用到了PHP与C服务器的socket通信,在做数据转换的时候,PHP没有提供对应将网络字节序和机器字节序相互转换的程序,但是根据函数的意义,我们可以做相应的转换来实现这一函数:

1
2
3
4
function ntohl($str){
    $arr = unpack('I', pack('N', $str));
    return $arr[1];
}

函数的意义
程序的作用是将网络字节序和机器字节序进行相互转换。在C/C++、Python、Delphi中都提供了相应的函数“ntohl”和“htonl”,如果是short类型就是“ntohs”和“htons”。下面是这些函数的意义:

ntohl()--"Network to Host Long" htonl()--"Host to Network Long" htons()--"Host to Network Short" ntohs()--"Network to Host Short"

什么是主机字节序
不同的机器字节序不相同,这与使用的CPU有关。不同的CPU在处理高位字节时所存储的顺序是不同的。
例如Intel x86结构下, short型数0×1234表示为34 12, int型数0×12345678表示为78 56 34 12
如IBM power PC结构下, short型数0×1234表示为12 34, int型数0×12345678表示为12 34 56 78

网络字节序
正是由于每个主机的处理顺序有可能不同,所以在网络协议中规定数据从高到低顺序存储,目的是在不同架构的主机中可以正常通信,避免兼容性问题。

posted @ 2013-07-29 14:49 多彩人生 阅读(685) | 评论 (0)编辑 收藏

仅列出标题
共25页: First 2 3 4 5 6 7 8 9 10 Last 

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜