【实验目的】
学习 signal 的函数的使用
【实验原理】
利用 signal 的函数的机制来实习我们发送截获信号的功能
【实验内容】
修改上面的程序,增加语句 signal(SIGINT,SIG_IGN)和语句 signal(SIGQUIT,SIG_IGN),再观察程序执
行时屏幕上出现的现象,并分析其原因。
<参考程序>
# include<stdio.h>
# include<signal.h>
# include<unistd.h>
int pid1, pid2;
int EndFlag = 0;
pf1 = 0;
pf2 = 0;
void IntDelete()
{
kill(pid1, 10);
kill(pid2, 12);
EndFlag=1;
}
void Int1()
{
printf(“child process 1 is killed by parent !\n”);
exit(0);
}
void Int2()
{
printf(“child process 2 is killed by parent !\n”);
exit(0);
}
main()
{
int exitcode;
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
while ((pid1 = fork()) == -1);
if (pid1 == 0)
{
signal(SIGUSR1, Int1);
//signal(SIGINT, SIG_IGN);
pause();
exit(0);
}
else
{
while ((pid2 = fork()) = = -1);
if (pid2 == 0)
{
signal(SIGUSR2, Int2);
//signal(SIGINT, SIG_IGN);
pause();
exit(0);
}
else
{
signal(SIGINT, IntDelete);
waitpid(-1, &exitcode, 0); /*等待任何子进程中断或结束*/
printf(“parent process exit\n”);
exit(0);
}
}
}
【 实验要求】 】
运行程序并分析结果。
司机售票员问题(选做题)
创建子进程代表售票员,父进程代表司机 ,同步过程如下:
售票员捕捉 SIGINT(代表开车),发 SIGUSR1 给司机,司机打印(“let’s gogogo”)
售票员捕捉 SIGQUIT(代表停车),发 SIGUSR2 给司机,司机打印(“stop the bus”)
司机捕捉 SIGTSTP(代表车到总站),发 SIGUSR1 给售票员,售票员打印(“please get off
the bus”)