#include "public.h"
void myaction(int sig,siginfo_t *info,void *reverse)
{
int i=0;
while (i<5)
{
/*printf("i = %d sig=%d\n",i,sig);
printf("recive value=%d\n",info->si_value.sival_int);
printf("info->si_int=%d\n",info->si_int);
printf("info->si_pid=%d\n",info->si_pid);
*/
printf("myaction %d\n",i);
sleep(1);
i++;
}
}
void myaction1(int sig,siginfo_t *info,void *reverse)
{
for (int i=0;i<5;i++)
{
printf("myaction1 = %d\n",i);
sleep(1);
}
}
void myaction2(int sig,siginfo_t *info,void *reverse)
{
for (int i=0;i<5;i++)
{
printf("myaction2 = %d\n",i);
sleep(1);
}
}
int main()
{
int num=0;
pid_t pid;
//在这里写信号
union sigval sival_data;
struct sigaction action;
action.sa_sigaction=myaction;
action.sa_flags=SA_SIGINFO|SA_RESTART;
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask,SIGUSR2);
sigaddset(&action.sa_mask,SIGINT);
sigaction(SIGUSR1,&action,NULL);
action.sa_sigaction=myaction1;
action.sa_flags=SA_SIGINFO|SA_RESTART;
sigaction(SIGUSR2,&action,NULL);
action.sa_sigaction=myaction2;
action.sa_flags=SA_SIGINFO|SA_RESTART;
sigaction(SIGINT,&action,NULL);
sigqueue(getpid(),SIGUSR1,sival_data);
pid=fork();
if (pid==-1)
{
perror("fork fail\n");
exit(1);
}
if (pid>0)
{
//父进程负责创建子进程,新建的进程会从上一个逻辑层开始
if (num<2)
{
pid=fork();
num++;
}
}
else if (pid ==0)
{
if (num==0)
{
sival_data.sival_int=num;
sigqueue(getpid(),SIGUSR2,sival_data);
}
if (num==1)
{
sival_data.sival_int=num;
sigqueue(getpid(),SIGINT,sival_data);
}
}
return 0;
}