您的位置:首页 > 其它

fork函数创建进程

2015-08-22 09:53 267 查看
执行fork()函数会创建一个新的进程,分别是父进程和子进程,这样的父子关系就相当于是克隆了一个和父亲一样的儿子。

#include<stdio.h>
 #include<unistd.h>
 
  int main()
  {
          pid_t ftip;
          int count = 0;
          ftip = fork();
          if(ftip<0)
                  printf("error in fork!");
          else if(ftip==0){
                  printf("I am the child process, my process id is %d\n", getpid());
                  printf("我是儿子\n");
                  count++;
          }else{
                  printf("I am the parent process, my process id is %d\n", getpid());
                  printf("我是爹\n");
                  count++;
          }
          printf("统计结果是:%d\n", count);
          return 0;
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: