<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[[c++]获得系统名称等信息]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Mon, 24 Sep 2007 06:40:05 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	内容：<br/>系统标识<br/>时间与日期<br/>多进程编程<br/><br/><br/>1. 系统标识符<br/> &nbsp; &nbsp;a.获得有关的系统信息<br/> &nbsp; &nbsp; &nbsp; #include<sys/types.h><br/> &nbsp; &nbsp; &nbsp; int uname( struct utsname * name);// 用 man uname 查看 struct utsname<br/> &nbsp; &nbsp;b.获得系统的名称<br/> &nbsp; &nbsp; &nbsp; #include<unistd.h><br/> &nbsp; &nbsp; &nbsp; int gethostname( char* name, int namelen ); // 成功返回 0， 否则返回 1<br/> &nbsp; &nbsp; &nbsp; 例子：<br/> &nbsp; &nbsp; &nbsp;<div class="code"><br/> &nbsp; &nbsp; &nbsp; #include&lt;iostream&gt;<br/> &nbsp;#include&lt;unistd.h&gt;<br/> &nbsp;#include&lt;sys/utsname.h&gt;<br/> &nbsp;<br/> &nbsp;using namespace std;<br/> &nbsp;<br/> &nbsp;int main()&#123;<br/> &nbsp; cout&lt;&lt;&quot;-------------- hostname ------------&quot;&lt;&lt;endl;<br/> &nbsp; char name&#91; 100 &#93;;<br/> &nbsp; memset( name, 0x00, sizeof( name ) );<br/> &nbsp; gethostname( name, sizeof( name ) );<br/> &nbsp; cout&lt;&lt; name &lt;&lt;endl;<br/> &nbsp; <br/> &nbsp; cout&lt;&lt;&quot;-------------- uname ----------------&quot;&lt;&lt;endl;<br/> &nbsp; struct utsname nm;<br/> &nbsp; memset( &amp;nm, 0x00, sizeof( nm ) );<br/> &nbsp; uname( &amp;nm );<br/> &nbsp; cout&lt;&lt;&quot;sysname =&quot;&lt;&lt; nm.sysname &lt;&lt;endl;<br/> &nbsp; cout&lt;&lt;&quot;nodename =&quot;&lt;&lt; nm.nodename &lt;&lt;endl;<br/> &nbsp; cout&lt;&lt;&quot;release =&quot;&lt;&lt; nm.release &lt;&lt;endl;<br/> &nbsp; cout&lt;&lt;&quot;version =&quot;&lt;&lt; nm.version &lt;&lt;endl;<br/> &nbsp; cout&lt;&lt;&quot;machine =&quot;&lt;&lt; nm.machine &lt;&lt;endl;<br/> &nbsp; return 0;<br/> &nbsp;&#125;<br/> &nbsp;</div><br/> &nbsp;<br/>2. 时间与日期<br/> &nbsp; &nbsp;4种表示形式：（1）time_t 1970年至今的秒数 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (2) struct tm 以结构表示<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(3) char * 字符串 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (4) formated char* &nbsp;自定义格式<br/> &nbsp; &nbsp;(1)-->(2) 转换有函数： localtime() // 本地时间 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gmtime() // 格林威治时间<br/> &nbsp; &nbsp;(2)-->(1) 转换有函数： mktime()<br/> &nbsp; &nbsp;(1)-->(3) 转换有函数： ctime()<br/> &nbsp; &nbsp;(2)-->(3) 转换有函数： asctime()<br/> &nbsp; &nbsp;(2)-->(4) 转换有函数： strftime()<br/> &nbsp; &nbsp;例子：<br/> &nbsp; &nbsp;<div class="code"><br/> &nbsp; &nbsp;#include&lt;iostream&gt;<br/> #include&lt;time.h&gt;<br/> using namespace std;<br/> <br/> int main()&#123;<br/> &nbsp;// time() get kernel time<br/> &nbsp;cout&lt;&lt;&quot;-----------time_t-----------&quot;&lt;&lt;endl;<br/> &nbsp;time_t t=0;<br/> &nbsp;time( &amp;t );<br/> &nbsp;cout&lt;&lt; t &lt;&lt;endl;<br/> &nbsp;<br/> &nbsp;// time_t --&gt; struct tm, localtime(),gmtime()<br/> &nbsp;// man localtime to see the struct tm<br/> &nbsp;cout&lt;&lt;&quot;-----------struct tm-----------&quot;&lt;&lt;endl;<br/> &nbsp;struct tm *p=NULL; <br/> &nbsp;p=localtime( &amp;t ); &nbsp;<br/> &nbsp;cout&lt;&lt;&quot;year:&quot;&lt;&lt; p-&gt;tm_year+1900 &lt;&lt;endl; // the number of years since 1900<br/> &nbsp;cout&lt;&lt;&quot;month:&quot;&lt;&lt; p-&gt;tm_mon+1 &lt;&lt;endl; // tm_mon ranges 0 to 11<br/> &nbsp;cout&lt;&lt;&quot;day:&quot;&lt;&lt; p-&gt;tm_mday &lt;&lt;endl;<br/> &nbsp;<br/> &nbsp;// struct tm --&gt; time_t<br/> &nbsp;cout&lt;&lt;&quot;-----------mktime-----------&quot;&lt;&lt;endl;<br/> &nbsp;time_t t1;<br/> &nbsp;t1=mktime( p );<br/> &nbsp;cout&lt;&lt; t1 &lt;&lt;endl;<br/> &nbsp;<br/> &nbsp;// time_t --&gt; char *<br/> &nbsp;cout&lt;&lt;&quot;-----------ctime-----------&quot;&lt;&lt;endl;<br/> &nbsp;cout&lt;&lt; ctime( &amp;t ) &lt;&lt;endl;<br/> &nbsp;char ct&#91; 100 &#93;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//如果想保持返回值应开辟新空间，不能直接定义指针保存<br/> &nbsp;memset( ct, 0x00, sizeof( ct ) ); //因为ctime的返回值总是在同一空间，下次调用ctime时就会被更改<br/> &nbsp;strcpy( ct, ctime( &amp;t ) );<br/> &nbsp;cout&lt;&lt; ct &lt;&lt;endl;<br/> &nbsp;<br/> &nbsp;// struct tm --&gt; char *<br/> &nbsp;cout&lt;&lt;&quot;-----------asctime-----------&quot;&lt;&lt;endl;<br/> &nbsp;cout&lt;&lt; asctime( p ) &lt;&lt;endl;<br/> &nbsp;<br/> &nbsp;// struct tm --&gt; formated char*<br/> &nbsp;cout&lt;&lt;&quot;-----------strftime-----------&quot;&lt;&lt;endl;<br/> &nbsp;char ft&#91; 100 &#93;;<br/> &nbsp;memset( ft, 0x00, sizeof( ft ) );<br/> &nbsp;strftime( ft, sizeof( ft ), &quot;%Y-%m-%d %H:%M:%S&quot;, p );<br/> &nbsp;cout&lt;&lt; ft &lt;&lt;endl;<br/> &nbsp;return 0;<br/> &#125;<br/> </div><br/><br/>3.system 函数<br/> &nbsp; #include<stdlib.h><br/> &nbsp; int system( const char * string );<br/> &nbsp; 执行string 所表示的命令，将产生一个新的进程，system为阻塞函数， 新的进程结束后才继续<br/> &nbsp; 例子：<br/> &nbsp;<div class="code"><br/> &nbsp; #include&lt;iostream&gt;<br/> #include&lt;stdlib.h&gt;<br/> using namespace std;<br/> <br/> int main()&#123;<br/> &nbsp;cout&lt;&lt;&quot;----------begin---------&quot;&lt;&lt;endl;<br/> &nbsp;system(&quot;ls -l&quot;);<br/> &nbsp;cout&lt;&lt;&quot;----------end-----------&quot;&lt;&lt;endl;<br/> &nbsp;return 0;<br/> &#125;<br/> <br/> // 一个简单的shell<br/> #include&lt;iostream&gt;<br/> #include&lt;unistd.h&gt;<br/> using namespace std;<br/> <br/> int main()&#123;<br/> &nbsp;char cmd&#91; 100 &#93;;<br/> &nbsp;memset( cmd, 0x00, sizeof( cmd ) );<br/> &nbsp;while( 1 )&#123;<br/> &nbsp; cout&lt;&lt;&quot;&#91;irini@localhost&#93;#&quot;;<br/> &nbsp; cin.getline( cmd,sizeof( cmd ) );<br/> &nbsp; if( strcmp( cmd,&quot;bye&quot; )==0 ) break;<br/> &nbsp; system( cmd );<br/> &nbsp;&#125;<br/> &nbsp;return 0;<br/> &#125;<br/><br/> </div><br/> <br/>4. atexit() 函数<br/> &nbsp; #include<stdlib.h><br/> &nbsp; int atexit( void (*func) (void) );<br/> &nbsp; 登记exit handler，最多可登记32个，在进程退出时最后登记的先调用，最先登记的最后调用<br/> &nbsp; <br/>5. exit 与 _exit<br/> &nbsp; &nbsp;* 进程的退出过程：<br/> &nbsp; &nbsp;进程做的事： exit handler（ atexit注册的), 关闭IO流，如果申请了堆空间就释放<br/> &nbsp; &nbsp;------------------------------------------------------------------<br/> &nbsp; &nbsp;kernel做的事： 销毁进程空间， 删除进程表中的相应项<br/> &nbsp; &nbsp;* &nbsp;exit 是正常退出，想做进程的，然后进入kernel处理<br/> &nbsp; &nbsp; &nbsp; _exit 是异常退出，直接进入kernel<br/> &nbsp; &nbsp;<br/> &nbsp; &nbsp;例子：<br/> &nbsp; &nbsp;<div class="code"><br/> &nbsp; &nbsp;#include&lt;iostream&gt;<br/> #include&lt;unistd.h&gt;<br/> using namespace std;<br/> <br/> void fn1()&#123;<br/> &nbsp;cout&lt;&lt;&quot;in fn1()...&quot;&lt;&lt;endl;<br/> &#125;<br/> void fn2()&#123;<br/> &nbsp;cout&lt;&lt;&quot;in fn2()...&quot;&lt;&lt;endl;<br/> &#125;<br/> int main()&#123;<br/> &nbsp;atexit( fn1 );<br/> &nbsp;atexit( fn2 );<br/> &nbsp;cout&lt;&lt;&quot;return from main...&quot;&lt;&lt;endl;<br/> &nbsp;//exit( 0 );<br/> &nbsp;_exit( 0 );<br/> &#125;<br/> </div><br/><br/> <br/>6. 进程标识符<br/> &nbsp; #include<sys/types.h><br/> &nbsp; #include<unistd.h><br/> &nbsp; pid_t getpid(); // 当前进程号<br/> &nbsp; pid_t getppid(); // 得到父进程号<br/> &nbsp; 例子：<br/> &nbsp;<div class="code"><br/> &nbsp; #include&lt;iostream&gt;<br/> #include&lt;sys/types.h&gt;<br/> #include&lt;unistd.h&gt;<br/> using namespace std;<br/> <br/> int main()&#123;<br/> &nbsp;cout&lt;&lt;&quot;pid=&quot;&lt;&lt;getpid()&lt;&lt;endl;<br/> &nbsp;cout&lt;&lt;&quot;ppid=&quot;&lt;&lt;getppid()&lt;&lt;endl;<br/> &nbsp;return 0;<br/> &#125;<br/> </div><br/><br/><br/>7. fork 函数<br/> &nbsp; &nbsp;* 创建一个新进程，这个进程是父进程的完全拷贝，完全拷贝父进程的进程空间，唯一的区别是fork()的返回值不同<br/> &nbsp; &nbsp; &nbsp;返回给父进程的是子进程的pid, 返回给子进程的是0 &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp;<br/> &nbsp; &nbsp; &nbsp;例子：<br/> &nbsp; &nbsp; &nbsp;<div class="code"><br/> &nbsp; &nbsp; &nbsp;#include&lt;iostream&gt;<br/> &nbsp;#include&lt;sys/types.h&gt;<br/> &nbsp;#include&lt;unistd.h&gt;<br/> &nbsp;using namespace std;<br/> &nbsp;<br/> &nbsp;int main()&#123;<br/> &nbsp; pid_t cid=fork();<br/> &nbsp; if( cid==0 )&#123;<br/> &nbsp; &nbsp;for( int i=0; i&lt;5; i++ )&#123;<br/> &nbsp; &nbsp; cout&lt;&lt;&quot;&#91;child&#93; pid=&quot;&lt;&lt;getpid()&lt;&lt;&quot; ppid=&quot;&lt;&lt;getppid()&lt;&lt;endl;<br/> &nbsp; &nbsp; sleep( 1 );<br/> &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp;exit( 0 );<br/> &nbsp; &#125;else if( cid &gt; 0 )&#123;<br/> &nbsp; &nbsp;for( int i=0; i&lt;5; i++ )&#123;<br/> &nbsp; &nbsp; cout&lt;&lt;&quot;&#91;father&#93; pid=&quot;&lt;&lt;getpid()&lt;&lt;&quot; cid=&quot;&lt;&lt;cid&lt;&lt;endl;<br/> &nbsp; &nbsp; sleep( 1 );<br/> &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp;exit( 0 );<br/> &nbsp; &#125;else&#123;<br/> &nbsp; &nbsp;cout&lt;&lt;&quot;error&quot;&lt;&lt;endl;<br/> &nbsp; &nbsp;exit( -1 );<br/> &nbsp; &#125;<br/> &nbsp; return 0;<br/> &nbsp;&#125;<br/> &nbsp;</div><br/> &nbsp;<br/> &nbsp; * fork后，父子进程相互独立，如果之前父进程申请了一个堆空间，那之后父子进程中的指针值相同吗？<br/> &nbsp; &nbsp; 通过程序可验证他们是相同的，但指向的空间是不同，因为进程中分配给指针的不是绝对地址，<br/> &nbsp; &nbsp; 是逻辑偏移地址，进程空间中正文段的起始地址是逻辑0<br/> &nbsp; &nbsp; <br/> &nbsp; * 如果父进程在子进程之前退出，子进程会被初始化进程（pid=1) 托管<br/> &nbsp; &nbsp; 如果子进程在父进程之前退出，子进程不会被销毁，变为僵死进程 Z状态，等待父进程处理<br/> &nbsp; &nbsp; <br/> &nbsp; &nbsp; 例子：<br/> &nbsp; &nbsp;<div class="code"><br/> &nbsp; &nbsp; #include&lt;iostream&gt;<br/> &nbsp;#include&lt;sys/types.h&gt;<br/> &nbsp;#include&lt;unistd.h&gt;<br/> &nbsp;using namespace std;<br/> &nbsp;<br/> &nbsp;void fn()&#123;<br/> &nbsp; cout&lt;&lt;&quot;in fn()... &quot;&lt;&lt;endl;<br/> &nbsp;&#125;<br/> &nbsp;int main()&#123;<br/> &nbsp; atexit( fn );<br/> &nbsp; pid_t cid=fork();<br/> &nbsp; if( cid==0 )&#123;<br/> &nbsp; &nbsp;for( int i=0; i&lt;5; i++ )&#123;<br/> &nbsp; &nbsp; cout&lt;&lt;&quot;&#91;child&#93; pid=&quot;&lt;&lt;getpid()&lt;&lt;endl;<br/> &nbsp; &nbsp; sleep( 1 );<br/> &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp;exit( 0 );<br/> &nbsp; &#125;else if( cid &gt; 0 )&#123;<br/> &nbsp; &nbsp;cout&lt;&lt;&quot;father exit...&quot;&lt;&lt;endl;<br/> &nbsp; &nbsp;exit( 0 );<br/> &nbsp; &#125;else&#123;<br/> &nbsp; &nbsp;cout&lt;&lt;&quot;error&quot;&lt;&lt;endl;<br/> &nbsp; &nbsp;exit( -1 );<br/> &nbsp; &#125;<br/> &nbsp; return 0;<br/> &nbsp;&#125;<br/> &nbsp;</div><br/> &nbsp;<br/>8. wait 和 waitpid 函数<br/> &nbsp; &nbsp;处理结束的子进程，是阻塞函数<br/> &nbsp; &nbsp;#include<sys/types.h><br/> &nbsp; &nbsp;#include<sys/wait.h><br/> &nbsp; &nbsp;pid_t wait( int * statloc );<br/> &nbsp; &nbsp;pid_t waitpid( pid_t pid, int *static, int option );<br/> &nbsp; &nbsp;返回值为子进程的pid<br/> &nbsp; &nbsp;statloc 用于接受终止的子进程的返回状态<br/> &nbsp; &nbsp;option 通常设为0 &nbsp;<br/> &nbsp; &nbsp;<br/> &nbsp; &nbsp;<br/> &nbsp; &nbsp;例子：<br/> &nbsp; &nbsp;<div class="code"><br/> &nbsp; &nbsp;#include&lt;iostream&gt;<br/> #include&lt;sys/types.h&gt;<br/> #include&lt;sys/wait.h&gt;<br/> #include&lt;unistd.h&gt;<br/> using namespace std;<br/> <br/> int main()&#123;<br/> &nbsp;pid_t cid=fork();<br/> &nbsp;if( cid==0 )&#123;<br/> &nbsp; sleep( 3 );<br/> &nbsp; cout&lt;&lt;&quot;&#91;child&#93; pid=&quot;&lt;&lt;getpid()&lt;&lt;&quot; ppid=&quot;&lt;&lt;getppid()&lt;&lt;endl;<br/> &nbsp; exit( 0 );<br/> &nbsp;&#125;else if( cid &gt; 0 )&#123;<br/> &nbsp; cout&lt;&lt;&quot;&#91;father&#93; pid=&quot;&lt;&lt;getpid()&lt;&lt;&quot; cid=&quot;&lt;&lt;cid&lt;&lt;endl;<br/> &nbsp; int statloc=0;<br/> &nbsp; pid_t id=waitpid( cid, &amp;statloc, 0 );<br/> &nbsp; cout&lt;&lt;&quot;&#91;child&#93; &quot;&lt;&lt; id &lt;&lt;&quot; exited&quot;&lt;&lt;endl;<br/> &nbsp; cout&lt;&lt;&quot;exit value is &quot;&lt;&lt; statloc &lt;&lt;endl;<br/> &nbsp; if( WIFEXITED( statloc ) )<br/> &nbsp; &nbsp;cout&lt;&lt;&quot;value &quot;&lt;&lt;WEXITSTATUS( statloc )&lt;&lt;endl;<br/> &nbsp; else &nbsp;cout&lt;&lt;&quot;signal &quot;&lt;&lt;WTERMSIG( statloc )&lt;&lt;endl;<br/> &nbsp;&#125;else&#123;<br/> &nbsp; cout&lt;&lt;&quot;error&quot;&lt;&lt;endl;<br/> &nbsp; exit( -1 );<br/> &nbsp;&#125;<br/> &nbsp;return 0;<br/> &#125;<br/> </div><br/> <br/>9. exec 函数<br/> &nbsp; 类似system，建立新的进程，但不新建进程空间，而是把原进程空间清0变成自己的开始执行<br/> &nbsp; 则原进程exec之后的代码不在存在，失效<br/> &nbsp; <br/> &nbsp; 例子：<br/> &nbsp; [code]<br/> &nbsp; #include<iostream><br/> #include<unistd.h><br/> using namespace std;<br/> <br/> int main()&#123;<br/> &nbsp;cout<<"-------------begin-----------"<<endl;<br/> &nbsp;//execlp( "ls","ls","-l","-a",NULL ); &nbsp; // 第一个参数为要执行的命令文件名，后面的为命令行参数（从0开始都要写，以NULL结束）<br/> &nbsp;char* argv[]=&#123; "ls","-l","-a",NULL &#125;;<br/> &nbsp;execvp( "ls", argv );<br/> &nbsp;cout<<"-------------end-------------"<<endl;<br/> &nbsp;return 0;<br/> &#125;<br/>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [c++]获得系统名称等信息]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>