当前位置: 技术问答>linux和unix
急~各位大侠帮帮忙!!!看下这个程序
来源: 互联网 发布时间:2016-02-16
本文导语: 各位大侠帮帮忙!!!看下这个程序 题目是:在父进程中接受中断信号然后用kill送到两个子进程中,子进程获得信息后分别输出 child process 1 is killed by parent! child process 2 is killed by parent! 然后终止。 我的问题...
各位大侠帮帮忙!!!看下这个程序
题目是:在父进程中接受中断信号然后用kill送到两个子进程中,子进程获得信息后分别输出
child process 1 is killed by parent!
child process 2 is killed by parent!
然后终止。
我的问题是在下面的程序中如果全部不要那个lockf();为什么不行?这个函数有什么用处?是怎么用的?
万分感谢!
#include
# include
# include
# include
#include
int wait_mark,status;
void waiting(),stop();
int main()
{ pid_t p1, p2;
signal(SIGINT,stop);
while((p1=fork())==-1);
if(p1>0)
{
while((p2=fork())==-1);
if(p2>0)
{
wait_mark=1;
waiting();
kill(p1,16);
kill(p2,17);
wait(&status);
wait(&status);
printf("parent process is killed!n");
exit(0);
}
else
{
wait_mark=1;
signal(17,stop);
waiting();
lockf(1,1,0);
printf("child process 2 is killed by parent!n");
lockf(1,0,0);
exit(0);
}
}
else
{
wait_mark=1;
signal(16,stop);
waiting();
lockf(1,1,0);
printf("child process 1 is killed by parent!n");
lockf(1,0,0);
exit(0);
}
return 0;
}
void waiting()
{
while(wait_mark!=0);
}
void stop()
{
wait_mark=0;
}
题目是:在父进程中接受中断信号然后用kill送到两个子进程中,子进程获得信息后分别输出
child process 1 is killed by parent!
child process 2 is killed by parent!
然后终止。
我的问题是在下面的程序中如果全部不要那个lockf();为什么不行?这个函数有什么用处?是怎么用的?
万分感谢!
#include
# include
# include
# include
#include
int wait_mark,status;
void waiting(),stop();
int main()
{ pid_t p1, p2;
signal(SIGINT,stop);
while((p1=fork())==-1);
if(p1>0)
{
while((p2=fork())==-1);
if(p2>0)
{
wait_mark=1;
waiting();
kill(p1,16);
kill(p2,17);
wait(&status);
wait(&status);
printf("parent process is killed!n");
exit(0);
}
else
{
wait_mark=1;
signal(17,stop);
waiting();
lockf(1,1,0);
printf("child process 2 is killed by parent!n");
lockf(1,0,0);
exit(0);
}
}
else
{
wait_mark=1;
signal(16,stop);
waiting();
lockf(1,1,0);
printf("child process 1 is killed by parent!n");
lockf(1,0,0);
exit(0);
}
return 0;
}
void waiting()
{
while(wait_mark!=0);
}
void stop()
{
wait_mark=0;
}
|
可以,在这里,lockf只是用来同步屏幕显示的,以避免两个进程同时往屏幕上输出而出现的混乱。