📄 linux环境进程间通信(二):信号(下).htm
字号:
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(&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></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 <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></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="/computer/UploadFiles/200506/200569181231831.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&name=NS-help&file=man">http://www.linuxjournal.com/modules.php?op=modload&name=NS-help&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 cellSpacing=0 cellPadding=0 width="100%" border=0>
<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">http://www-900.ibm.com/developerWorks/cn/linux/l-ipc/part2/mlinux@163.com</A>.
</TD></TR></TBODY></TABLE>
<P></P></TD></TR>
<TR class=left_tdbgall align=right>
<TD colSpan=2 height=24>【字体:<A class=top_UserLogin
href="javascript:fontZoomA();">小</A> <A class=top_UserLogin
href="javascript:fontZoomB();">大</A>】【<A
href="http://study.feloo.com/computer/Comment.asp?ArticleID=39161"
target=_blank>发表评论</A>】【<A
href="http://study.feloo.com/User/User_Favorite.asp?Action=Add&ChannelID=25&InfoID=39161"
target=_blank>加入收藏</A>】【<A
href="http://study.feloo.com/computer/SendMail.asp?ArticleID=39161"
target=_blank>告诉好友</A>】【<A
href="http://study.feloo.com/computer/Print.asp?ArticleID=39161"
target=_blank>打印此文</A>】【<A
href="javascript:window.close();">关闭窗口</A>】 </TD></TR>
<TR>
<TD class=left_tdbgall align=right colSpan=2
height=24>电脑文章录入:admin 责任编辑:admin
</TD></TR><!--文章内容下部广告代码开始-->
<TR>
<TD align=middle colSpan=2>
<SCRIPT src="Linux环境进程间通信(二):信号(下).files/wen-bot.js"></SCRIPT>
</TD></TR><!--文章同容下部广告代码结束-->
<TR>
<TD class=left_tdbgall align=right colSpan=2 height=24><STRONG><FONT
color=#ff0000>百度搜索:<A
href="http://www.baidu.com/baidu?tn=study888&word=Linux环境进程间通信(二):信号(下)"
target=_blank>Linux环境进程间通信(二):信号(下)</FONT></A></STRONG>
<SCRIPT src=""></SCRIPT>
</TD></TR>
<TR>
<TD width=5></TD>
<TD width=787>
<LI>上一篇电脑文章: <A
title="电脑文章标题:Linux环境进程间通信(二):信号(上) 作 者:未知 更新时间:2005-6-9 18:12:28"
href="http://study.feloo.com/computer/pro/vc/system/200506/39160.html">Linux环境进程间通信(二):信号(上)</A><BR>
<LI>下一篇电脑文章: <A
title="电脑文章标题:Linux环境进程间通信(三):消息队列 作 者:未知 更新时间:2005-6-9 18:12:33"
href="http://study.feloo.com/computer/pro/vc/system/200506/39162.html">Linux环境进程间通信(三):消息队列</A></LI></TD></TR><!--文章内容下部广告代码开始-->
<TR>
<TD align=middle colSpan=2>
<SCRIPT src="Linux环境进程间通信(二):信号(下).files/wen-bot-2.js"></SCRIPT>
</TD></TR><!--文章同容下部广告代码结束--></TBODY></TABLE><!--网页中部左栏文章内容代码结束-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=middle height=20></TD></TR></TBODY></TABLE></TD>
<TD style="BORDER-LEFT: #a7a9a7 1px solid" vAlign=top width=180
bgColor=#f8f8f8><!--网页中部右栏代码开始-->
<TABLE style="WORD-BREAK: break-all" cellSpacing=0 cellPadding=0
width="100%" border=0>
<TBODY>
<TR>
<TD class=left_title><IMG height=28
src="Linux环境进程间通信(二):信号(下).files/ad_search.gif" width=174></TD></TR>
<TR>
<TD align=middle>
<SCRIPT
src="Linux环境进程间通信(二):信号(下).files/ShowSearchForm_wen.js"></SCRIPT>
</TD></TR>
<TR>
<TD align=middle>
<SCRIPT src="Linux环境进程间通信(二):信号(下).files/right-1.js"></SCRIPT>
</TD></TR>
<TR>
<TD class=left_title><IMG height=28
src="Linux环境进程间通信(二):信号(下).files/ad_ztlm.gif" width=174></TD></TR>
<TR>
<TD class=left_tdbg1>
<SCRIPT language=javascript
src="Linux环境进程间通信(二):信号(下).files/com-tjlm.js"></SCRIPT>
</TD></TR>
<TR>
<TD class=left_tdbg2></TD></TR>
<TR>
<TD class=left_title><A class=class
href="http://study.feloo.com/computer/ShowHot.asp"><IMG height=28
src="Linux环境进程间通信(二):信号(下).files/ad_zxrmwz.gif" width=174
border=0></A></TD></TR>
<TR>
<TD class=left_tdbg1
background=Linux环境进程间通信(二):信号(下).files/ad_bg2.gif>
<TABLE cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD class=listbg vAlign=top width=10><IMG alt=普通电脑文章
src="Linux环境进程间通信(二):信号(下).files/article_common.gif"></TD>
<TD class=listbg><A class=listA
title="电脑文章标题:VC实现校正电脑时间 作 者:未知 更新时间:2005-6-9 18:12:58"
href="http://study.feloo.com/computer/pro/vc/system/200506/39169.html"
target=_self>VC实现校正电脑时间</A></TD></TR>
<TR>
<TD class=listbg2 vAlign=top width=10><IMG alt=普通电脑文章
src="Linux环境进程间通信(二):信号(下).files/article_common.gif"></TD>
<TD class=listbg2><A class=listA
title="电脑文章标题:VC++中进程与多进程管理的方法 作 者:未知 更新时间:2005-6-9 18:12:58"
href="http://study.feloo.com/computer/pro/vc/system/200506/39168.html"
target=_self>VC++中进程与多进程管理的…</A></TD></TR>
<TR>
<TD class=listbg vAlign=top width=10><IMG alt=普通电脑文章
src="Linux环境进程间通信(二):信号(下).files/article_common.gif"></TD>
<TD class=listbg><A class=listA
title="电脑文章标题:多进程编程的相关知识总结(三) 作 者:未知 更新时间:2005-6-9 18:12:57"
href="http://study.feloo.com/computer/pro/vc/system/200506/39167.html"
target=_self>多进程编程的相关知识总结…</A></TD></TR>
<TR>
<TD class=listbg2 vAlign=top width=10><IMG alt=普通电脑文章
src="Linux环境进程间通信(二):信号(下).files/article_common.gif"></TD>
<TD class=listbg2><A class=listA
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -