Chinaunix首页 | 论坛 | 博客
  • 博客访问: 63957
  • 博文数量: 12
  • 博客积分: 230
  • 博客等级: 入伍新兵
  • 技术积分: 210
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-22 13:20
文章分类

全部博文(12)

文章存档

2012年(12)

我的朋友

分类: LINUX

2012-04-11 19:11:16


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <sys/types.h>

  6. int p1;

  7. void go_func()
  8. {
  9.     kill(getppid(),SIGUSR1);
  10. }

  11. void stop_func()
  12. {
  13.     kill(getppid(),SIGUSR2);
  14. }

  15. void get_gofunc()
  16. {
  17.     printf("go go go! ... \n");
  18. }
  19. void get_stopfunc()
  20. {
  21.     printf("stop the bus...\n");
  22. }

  23. void alloff_func()
  24. {
  25.     kill(p1, SIGUSR1);
  26.     wait(NULL);
  27.     exit(0);
  28. }

  29. void exit_func()
  30. {
  31.     printf("Please get off all,it's the final station!\n");
  32.     exit(0);
  33. }

  34. int main()
  35. {
  36.     //int p1;

  37.     while((p1 = fork()) == -1);

  38.     if(p1 > 0)
  39.     {
  40.         signal(SIGINT, SIG_IGN);
  41.         signal(SIGQUIT, SIG_IGN);
  42.         signal(SIGTSTP, alloff_func);
  43.         signal(SIGUSR1,get_gofunc);
  44.         signal(SIGUSR2,get_stopfunc);
  45.         while(1)
  46.             pause();
  47.         wait(NULL);            
  48.         exit(0);    
  49.         
  50.     }
  51.     else if(p1 == 0)
  52.     {
  53.         signal(SIGTSTP, SIG_IGN);
  54.         signal(SIGUSR2, SIG_IGN);
  55.         signal(SIGUSR1,exit_func);
  56.         signal(SIGINT,go_func);
  57.         signal(SIGQUIT,stop_func);
  58.         while(1)
  59.             pause();
  60.     
  61.     }

  62.     return 0;
  63. }

阅读(1253) | 评论(3) | 转发(0) |
0

上一篇:图的存储

下一篇:Linux的启动流程详解

给主人留下些什么吧!~~

ldjunnbor2012-04-14 14:54:58

BUS是公交不是总线哟!!

ldjunnbor2012-04-14 14:48:02

小布丁姐姐: 第一个循环没看懂啊~~有木有代码的注释?.....
创建了一个子进程,模仿的是2个人!
1:客户让司机走、停;
2:司机要处理客户请求,同时还要发信号给客户全下去!

2边互相传递信号,有各自的信号处理函数!

小布丁姐姐2012-04-12 10:28:11

第一个循环没看懂啊~~有木有代码的注释?