Linux 下的KILL函数的用法

论坛 期权论坛     
选择匿名的用户   2021-5-30 23:21   124   0
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><strong>[ KILL ]功能描述:<br>用于向任何进程组或进程发送信号。</strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><strong>头文件用法:</strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><br><strong>#include &lt;sys/types.h&gt;<br> <br>#include &lt;signal.h&gt;</strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><strong>int kill(pid_t pid, int sig);<br> </strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><strong> </strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><strong>参数: <br>pid:可能选择有以下四种</strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><strong>1. pid大于零时,pid是信号欲送往的进程的标识。<br>2. pid等于零时,信号将送往所有与调用kill()的那个进程属同一个使用组的进程。<br>3. pid等于-1时,信号将送往所有调用进程有权给其发送信号的进程,除了进程1(init)。<br>4. pid小于-1时,信号将送往以-pid为组标识的进程。</strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><span style="line-height:19.5px;color:rgb(255,0,0);"><strong>sig:准备发送的信号代码,假如其值为零则没有任何信号送出,但是系统会执行错误检查,通常会利用sig值为零来检验某个进程是否仍在执行。</strong></span></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><br><strong>返回值说明: 成功执行时,返回0。失败返回-1,errno被设为以下的某个值 EINVAL:指定的信号码无效(参数 sig 不合法) EPERM;权限不够无法传送信号给指定进程 ESRCH:参数 pid 所指定的进程或进程组不存在</strong></p>
<p style="margin:10px auto;color:rgb(75,75,75);font-family:georgia, verdana, Arial, helvetica, &#39;sans-seriff&#39;;font-size:13px;background-color:rgb(255,255,255);"><strong>代码</strong></p>
<div class="cnblogs_code" style="background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);padding:5px;margin:5px 0px;color:rgb(0,0,0);font-family:&#39;Courier New&#39;;font-size:12px;">
<div class="cnblogs_code_toolbar" style="margin-top:5px;">
  <span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a style="color:rgb(67,113,166);border:none;" title="复制代码"><img alt="复制代码" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-48304ba5e6f9fe08f3fa1abda7d326ab.gif" style="max-width:900px;border:none;"></a></span>
</div>
<pre style="margin-bottom:0px;white-space:pre-wrap;font-family:&#39;Courier New&#39;;"><span style="font-weight:bold;"><span style="color:rgb(0,128,128);line-height:1.5;"> 1 #include &lt;sys/wait.h&gt;
<span style="line-height:1.5;"> 2 #include &lt;sys/types.h&gt;
<span style="line-height:1.5;"> 3 #include &lt;stdio.h&gt;
<span style="line-height:1.5;"> 4 #include &lt;stdlib.h&gt;
<span style="line-height:1.5;"> 5 #include &lt;signal.h&gt; <span style="line-height:1.5;"> 6 <span style="line-height:1.5;"> 7 <span style="color:rgb(0,0,255);line-height:1.5;">int main( <span style="line-height:1.5;">void<span style="color:rgb(0,0,0);line-height:1.5;"> ) <span style="color:rgb(0,128,128);line-height:1.5;"> 8 <span style="color:rgb(0,0,0);line-height:1.5;">{ <span style="color:rgb(0,128,128);line-height:1.5;"> 9 <span style="color:rgb(0,0,0);line-height:1.5;"> pid_t childpid; <span style="color:rgb(0,128,128);line-height:1.5;">10 <span style="color:rgb(0,0,255);line-height:1.5;">int<span style="color:rgb(0,0,0);line-height:1.5;"> status; <span style="color:rgb(0,128,128);line-height:1.5;">11 <span style="color:rgb(0,0,255);line-height:1.5;">int<span style="color:rgb(0,0,0);line-height:1.5;"> retval; <span style="color:rgb(0,128,128);line-height:1.5;">12 <span style="line-height:1.5;">13 childpid &#61;<span style="color:rgb(0,0,0);line-height:1.5;"> fork(); <span style="color:rgb(0,128,128);line-height:1.5;">14 <span style="color:rgb(0,0,255);line-height:1.5;">if ( -<span style="color:rgb(128,0,128);line-height:1.5;">1 &#61;&#61;<span style="color:rgb(0,0,0);line-height:1.5;"> childpid ) <span style="color:rgb(0,128,128);line-height:1.5;">15 <span style="color:rgb(0,0,0);line-height:1.5;"> { <span style="color:rgb(0,128,128);line-height
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP