关于alarm函数的使用的问题 有代码 谢谢帮忙看看

zxyzsj 2003-09-29 10:07:36
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
main()
{
int b;
int pid;
void p();
for(b=10;b>=1;b--)
{
if ((pid=fork())==0)
{ alarm(10);
signal(SIGALRM,p);
pause();
exit(0);
}
}
exit(0);
}
void p()
{ signal(SIGALRM,p);
printf("this is ok\n");}
我创建了10个进程,在每一个里面设定一个定时器,然后10秒后等它超时发出SIGLRM信号 ,将其捕获.
问题是 该程序 编译没有问题 ,执行却出现一行莫名其妙的字符.不知道为什么?alarm的errno是多少啊?我man没有找到啊
...全文
64 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixiaoshui 2003-09-29
  • 打赏
  • 举报
回复
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
main()
{
int b,i;
int pid;
void p();
for(b=0;b<10;b++) {
if ((pid=fork())==0) {
alarm(10);
signal(SIGALRM,p);
pause();
exit(0);
}
}
for(i=0;i<10;i++)
wait();
exit(0);
}
void p()
{
printf("this is ok\n");
}

结果

this is ok
this is ok
this is ok
this is ok
this is ok
this is ok
this is ok
this is ok
this is ok
this is ok
zxyzsj 2003-09-29
  • 打赏
  • 举报
回复
程序运行后的结果是 5.0s 22.09 1% O+Ok O+Oio opf +OW
zxyzsj 2003-09-29
  • 打赏
  • 举报
回复
to wabc(wabc) 我用的是solaris 5.6 恩 我刚才已经在程序中加入wait语句 来防范僵尸进程 ,可是问题还是照旧。
to sunshinethinking(thinking)
在你的函数P的程序体中,你需要将SIGALRM信号屏蔽掉,而你是又调用一次
?????我想就算不屏蔽 ,也不会有什么影响啊。而且signal的第二个参数只能用常数SIG_IGN,SIG_DFL,或则要调用的函数地址把。
真心感谢两位的帮助,可是我还是有点不明白。
sunshinethinking 2003-09-29
  • 打赏
  • 举报
回复
程序都贴了,干嘛不把那一行莫名其妙的字符贴出来。

一:alarm 表示等待一段时间后向调用进程发送SIGALRM信号
二:signal(SIGALRM,(void *)p) 表示调用进程接受到SIGALRM信号后处理 <函数p>
三:在你的函数P的程序体中,你需要将SIGALRM信号屏蔽掉,而你是又调用一次
void p() {
signal(SIGALRM,p); <----错误在此
printf("this is ok\n");
}

修改如下:

void p() {
signal(SIGALRM,SIG_INT);
printf("this is ok\n");
}

wabc 2003-09-29
  • 打赏
  • 举报
回复
your program can run on Solaris 8 correctly.
but you have not used "wait" before the end of your parent process,so all child processes will be managed by the system process "init"

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