linux环境进程间通信(二) 信号(下).htm
来自「关于Linux内核进程间通信的几篇文章」· HTM 代码 · 共 848 行 · 第 1/4 页
HTM
848 行
<TR>
<TD class=code-outline><PRE class=displaycode>#include "signal.h"
#include "unistd.h"
static void my_op(int);
main()
{
sigset_t new_mask,old_mask,pending_mask;
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags=SA_SIGINFO;
act.sa_sigaction=(void*)my_op;
if(sigaction(SIGRTMIN+10,&act,NULL))
printf("install signal SIGRTMIN+10 error\n");
sigemptyset(&new_mask);
sigaddset(&new_mask,SIGRTMIN+10);
if(sigprocmask(SIG_BLOCK, &new_mask,&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(&pending_mask)<0)
printf("get pending mask error\n");
if(sigismember(&pending_mask,SIGRTMIN+10))
printf("signal SIGRTMIN+10 is pending\n");
if(sigprocmask(SIG_SETMASK,&old_mask,NULL)<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></TD></TR></TBODY></TABLE><BR>
<P>编译该程序,并以后台方式运行。在另一终端向该进程发送信号(运行kill -s 42
pid,SIGRTMIN+10为42),查看结果可以看出几个关键函数的运行机制,信号集相关操作比较简单。</P>
<P><B>注:</B>在上面几个实例中,使用了printf()函数,只是作为诊断工具,pringf()函数是不可重入的,不应在信号处理函数中使用。
</P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt=""
src="Linux环境进程间通信(二) 信号(下).files/blue_rule.gif"
width="100%"><BR><IMG height=6 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width=8
border=0></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD><IMG height=4 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width="100%"><BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt=""
src="Linux环境进程间通信(二) 信号(下).files/u_bold.gif" width=16
border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index2.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=4><SPAN class=atitle>结束语:</SPAN></A></P>
<P>系统地对linux信号机制进行分析、总结使我受益匪浅!感谢王小乐等网友的支持! <BR>Comments and
suggestions are greatly welcome! </P><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt=""
src="Linux环境进程间通信(二) 信号(下).files/blue_rule.gif"
width="100%"><BR><IMG height=6 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width=8
border=0></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD><IMG height=4 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width="100%"><BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt=""
src="Linux环境进程间通信(二) 信号(下).files/u_bold.gif" width=16
border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index2.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=5><SPAN class=atitle>附录1:</SPAN></A></P>
<P>用sigqueue实现的命令行信号发送程序sigqueuesend,命令行第二个参数是发送的信号值,第三个参数是接收该信号的进程ID,可以配合实例一使用:</P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
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></TD></TR></TBODY></TABLE><BR><BR><BR>
<P><A name=resources><SPAN class=atitle>参考资料 </SPAN></A></P>
<UL>
<LI>linux内核源代码情景分析(上),毛德操、胡希明著,浙江大学出版社,当要验证某个结论、想法时,最好的参考资料;<BR><BR>
<LI>UNIX环境高级编程,作者:W.Richard
Stevens,译者:尤晋元等,机械工业出版社。对信号机制的发展过程阐述的比较详细。<BR><BR>
<LI>signal、sigaction、kill等手册,最直接而可靠的参考资料。<BR><BR>
<LI><A
href="http://www.linuxjournal.com/modules.php?op=modload&name=NS-help&file=man">http://www.linuxjournal.com/modules.php?op=modload&name=NS-help&file=man</A>提供了许多系统调用、库函数等的在线指南。
<BR><BR>
<LI><A
href="http://www.opengroup.org/onlinepubs/007904975/">http://www.opengroup.org/onlinepubs/007904975/</A>可以在这里对许多关键函数(包括系统调用)进行查询,非常好的一个网址。
<BR><BR>
<LI><A
href="http://unix.org/whitepapers/reentrant.html">http://unix.org/whitepapers/reentrant.html</A>对函数可重入进行了阐述。
<BR><BR>
<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>对实时信号给出了相当好的描述。
<BR></LI></UL><BR><BR>
<P><A name=author><SPAN class=atitle>关于作者</SPAN></A></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD colSpan=3><IMG height=5 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width="100%"></TD></TR>
<TR vAlign=top align=left>
<TD>
<P></P></TD>
<TD><IMG height=5 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width=4></TD>
<TD width="100%">
<P>郑彦兴,国防科大攻读博士学位。联系方式: <A
href="mailto:mlinux@163.com?cc=">mailto:mlinux@163.com?cc=</A>.
</P></TD></TR></TBODY></TABLE><BR><BR><BR>
<P class=no-print><SPAN class=atitle><A
name=rate>对本文的评价</A></SPAN></P><SPAN class=no-print>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD>
<FORM
action=https://www.ibm.com/developerworks/secure/cnratings.jsp
method=get><INPUT type=hidden value="Linux环境进程间通信(二): 信号(下)"
name=ArticleTitle><INPUT type=hidden value=Linux
name=Zone><INPUT type=hidden
value=http://www.ibm.com/developerworks/cn/thankyou/
name=RedirectURL><INPUT type=hidden value=china
name=localsite>
<SCRIPT language=javascript>document.write('<input type="hidden" name="url" value="'+location.href+'" />');</SCRIPT>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD><IMG height=8 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width=100
border=0></TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=1 name=Rating>太差! (1)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=2 name=Rating>需提高 (2)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=3 name=Rating>一般;尚可
(3)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=4 name=Rating>好文章 (4)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=5
name=Rating>真棒!(5)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA id=Comments name=Comments rows=5 wrap=virtual cols=60> </TEXTAREA><BR><BR><INPUT type=submit value=反馈意见></FORM></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff><IMG height=8 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width=100
border=0></TD></TR></TBODY></TABLE></SPAN><SPAN class=no-print>
<TABLE cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD><IMG height=8 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif" width="100%"><BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt=""
src="Linux环境进程间通信(二) 信号(下).files/u_bold.gif" width=16
border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox
href="http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index2.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR></SPAN></TD>
<TD width=10><IMG height=1 alt=""
src="Linux环境进程间通信(二) 信号(下).files/c.gif"
width=10></TD></TR></TBODY></TABLE><SPAN class=small>IBM 公司保留在
developerWorks 网站上发表的内容的著作权。未经IBM公司或原始作者的书面明确许可,请勿转载。如果您希望转载,请通过 <A
href="https://www.ibm.com/developerworks/secure/reprintreq.jsp?domain=dwchina">提交转载请求表单</A>
联系我们的编辑团队。</SPAN></TD></TR></TBODY></TABLE><!--FOOTER_BEGIN--><!-- IBM FOOTER -->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=bbg height=19>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD><SPAN class=spacer> </SPAN><A
class=mainlink href="http://www.ibm.com/cn/ibm/index.shtml">关于
IBM</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink
href="http://www.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink href="http://www.ibm.com/contact/cn/">联系
IBM</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- end footer -->
<SCRIPT language=JavaScript1.2 src="Linux环境进程间通信(二) 信号(下).files/stats.js"
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt=""
src="E:\sun_job\notebook\linux_进程间通信\Linux环境进程间通信(二) 信号(下).files\c(1).gif"
width=1 border=0></NOSCRIPT><!--FOOTER_END--><!--XSLT stylesheet used to transform this file: dw-document-html-5.8.xsl--></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?