⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux环境进程间通信(二):信号(下).htm

📁 描述unix,linux下进程间通信方式
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	int i;
	for(i=0;i<10;i++)
	{
		printf("%c\n ",(*( (char*)((*info).si_ptr)+i)));
	}
	printf("handle signal %d over;",signum);
}

</PRE></CODE>
        <P>这个例子中,信号实现了附加信息的传递,信号究竟如何对这些信息进行处理则取决于具体的应用。</P>
        <LI>2、 不同进程间传递整型参数:把1中的信号发送和接收放在两个程序中,并且在发送过程中传递整型参数。<BR>信号接收程序: <PRE><CODE>#include &lt;signal.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;unistd.h&gt;
void new_op(int,siginfo_t*,void*);
int main(int argc,char**argv)
{
	struct sigaction act;
	int sig;
	pid_t pid;		
	
	pid=getpid();
	sig=atoi(argv[1]);	
	
	sigemptyset(&amp;act.sa_mask);
	act.sa_sigaction=new_op;
	act.sa_flags=SA_SIGINFO;
	if(sigaction(sig,&amp;act,NULL)&lt;0)
	{
		printf("install sigal error\n");
	}
	while(1)
	{
		sleep(2);
		printf("wait for the signal\n");
	}

}
void new_op(int signum,siginfo_t *info,void *myact)
{
	printf("the int value is %d \n",info-&gt;si_int);
}

</PRE></CODE>
        <P>信号发送程序:命令行第二个参数为信号值,第三个参数为接收进程ID。</P><PRE><CODE>#include &lt;signal.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
main(int argc,char**argv)
{
	pid_t pid;
	int signum;
	union sigval mysigval;

	signum=atoi(argv[1]);
	pid=(pid_t)atoi(argv[2]);
	mysigval.sival_int=8;//不代表具体含义,只用于说明问题

	if(sigqueue(pid,signum,mysigval)==-1)
		printf("send error\n");
	sleep(2);
}

</PRE></CODE>
        <P><B>注:</B>实例2的两个例子侧重点在于用信号来传递信息,目前关于在linux下通过信号传递信息的实例非常少,倒是Unix下有一些,但传递的基本上都是关于传递一个整数,传递指针的我还没看到。我一直没有实现不同进程间的指针传递(实际上更有意义),也许在实现方法上存在问题吧,请实现者email我。</P></LI></OL><!--example3-->
      <P><B>实例三:信号阻塞及信号集操作</B></P><PRE><CODE>#include "signal.h"
#include "unistd.h"
static void my_op(int);
main()
{
	sigset_t new_mask,old_mask,pending_mask;
	struct sigaction act;

	sigemptyset(&amp;act.sa_mask);
	act.sa_flags=SA_SIGINFO;
	act.sa_sigaction=(void*)my_op;
	if(sigaction(SIGRTMIN+10,&amp;act,NULL))
		printf("install signal SIGRTMIN+10 error\n");

	sigemptyset(&amp;new_mask);
	sigaddset(&amp;new_mask,SIGRTMIN+10);
	if(sigprocmask(SIG_BLOCK, &amp;new_mask,&amp;old_mask))
		printf("block signal SIGRTMIN+10 error\n");

	sleep(10);	
	printf("now begin to get pending mask and unblock SIGRTMIN+10\n");
	if(sigpending(&amp;pending_mask)&lt;0)
		printf("get pending mask error\n");
	if(sigismember(&amp;pending_mask,SIGRTMIN+10))
		printf("signal SIGRTMIN+10 is pending\n");

	if(sigprocmask(SIG_SETMASK,&amp;old_mask,NULL)&lt;0)
		printf("unblock signal error\n");
	printf("signal unblocked\n");

	sleep(10);
}
static void my_op(int signum)
{
	printf("receive signal %d \n",signum);
}

</PRE></CODE>
      <P>编译该程序,并以后台方式运行。在另一终端向该进程发送信号(运行kill -s 42 
      pid,SIGRTMIN+10为42),查看结果可以看出几个关键函数的运行机制,信号集相关操作比较简单。</P>
      <P><B>注:</B>在上面几个实例中,使用了printf()函数,只是作为诊断工具,pringf()函数是不可重入的,不应在信号处理函数中使用。</P>
      <P><A name=4><SPAN class=atitle2>结束语:</SPAN></A></P>
      <P>系统地对linux信号机制进行分析、总结使我受益匪浅!感谢王小乐等网友的支持!<BR>Comments and suggestions are 
      greatly welcome! </P>
      <P><A name=5><SPAN class=atitle2>附录1:</SPAN></A></P>
      <P>用sigqueue实现的命令行信号发送程序sigqueuesend,命令行第二个参数是发送的信号值,第三个参数是接收该信号的进程ID,可以配合实例一使用:</P><PRE><CODE>#include &lt;signal.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;unistd.h&gt;
int main(int argc,char**argv)
{
	pid_t pid;
	int sig;
	sig=atoi(argv[1]);
	pid=atoi(argv[2]);
	sigqueue(pid,sig,NULL);
	sleep(2);
}

