码迷,mamicode.com
首页 > 其他好文 > 详细

fork系统调用

时间:2015-12-24 23:34:03      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

fork()学习, 理解

例1: (独立的上下文)

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #define ERROR(flag)                 \
  8.      if(flag)                \
  9.     {                    \
  10.         printf("%d: ",__LINE__);    \
  11.         fflush(stdout);            \
  12.         perror("error");        \
  13.         exit(errno);            \
  14.     }
  15. int num = 999;
  16. int main(int argc,char *argv[])
  17. {
  18.     pid_t pid; 
  19.     char *s = NULL;
  20.     printf("hello, my pid is %d\n\n",getpid());
  21.     pid = fork();
  22.     ERROR(pid == -1);
  23.     if(pid == 0)
  24.     {
  25.         int i = 2;
  26.         while(i--)
  27.         {
  28.             printf("child process, pid = %d, ppid = %d\n\n",
  29.             getpid(),getppid());
  30.             s = "child process"; 
  31.             sleep(1);
  32.         }
  33.     }
  34.     else
  35.     {
  36.         int i = 2;
  37.         while(i--)
  38.         {
  39. //          getchar();
  40.             printf("parent process, pid = %d, child pid = %d\n\n",
  41.             getpid(),pid);
  42.             s = "parent process";
  43.             sleep(1);
  44.         }
  45.     }
  46.     printf("i‘m %s, byebye!!!\n",s);
  47.     return 0;
  48. }

编译链接成功后运行, 父进程输出当前及子进程号, 子进程输出当前及父进程号. 结果如下图:
技术分享

例2: (独立的文件读写)
 code:1

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #define ERROR(flag)                 \
  8.     if(flag)                \
  9. {                    \
  10.     printf("%d: ",__LINE__);    \
  11.     fflush(stdout);            \
  12.     perror("error");        \
  13.     exit(errno);            \
  14. }
  15. int main(int argc,char *argv[])
  16. {
  17. #if 0
  18.     int fd = open(argv[1],O_RDONLY);
  19.     ERROR(fd == -1);
  20. #endif
  21.     pid_t pid = fork();
  22.     ERROR(pid == -1);
  23. #if 1
  24.     int fd = open(argv[1],O_RDONLY);
  25.     ERROR(fd == -1);
  26. #endif    
  27.     if(pid == 0)
  28.     {
  29.         char ch;
  30.         int ret = read(fd,&ch,1);
  31.         ERROR(ret < 0);
  32.         printf("child process got %c\n",ch);
  33.     }
  34.     else
  35.     {
  36.         char ch;
  37.         int ret = read(fd,&ch,1);
  38.         ERROR(ret < 0);
  39.         printf("parent process got %c\n",ch);
  40.     }
  41.     return 0;
  42. }

编译链接成功后运行, 父子进程输出从文件中文件读出的内容. 结果如下图:
技术分享

例3: 孤儿进程

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #define ERROR(flag)                 \
  8.      if(flag)                \
  9.     {                    \
  10.         printf("%d: ",__LINE__);    \
  11.         fflush(stdout);            \
  12.         perror("error");        \
  13.         exit(errno);            \
  14.     }
  15. int main(int argc,char *argv[])
  16. {
  17.     pid_t pid;  
  18.     pid = fork();
  19.     ERROR(pid == -1);
  20.     if(pid == 0)
  21.     {
  22.         int i = 1000;
  23.         while(i--)
  24.         {
  25.             printf("child process, pid = %d, ppid = %d\n",getpid(),getppid());
  26.             sleep(5);
  27.         }
  28.     }
  29.     else
  30.     {
  31.             printf("parent process, byebye!!!\n");
  32.             sleep(3);
  33.             _exit(0);
  34.     }
  35.     
  36.     return 0;
  37. }

编译链接成功后运行, 父子进程输出内容. 结果如下图:
技术分享
在另一个终端查看的进程间关系如下图:
技术分享

例4: 父进程取得子进程的返回值

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include <sys/wait.h>
  8. #define ERROR(flag)                 \
  9.     if(flag)                \
  10. {                    \
  11.     printf("%d: ",__LINE__);    \
  12.     fflush(stdout);            \
  13.     perror("error");        \
  14.     exit(errno);            \
  15. }
  16. int main(int argc,char *argv[])
  17. {
  18.     pid_t pid;
  19.     pid = fork();
  20.     ERROR(pid == -1);
  21.     if(pid == 0)
  22.     {
  23.         int i = 3;
  24.         while(i--)
  25.         {
  26.             printf("child process, pid = %d, ppid = %d\n\n",
  27.             getpid(),getppid());
  28.             sleep(1);
  29.         }
  30.         
  31.         printf("chile process exit, byebye!!!\n\n");
  32.         _exit(99);
  33.     }
  34. #if 1
  35.     int stat;
  36.     pid_t child_pid;
  37.     child_pid = wait(&stat);
  38.     printf("child process has exited, pid = %d\n\n",child_pid);
  39.     if(WIFEXITED(stat))
  40.         printf("child exited with code %d\n\n",WEXITSTATUS(stat));
  41.     else
  42.         printf("child exit abnormally\n\n");
  43. #endif
  44.     printf("parent process, pid = %d\n",getpid());
  45.     getchar();
  46.     return 0;
  47. }

编译链接成功后运行, 父子进程输出内容. 结果如下图:
技术分享
例5: 进程退出

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <errno.h>
  8. int main(int argc, char *argv[])
  9. {
  10.     printf("hello");
  11.     _exit(0);
  12.     return 0;
  13. }

编译链接成功后运行, 无输出内容. 结果如下图:
技术分享

注释掉_exit()函数后,如下

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <errno.h>
  8. int main(int argc, char *argv[])
  9. {
  10.     printf("hello");
  11.     //_exit(0);
  12.     return 0;
  13. }

编译链接成功后运行, 输出内容如下图:
技术分享

_exit()使得进程在字符输出到显示设备之前退出.

fork系统调用

标签:

原文地址:http://www.cnblogs.com/zhanglong71/p/5074466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!