Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1438350
  • 博文数量: 244
  • 博客积分: 3353
  • 博客等级: 中校
  • 技术积分: 3270
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-09 17:56
文章分类

全部博文(244)

文章存档

2023年(7)

2022年(7)

2021年(4)

2020年(1)

2019年(2)

2017年(2)

2016年(3)

2015年(11)

2014年(20)

2013年(10)

2012年(176)

分类:

2012-08-12 13:08:25

一个关于fork的思考

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<unistd.h>
  4. int i=5;

  5. int main()
  6. {
  7.     int j;
  8.     pid_t pid;
  9.     //pid = fork();//子进程是从fork后的语句开始执行的,所以这样起不到创建两个子进程的效果
  10. for(j=0; j<2; j++)
  11. {
  12.     //pid = fork();//请思考为什么fork不能放在循环里
  13.     pid = vfork();//请比较此例中创建两个子进程和下例中用递归创建有什么不同
  14.     switch(pid)
  15.     {
  16.         case 0:
  17.             i++;
  18.             printf("CP is run\n");
  19.             printf("%d\n%d\n",i,getpid());
  20.             break;
  21.         case -1:
  22.             perror("pc failed");
  23.             break;
  24.         default:
  25.             printf("PP is run\n");
  26.             printf("%d\n%d\n",i,getpid());
  27.             break;
  28.     }

  29. }
  30. exit(0);
  31. }
递归,可创建多个子进程

点击(此处)折叠或打开

  1. #include<stdio.h>
  2.     #include<stdlib.h>
  3.     #include<unistd.h>
  4.       
  5.     pid_t pid;
  6.       
  7.     /*
  8.      * num:当前已经创建的子进程数
  9.      * max:需要创建的子进程数
  10.      */
  11.     void createsubprocess(int num,int max)
  12.     {
  13.         if(num>=max)return;
  14.         pid=fork();
  15.         if(pid<0)
  16.         {
  17.             perror("fork error!\n");
  18.             exit(1);
  19.         }
  20.         //子进程
  21.         else if(pid==0)
  22.         {
  23.             sleep(3);
  24.             printf("子进程id=%d,父进程id=%d\n",getpid(),getppid());
  25.         }
  26.         //父进程
  27.         else
  28.         {
  29.             num++;
  30.             if(num==1)printf("父进程id=%d\n",getpid());
  31.             if(num<max)createsubprocess(num,max);
  32.             //此处加sleep是为了防止父进程先退出,从而产生异常
  33.             sleep(5);
  34.         }
  35.     }
  36.       
  37.     int main()
  38.     {
  39.         int num=0;
  40.         int max=3;
  41.         createsubprocess(num,max);
  42.         return 0;
  43.     }

创建守护进程(递归)

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<unistd.h>
  4. #include<signal.h>
  5. #include<sys/param.h>
  6. #include<sys/stat.h>
  7. #include<time.h>
  8. #include<syslog.h>

  9. int init_daemon(void)
  10. {
  11.     int pid;
  12.     int i;

  13.     signal(SIGTTOU,SIG_IGN);
  14.     signal(SIGTTIN,SIG_IGN);
  15.     signal(SIGTSTP,SIG_IGN);
  16.     signal(SIGHUP,SIG_IGN);
  17. //两次创建子进程,使得进程不再是会话组长从而脱离控制终端
  18.     pid = fork();
  19.     if(pid>0)
  20.     {
  21.     exit(0);
  22.     }
  23.     else if(pid<0)
  24.     {
  25.         return -1;
  26.     }
  27.     setsid();
  28.     pid = fork;
  29.     if(pid>0)
  30.     {
  31.         exit(0);
  32.     }
  33.     else if(pid<0)
  34.     {
  35.         return -1;
  36.     }
  37. //关闭0到最高文件进程描述符值
  38.     for(i=0; i<NOFILE; close(i++));

  39.     chdir("/");
  40.     
  41.     umask(0);

  42.     signal(SIGCHLD,SIG_IGN);

  43.     return 0;

  44. }

  45. int main()
  46. {
  47.     time_t now;
  48.     init_daemon();
  49.     syslog(LOG_USER | LOG_INFO,"测试守护进程!\n");
  50.     while(1)
  51.     {
  52.         sleep(8);
  53.         time(&now);
  54.         syslog(LOG_USER | LOG_INFO,"系统时间:\t%s\t\t\n",ctime(&now));
  55.     }
  56. }

创建进程扇/进程链:

进程扇:/* 由一个进程派生多个子进程 */
进程链:/* 由父进程派生子进程,子进程再派生子进程 */

请思考3个问题:
1.怎样保证父子进程的执行顺序
2.sleep的作用到底是什么?
3.break能不能换成return(0)?它们之间有什么区别?

点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>


  4. int main(void)
  5. {
  6.         int i;
  7.         pid_t pid;

  8.         printf("This is a example\n");
  9.         for (i=0 ;i<3; i++)
  10.         {
  11.                  if (!(pid = fork())  //创建进程扇(让子进程跳出)
  12.              //  if ( pid = fork())  //创建进程链(让父进程跳出,子进程作为后继的父进程)
  13.                  break;
  14.         }
  15.         printf ("("My parent pid is %ld,My pid is %ld\n",getpid()getppid())
  16.         sleep(3);//等待所有的进程执行完毕
  17.         return 0;
  18. }


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