﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-飛天-随笔分类-linux c函数</title><link>http://www.cppblog.com/cjz/category/18433.html</link><description>快乐的生活......</description><language>zh-cn</language><lastBuildDate>Thu, 29 Dec 2011 15:02:31 GMT</lastBuildDate><pubDate>Thu, 29 Dec 2011 15:02:31 GMT</pubDate><ttl>60</ttl><item><title>Daemon函数的用法</title><link>http://www.cppblog.com/cjz/archive/2011/12/29/163123.html</link><dc:creator>飛天</dc:creator><author>飛天</author><pubDate>Thu, 29 Dec 2011 09:27:00 GMT</pubDate><guid>http://www.cppblog.com/cjz/archive/2011/12/29/163123.html</guid><wfw:comment>http://www.cppblog.com/cjz/comments/163123.html</wfw:comment><comments>http://www.cppblog.com/cjz/archive/2011/12/29/163123.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cjz/comments/commentRss/163123.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cjz/services/trackbacks/163123.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; "><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: medium; ">Daemon</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: medium; ">函数的用法</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">说明：</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">让一个程序后台运行。</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">原型：</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; "></span></span></p><div class="dp-highlighter bg_c-sharp" style="width: 687px; "><div class="bar"><div class="tools"><a href="http://blog.csdn.net/callmeback/article/details/5884579#" class="ViewSource" title="view plain" style="background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); border-width: initial; border-color: initial; padding-top: 1px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">view plain</a></div></div><ol start="1" class="dp-c" style="border-width: initial; border-color: initial; "><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="preprocessor" style="border-width: initial; border-color: initial; ">#include&nbsp;&lt;unistd.h&gt;</span><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="keyword" style="border-width: initial; border-color: initial; ">int</span><span style="border-width: initial; border-color: initial; ">&nbsp;daemon(</span><span class="keyword" style="border-width: initial; border-color: initial; ">int</span><span style="border-width: initial; border-color: initial; ">&nbsp;nochdir,&nbsp;</span><span class="keyword" style="border-width: initial; border-color: initial; ">int</span><span style="border-width: initial; border-color: initial; ">&nbsp;noclose);&nbsp;&nbsp;</span></span></li></ol></div></span><p>&nbsp;</p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">参数：</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">当&nbsp;</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">nochdir</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">为零时，当前目录变为根目录，否则不变；</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">当&nbsp;</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">noclose</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">为零时，标准输入、标准输出和错误输出重导向为</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">/dev/null</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">，也就是不输出任何信</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">&nbsp;</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">息，否则照样输出。</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">返回值：</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">deamon()</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">调用了</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">fork()</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">，如果</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">fork</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">成功，那么父进程就调用</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">_exit(2)</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">退出，所以看到的错误信息</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">&nbsp;</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">全部是子进程产生的。如果成功函数返回</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">0</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">，否则返回</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">-1</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">并设置</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">errno</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">。</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">示例：</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "></p><div class="dp-highlighter bg_c-sharp" style="width: 687px; "><div class="bar"><div class="tools"><a href="http://blog.csdn.net/callmeback/article/details/5884579#" class="ViewSource" title="view plain" style="background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); border-width: initial; border-color: initial; padding-top: 1px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">view plain</a></div></div><ol start="1" class="dp-c" style="border-width: initial; border-color: initial; "><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="preprocessor" style="border-width: initial; border-color: initial; ">#include&nbsp;&lt;stdio.h&gt;</span><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="preprocessor" style="border-width: initial; border-color: initial; ">#include&nbsp;&lt;stdlib.h&gt;</span><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="preprocessor" style="border-width: initial; border-color: initial; ">#include&nbsp;&lt;unistd.h&gt;</span><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="preprocessor" style="border-width: initial; border-color: initial; ">#include&nbsp;&lt;fcntl.h&gt;</span><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="preprocessor" style="border-width: initial; border-color: initial; ">#include&nbsp;&lt;limits.h&gt;</span><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; "><span class="keyword" style="border-width: initial; border-color: initial; ">int</span><span style="border-width: initial; border-color: initial; ">&nbsp;main(</span><span class="keyword" style="border-width: initial; border-color: initial; ">int</span><span style="border-width: initial; border-color: initial; ">&nbsp;argc,&nbsp;</span><span class="keyword" style="border-width: initial; border-color: initial; ">char</span><span style="border-width: initial; border-color: initial; ">&nbsp;*argv[])&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">{&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="border-width: initial; border-color: initial; ">char</span><span style="border-width: initial; border-color: initial; ">&nbsp;strCurPath[PATH_MAX];&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="border-width: initial; border-color: initial; ">if</span><span style="border-width: initial; border-color: initial; ">(daemon(1,&nbsp;1)&nbsp;&lt;&nbsp;0)&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(<span class="string" style="border-width: initial; border-color: initial; ">"error&nbsp;daemon.../n"</span><span style="border-width: initial; border-color: initial; ">);&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;sleep(10);&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;</span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="border-width: initial; border-color: initial; ">if</span><span style="border-width: initial; border-color: initial; ">(getcwd(strCurPath,&nbsp;PATH_MAX)&nbsp;==&nbsp;NULL)&nbsp;&nbsp;</span></span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(<span class="string" style="border-width: initial; border-color: initial; ">"error&nbsp;getcwd"</span><span style="border-width: initial; border-color: initial; ">);&nbsp;&nbsp;</span></span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);&nbsp;&nbsp;</span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;printf(<span class="string" style="border-width: initial; border-color: initial; ">"%s/n"</span><span style="border-width: initial; border-color: initial; ">,&nbsp;strCurPath);&nbsp;&nbsp;</span></span></li><li class="" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="border-width: initial; border-color: initial; ">return</span><span style="border-width: initial; border-color: initial; ">&nbsp;0;&nbsp;&nbsp;</span></span></li><li class="alt" style="border-width: initial; border-color: initial; line-height: 18px; "><span style="border-width: initial; border-color: initial; ">}&nbsp;&nbsp;</span></li></ol></div><p>&nbsp;</p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">假如运行成功，父进程在</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">daemon</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">函数运行完毕后自杀，以后的休眠和打印全部是子进程来运行。</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">可以修改</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">daemon</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">函数的参数来查看效果。</span></span></p><p lang="zh-CN" style="margin-bottom: 0cm; "><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">可以去掉</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">daemon</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">一句，用</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">./a.out&amp;</span></span><span style="font-family: 微软雅黑, sans-serif; "><span style="font-size: x-small; ">来验证效果。</span></span></p><img src ="http://www.cppblog.com/cjz/aggbug/163123.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cjz/" target="_blank">飛天</a> 2011-12-29 17:27 <a href="http://www.cppblog.com/cjz/archive/2011/12/29/163123.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>strdup函数简介</title><link>http://www.cppblog.com/cjz/archive/2011/12/29/163113.html</link><dc:creator>飛天</dc:creator><author>飛天</author><pubDate>Thu, 29 Dec 2011 07:52:00 GMT</pubDate><guid>http://www.cppblog.com/cjz/archive/2011/12/29/163113.html</guid><wfw:comment>http://www.cppblog.com/cjz/comments/163113.html</wfw:comment><comments>http://www.cppblog.com/cjz/archive/2011/12/29/163113.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cjz/comments/commentRss/163113.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cjz/services/trackbacks/163113.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff; ">用法：#include &lt;string.h&gt;<br />&nbsp;功能：复制字符串s<br />&nbsp;<br />&nbsp;说明：返回指向被复制的字符串的指针，所需空间由malloc()分配且可以由free()释放。<br />&nbsp;<br />&nbsp;举例：<br />&nbsp;<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // strdup.c<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include &lt;syslib.h&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include &lt;string.h&gt;<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *s="this is just f";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *d;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d=strdup(s);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%s",d);<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getchar();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /><br />strdup()主要是拷贝字符串s的一个副本，由函数返回值返回，这个副本有自己的内存空间，和s不相干。<br /><br />char *strdup(const char *s)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *t = NULL;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (s &amp;&amp; (t = (char*)malloc(strlen(s) + 1)))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcpy(t, s);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t;<br />}</span><img src ="http://www.cppblog.com/cjz/aggbug/163113.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cjz/" target="_blank">飛天</a> 2011-12-29 15:52 <a href="http://www.cppblog.com/cjz/archive/2011/12/29/163113.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>