Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7553690
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: LINUX

2011-06-13 20:50:39

  1. /*
  2.  *    使用fork创建子进程
  3.  *    Lzy    2011-6-13
  4.  */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>

  8. int main(void)
  9. {
  10.     pid_t pid;                //定义进程描述符
  11.     
  12.     pid = fork();            //创建子进程
  13.     if(pid < 0)                //创建子进程失败
  14.     {
  15.         perror("fork");
  16.         exit(0);
  17.     }
  18.     else if(pid == 0)            /* 子进程 */
  19.         printf("This is the child process \nID:%d, PID:%d\n",getpid(), getppid());
  20.     else                        /* 父进程 */
  21.         printf("This is the parent process \nID:%d, child ID:%d\n",getpid(), pid);
  22.     
  23.     return 0;
  24. }
  1. /*
  2.  *    使用fork创建子进程
  3.  *    wait函数阻塞父进程直至子进程退出
  4.  *    Lzy    2011-6-13
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/wait.h>

  10. int main(void)
  11. {
  12.     pid_t pid;                //定义进程描述符
  13.     
  14.     pid = fork();            //创建子进程
  15.     if(pid < 0)                //创建子进程失败
  16.     {
  17.         perror("fork");
  18.         exit(0);
  19.     }
  20.     else if(pid == 0)                /* 子进程 */
  21.     {
  22.         sleep(3);
  23.         printf("This is the child process \nID %d, PID %d\n",getpid(), getppid());
  24.     }
  25.     else                            /* 父进程 */
  26.     {
  27.         //wait(NULL);                    /* 父进程阻塞直至有一个子进程退出 */
  28.         waitpid(pid, NULL, 0);            /* 等待指定的子进程退出 */
  29.         printf("This is the parent process \nID %d, child ID %d\n",getpid(), pid);
  30.     }
  31.     
  32.     return 0;
  33. }
  1. /*
  2.  *    使用fork创建子进程
  3.  *    wait函数阻塞父进程直至子进程退出
  4.  *    Linux系统保用写时拷贝(copy-on-write)技术
  5.  *    Lzy    2011-6-13
  6.  */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sys/wait.h>

  11. void process_child(int x)
  12. {
  13.     printf("child x = %d\n",x);
  14.     x++;
  15.     printf("child x = %d\n",x);
  16. }

  17. int main(void)
  18. {
  19.     pid_t pid;                //定义进程描述符
  20.     int x = 0;
  21.     
  22.     pid = fork();            //创建子进程
  23.     if(pid < 0)                //创建子进程失败
  24.     {
  25.         perror("fork");
  26.         exit(0);
  27.     }
  28.     else if(pid == 0)                /* 子进程 */
  29.     {
  30.         sleep(3);
  31.         printf("This is the child process \nID %d, PID %d\n",getpid(), getppid());
  32.         process_child(x);
  33.     }
  34.     else                            /* 父进程 */
  35.     {
  36.         //wait(NULL);                    /* 父进程阻塞直至有一个子进程退出 */
  37.         waitpid(pid, NULL, 0);            /* 等待指定的子进程退出 */
  38.         printf("This is the parent process \nID %d, child ID %d\nx = %d\n",getpid(), pid,x);
  39.     }
  40.     
  41.     return 0;
  42. }
阅读(1953) | 评论(0) | 转发(2) |
0

上一篇:atexit函数

下一篇:execl函数

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