</PRE></CODE><!--<p>[Next paragraph goes here]</p><p><b>Figure x.牋 Figure example (Note: figure # separated from caption by two hard-coded blanks)</b><br /><img alt="" height="200" src="xxx.jpg" width="550" /></p><p>[Next paragraph goes here]</p>--><!-- RESOURCES-->
      <P><A name=resources><SPAN class=atitle2>参考文献:</SPAN></A>
      <UL><!-- Comment out list item below if there is no forum for this article-->
        <LI>linux内核源代码情景分析(上),毛德操、胡希明著,浙江大学出版社,当要验证某个结论、想法时,最好的参考资料; 
        <LI>UNIX环境高级编程,作者:W.Richard Stevens,译者:尤晋元等,机械工业出版社。对信号机制的发展过程阐述的比较详细。 
        <LI>signal、sigaction、kill等手册,最直接而可靠的参考资料。 
        <LI><A 
        href="http://www.linuxjournal.com/modules.php?op=modload&amp;name=NS-help&amp;file=man">http://www.linuxjournal.com/modules.php?op=modload&amp;name=NS-help&amp;file=man</A>提供了许多系统调用、库函数等的在线指南。 

        <LI><A 
        href="http://www.opengroup.org/onlinepubs/007904975/">http://www.opengroup.org/onlinepubs/007904975/</A>可以在这里对许多关键函数(包括系统调用)进行查询,非常好的一个网址。 

        <LI><A 
        href="http://unix.org/whitepapers/reentrant.html">http://unix.org/whitepapers/reentrant.html</A>对函数可重入进行了阐述。 

        <LI><A 
        href="http://www.uccs.edu/~compsvcs/doc-cdrom/DOCS/HTML/APS33DTE/DOCU_006.HTM">http://www.uccs.edu/~compsvcs/doc-cdrom/DOCS/HTML/APS33DTE/DOCU_006.HTM</A>对实时信号给出了相当好的描述。 
        </LI></UL>
      <P></P><!-- AUTHOR BIOS--><!-- Make author heading singular or plural as needed-->
      <TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
        <TBODY>
        <TR>
          <TD><A name=author1><SPAN 
            class=atitle2>关于作者</SPAN></A><BR>郑彦兴,国防科大攻读博士学位。联系方式: <A 
            href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part2/mlinux@163.com">mlinux@163.com</A>. 
          </TD></TR></TBODY></TABLE><!-- END PAPER BODY--></TD>
    <TD width=10><IMG alt="" border=0 height=1 
      src="Linux环境进程间通信(二):信号(下).files/c.gif" width=10></TD></TR></TBODY></TABLE><BR 
clear=all><IMG alt="" border=0 height=10 src="Linux环境进程间通信(二):信号(下).files/c.gif" 
width=100><BR>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
  <TBODY>
  <TR vAlign=top>
    <TD align=right width="100%"><A 
      href="http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part2/index2.shtml#top">到页首</A></TD>
    <TD width=5><IMG alt="" border=0 height=1 
      src="Linux环境进程间通信(二):信号(下).files/c.gif" width=5></TD></TR>
  <TR vAlign=top>
    <TD bgColor=#000000 colSpan=2><IMG alt="" border=0 height=1 
      src="Linux环境进程间通信(二):信号(下).files/c.gif" width=100></TD></TR>
  <TR vAlign=top>
    <TD bgColor=#ffffff colSpan=2><IMG alt="" border=0 height=8 
      src="Linux环境进程间通信(二):信号(下).files/c.gif" width=100></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=10 cellSpacing=0 width="100%">
  <TBODY>
  <TR vAlign=top>
    <TD>
      <FORM action=/developerWorks/cn/cnratings.nsf/RateArticle?CreateDocument 
      method=post name=getURL><INPUT name=ArticleTitle type=hidden 
      value="Linux 环境进程间通信(二):信号(下)"> <INPUT name=url type=hidden>
      <SCRIPT language=javascript>getURL();</SCRIPT>
       <INPUT name=Zone type=hidden value=linux> <INPUT name=RedirectURL 
      type=hidden value=/developerWorks/cn/thankyou/feedback-linux.html> <A 
      name=rating><B>您对这篇文章的看法如何?</B></A> 
      <TABLE border=0 cellPadding=0 cellSpacing=0 width=600>
        <TBODY>
        <TR>
          <TD colSpan=5><IMG alt="" border=0 height=8 
            src="Linux环境进程间通信(二):信号(下).files/c.gif" width=100></TD></TR>
        <TR vAlign=top>
          <TD width="16%"><INPUT name=Rating type=radio value=5>真棒!(5)</TD>
          <TD width="20%"><INPUT name=Rating type=radio value=4>好材料 (4)</TD>
          <TD width="24%"><INPUT name=Rating type=radio value=3>一般;尚可 (3)</TD>
          <TD width="22%"><INPUT name=Rating type=radio value=2>需提高 (2)</TD>
          <TD width="18%"><INPUT name=Rating type=radio value=1>太差! 
        (1)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA cols=60 name=Comments rows=5 wrap=virtual></TEXTAREA><BR><BR><INPUT type=submit value=提交反馈意见></FORM></TD></TR>
  <TR vAlign=top>
    <TD bgColor=#ffffff><IMG alt="" border=0 height=8 
      src="Linux环境进程间通信(二):信号(下).files/c.gif" width=100></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
  <TBODY>
  <TR>
    <TD align=right>(c) Copyright IBM Corp. 2001, (c) Copyright IBM China 
      2001, All Right Reserved</TD></TR>
  <TR vAlign=top>
    <TD class=bbg height=21>&nbsp;&nbsp;<A class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/index.shtml&amp;origin=dwhead">关于 
      IBM</A><SPAN class=divider>&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN><A 
      class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/privacy/index.shtml&amp;origin=dwhead">隐私条约</A><SPAN 
      class=divider>&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN><A class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/legal/index.shtml&amp;origin=dwhead">使用条款</A><SPAN 
      class=divider>&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN><A class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/contact/index.shtml&amp;origin=dwhead">联系 
      IBM</A></TD></TR></TBODY></TABLE>
<SCRIPT language=JavaScript1.2 src="Linux环境进程间通信(二):信号(下).files/stats.js" 
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG alt="" border=0 height=1 
src="F:\项目文档\进程间通信\Linux环境进程间通信(二):信号(下).files\c(1).gif" width=1></NOSCRIPT> 
</A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -