fork???vfork??????????????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fork???vfork??????????????????相关的知识,希望对你有一定的参考价值。
?????????fork??????vfork??????????????????
fork???vfork?????????????????????
fork?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
#include<stdio.h> #include<sys/types.h> #include<stdio.h> int main() { int count=0; pid_t pid; pid=fork(); // ????????????????????? if(pid<0) { printf("error in fork!"); exit(1); } else if(pid==0) { printf("I am the child process,the count is %d,my process ID %d,pid=%d\n",++count,getpid(),pid); exit(0); } else printf("I am the parent process,the count is %d,my process ID %d,pid=%d\n",count,getpid(),pid); return 0; }
???????????????
I am the parent process,the count is 0,my process ID 2765,pid=2766
I am the child process,the count is 1,my process ID 2766,pid=0
????????????????????????????????????pid????????????fork?????????????????????????????????????????????????????????????????????????????????????????????3?????????????????????
??????????????????fork??????????????????????????????ID
??????????????????fork??????0
?????????????????????fork??????????????????
fork???????????????????????????????????????????????????????????????????????????????????????????????????errno??????????????????EAGAIN??????????????????????????????????????????errno??????????????????ENOMEM
2.vfork
???fork??????????????????????????????????????????????????????vfork???fork??????????????????fork????????????????????????????????????vfork????????????????????????????????????????????????????????????????????????exec???exit????????????????????????????????????????????????fork??????????????????????????????????????????????????????vfork??????????????????????????????????????????????????????????????????????????????exec??????exit??????????????????????????????????????????????????????
vfork????????????????????????????????????????????????
#include<stdio.h> #include<sys/types.h> #include<unistd.h> int main(void) { int count=1; int child; printf("Before create son, the father???s count is %d\n",count); child=vfork(); //???????????????????????????????????????????????????????????? if(child < 0) { printf("error in vfork!\n"); exit(1); } if(child==0) { printf("This is son,his pid id %d and the count is %d\n",getpid(),++count); exit(1); } else { printf("After son, This is father,his pid is %d and the count is %d, and the child is %d\n",getpid(),count,child); } return 0; }
Before create son, the father???s count is 1
This is son,his pid id 2809 and the count is 2
After son, This is father,his pid is 2808 and the count is 2, and the child is 2809
????????????????????????count?????????????????????????????????count????????????2??????????????????????????????????????????count????????????????????????vfork??????????????????????????????????????????????????????????????????
????????????vfork??????????????????????????????????????????????????????????????????????????????exit??????exec?????????????????????????????????
#include<stdio.h> #include<sys/types.h> #include<unistd.h> int main() { int count = 1; int child; printf("Before create son,the father???s connt is %d\n",count); if(!(child=vfork())) { int i; for(i=0;i<100;i++) { printf("This is son,the i is %d\n",i); if(i==10)exit(0); } printf("This is son, his pid is %d and the count is %d\n",getpid(),++count); exit(0); } else { printf("After son,This is father,his pid id %d and the count is %d and the child is %d\n",getpid(),count,child); } return 0; }
?????????????????????
Before create son,the father???s connt is 1
This is son,the i is 0
This is son,the i is 1
This is son,the i is 2
This is son,the i is 3
This is son,the i is 4
This is son,the i is 5
This is son,the i is 6
This is son,the i is 7
This is son,the i is 8
This is son,the i is 9
This is son,the i is 10
After son,This is father,his pid id 2720 and the count is 1 and the child is 2721
????????????????????????????????????????????????????????????????????????????????????
???????????? ?????????????????????????????? ?????????????????????????????????http://9409270.blog.51cto.com/9399270/1869621
以上是关于fork???vfork??????????????????的主要内容,如果未能解决你的问题,请参考以下文章