当前位置: 技术问答>linux和unix
关于信号的程序
来源: 互联网 发布时间:2015-12-11
本文导语: 总是出错,请大虾指点: include #include #include #include #include volatile sig_atomic_t child_quit=0; time_t now; struct tm *timenow; void parent_handler(int signum) { if (signum==SIGUSR1) { time(&...
总是出错,请大虾指点:
include
#include
#include
#include
#include
volatile sig_atomic_t child_quit=0;
time_t now;
struct tm *timenow;
void parent_handler(int signum)
{
if (signum==SIGUSR1)
{ time(&now);
timenow=localtime(&now);
printf("Now, the time is %sn",asctime(timenow));
}
}
void parent_handler1(int signum)
{ if (signum==SIGUSR2)
{ child_quit=1;
printf("I have rece the sigusr2");
}
}
int main()
{
pid_t p_id,ppid;
int i;
struct sigaction user_action,user_action1;
sigset_t blockmask;
sigfillset(&blockmask);
user_action.sa_handler=parent_handler;
user_action1.sa_handler=parent_handler1;
user_action.sa_mask=blockmask;
user_action1.sa_mask=blockmask;
ppid=getpid();
printf("the current process id is %dn",ppid);
p_id =fork();
if (p_id>0 )//parent process
{
printf("I am the parent process ");
sigaction(SIGUSR1,&user_action,NULL);
sigaction(SIGUSR2,&user_action1,NULL);
for(;;)
{ if (child_quit!=1)
{
sleep(1); //or use sigsuspend
}
else return ;
}
}
else if (p_id==0) //child porcess
{
printf("I am the child process ");
for (i=0;i ./sigtest
the current process id is 3056.
I am the parent process.
I am the child process.
the SIGUSR1 is send successfully.
Now, the time is Mon Aug 21 23:06:12 2006
the SIGUSR1 is send successfully.
User signal 1
sms@test1> the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
include
#include
#include
#include
#include
volatile sig_atomic_t child_quit=0;
time_t now;
struct tm *timenow;
void parent_handler(int signum)
{
if (signum==SIGUSR1)
{ time(&now);
timenow=localtime(&now);
printf("Now, the time is %sn",asctime(timenow));
}
}
void parent_handler1(int signum)
{ if (signum==SIGUSR2)
{ child_quit=1;
printf("I have rece the sigusr2");
}
}
int main()
{
pid_t p_id,ppid;
int i;
struct sigaction user_action,user_action1;
sigset_t blockmask;
sigfillset(&blockmask);
user_action.sa_handler=parent_handler;
user_action1.sa_handler=parent_handler1;
user_action.sa_mask=blockmask;
user_action1.sa_mask=blockmask;
ppid=getpid();
printf("the current process id is %dn",ppid);
p_id =fork();
if (p_id>0 )//parent process
{
printf("I am the parent process ");
sigaction(SIGUSR1,&user_action,NULL);
sigaction(SIGUSR2,&user_action1,NULL);
for(;;)
{ if (child_quit!=1)
{
sleep(1); //or use sigsuspend
}
else return ;
}
}
else if (p_id==0) //child porcess
{
printf("I am the child process ");
for (i=0;i ./sigtest
the current process id is 3056.
I am the parent process.
I am the child process.
the SIGUSR1 is send successfully.
Now, the time is Mon Aug 21 23:06:12 2006
the SIGUSR1 is send successfully.
User signal 1
sms@test1> the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
the SIGUSR1 is send successfully.
|
你是在主进程里执行:
sigaction(SIGUSR1,&user_action,NULL);
sigaction(SIGUSR2,&user_action1,NULL);
如果子进程先运行的话, 发送SIGUSR1信号,这时主进程默认是忽略此信号的.
sigaction(SIGUSR1,&user_action,NULL);
sigaction(SIGUSR2,&user_action1,NULL);
如果子进程先运行的话, 发送SIGUSR1信号,这时主进程默认是忽略此信号的.
|
在信号发生跳转到自定的handler处理函数执行后,系统会自动将此处理函数换回原来系统预设的处理方式.