Chinaunix首页 | 论坛 | 博客
  • 博客访问: 98431
  • 博文数量: 24
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 171
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-24 12:12
个人简介

好好学习天天向上

文章分类

全部博文(24)

文章存档

2018年(1)

2016年(7)

2015年(6)

2014年(10)

我的朋友

分类: LINUX

2014-10-24 15:58:36

    将下面的程序保存到fork.c里面。从运行的结果看,父子进程中的局部变量或者全局变量都没有被彼此影响。

点击(此处)折叠或打开

  1. ?#include<stdio.h>
  2. #include<signal.h>
  3. #include <stdlib.h>
  4. //#include <unistd.h>
  5. //变量在不同进程中是相互独立的
  6. int wait_mark;
  7. int g_temp = 21;
  8. void waiting()
  9. {
  10.     while (wait_mark == 1);
  11. }

  12. void stop()
  13. {
  14.     wait_mark = 0;
  15. }

  16. int main()
  17. {
  18.     int p1, p2;
  19.     int temp = 3;
  20.     while ((p1 = fork()) == -1);
  21. if (p1 == 0) //第一个子进程
  22. {
  23.         wait_mark = 1;
  24.         printf("Child Process11 temp1 = %d \n",temp);
  25.         printf("Child Process11 g_temp1 = %d \n",g_temp);
  26.         signal(SIGINT, SIG_IGN);//忽略来自键盘的中断信号 ( ctrl + c )
  27.         signal(16, stop);
  28.         waiting();
  29.         lockf(1, 1, 0); //加锁
  30.         printf("Child Process11 is Killed by Parent!\n");
  31.         printf("Child Process11 temp2 = %d \n",temp);
  32.         temp = 6;
  33.         printf("Child Process11 g_temp2 = %d \n",g_temp);
  34.         g_temp = 23;
  35.         lockf(1, 0, 0); //解锁
  36.         exit(0);
  37. }
  38. else
  39. {
  40.         while ((p2 = fork()) == -1);
  41.         if (p2 == 0) //第二个子进程
  42. {
  43.             wait_mark = 1;
  44.             printf("Child Process12 temp1 = %d \n",temp);
  45.             printf("Child Process12 g_temp1 = %d \n",g_temp);
  46.             signal(SIGINT, SIG_IGN);//忽略来自键盘的中断信号 ( ctrl + c )
  47.             signal(17, stop);
  48.             waiting();
  49.             lockf(1, 1, 0); //加锁
  50.             printf("Child Process12 is Killed by Parent!\n");
  51.             printf("Child Process12 temp2 = %d \n",temp);
  52.             temp = 5;
  53.             printf("Child Process12 g_temp2 = %d \n",g_temp);
  54.             g_temp = 24;
  55.             lockf(1, 0, 0);//解锁
  56.             exit(0);
  57.         }
  58. else//父进程
  59. {
  60.             wait_mark = 1;
  61.             temp = 4;
  62.             g_temp = 22;
  63.             printf("Parent temp1 = %d \n",temp);
  64.             printf("Parent g_temp1 = %d \n",g_temp);
  65.             signal(SIGINT, stop);//注册来自键盘的中断信号 ( ctrl + c ) ,中断后跳到stop函数中运行
  66.             waiting();
  67.             kill(p1, 16);
  68.             kill(p2, 17);
  69.             wait(0);
  70.             wait(0);
  71.             printf("Parent Process is Killed!\n");
  72.             printf("Parent temp2 = %d \n",temp);
  73.             printf("Parent g_temp2 = %d \n",g_temp);
  74.             exit(0);
  75.         }
  76.     }
  77. }
在当前目录下
[chenyun fork]$ gcc fork.c -o fork.o
结果打印如下:
[chenyun fork]$ ./fork.o
Child Process11 temp1 = 3
Child Process11 g_temp1 = 21
Parent temp1 = 4
Parent g_temp1 = 22
Child Process12 temp1 = 3
Child Process12 g_temp1 = 21

^CChild Process11 is Killed by Parent!
Child Process11 temp2 = 3
Child Process11 g_temp2 = 21
Child Process12 is Killed by Parent!
Child Process12 temp2 = 3
Child Process12 g_temp2 = 21
Parent Process is Killed!
Parent temp2 = 4
Parent g_temp2 = 22
[chenyun fork]$
注:程序运行中间按ctrl+C会中断父进程,父进程会发kill 16消息给子进程1,发kill 17消息给子进程2。

阅读(1051) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~