fork函数创建进程

来源:互联网 发布:网络推广的岗位职责 编辑:程序博客网 时间:2024/06/04 17:59

执行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;  }

0 0
原创粉丝点击