#include<unistd.h> #include<sys/types.h> #include<pwd.h> #include<stdio.h> int main(int argc,char **argv) { pid_t my_pid,parent_pid; pid_t te ; my_pid=getpid(); parent_pid=getppid(); printf("mypid is [%d] parentid is [%d] ",my_pid,parent_pid); te = fork(); printf("*********begin************* "); my_pid=getpid(); parent_pid=getppid(); printf("mypid is [%d] parentid is [%d] te is %d ",my_pid,parent_pid,te); printf("*********end************* "); #if 0 te = fork(); my_pid=getpid(); parent_pid=getppid(); printf("mypid is [%d] parentid is [%d] te is %d ",my_pid,parent_pid,te); getchar(); #endif
RESULT:
mypid is [1705] parentid is [1521]
*********begin*************
mypid is [1705] parentid is [1521] te is 1706
*********end*************
root@ubuntu:/usr/lsrcc/tempUserFun# *********begin*************
mypid is [1706] parentid is [1] te is 0
*********end*************
结果分析:调用fork一次,fork返回2次,一个是父进程,一个是子进程。
#include<unistd.h> #include<sys/types.h> #include<pwd.h> #include<stdio.h> int main(int argc,char **argv) { pid_t my_pid,parent_pid; pid_t te ; my_pid=getpid(); parent_pid=getppid(); printf("mypid is [%d] parentid is [%d] ",my_pid,parent_pid); te = fork(); printf("*********begin************* "); my_pid=getpid(); parent_pid=getppid(); printf("mypid is [%d] parentid is [%d] te is %d ",my_pid,parent_pid,te); printf("*********end************* "); #if 1 printf("*********2THbegin************* "); te = fork(); my_pid=getpid(); parent_pid=getppid(); printf("mypid is [%d] parentid is [%d] te is %d ",my_pid,parent_pid,te); printf("********2TH*end************* "); getchar(); #endif }
RESULT:
*********begin*************
mypid is [1801] parentid is [1521] te is 1802
*********end*************//第一次fork父进程
*********2THbegin*************
mypid is [1801] parentid is [1521] te is 1803//第2次fork的父进程(在第一次fork的父进程基础上)
********2TH*end*************
mypid is [1803] parentid is [1801] te is 0//第2次fork的子进程(在第一次fork的父进程基础上)
********2TH*end*************
*********begin*************
mypid is [1802] parentid is [1801] te is 0
*********end*************//第1次fork的子进程
*********2THbegin*************
mypid is [1802] parentid is [1801] te is 1804//第2次fork的父进程(在第2次fork的父进程基础上)
********2TH*end*************
mypid is [1804] parentid is [1802] te is 0//第2次fork的子进程(在第2次fork的父进程基础上)
********2TH*end*************
系统运行结束后有6个进程。
#include<unistd.h> #include<sys/types.h> #include<pwd.h> #include<stdio.h> int main(int argc,char **argv) { pid_t my_pid,parent_pid; pid_t te ; static int count = 0; my_pid=getpid(); parent_pid = getppid(); int i = 0; const char *p = "/usr/lsrcc/dMF/main"; for(i = 0;i < 2;++i){ te = fork(); if(te < 0){ printf("fork err! "); return -1; } if(te > 0){ printf("i is %i mypid is [%d] parentid is [%d] te is %d ",i,my_pid,parent_pid,te); printf("this is PARENT "); count++; } else if(0 == te){ printf("i is %i mypid is [%d] parentid is [%d] te is %d ",i,my_pid,parent_pid,te); printf("this is CHILD "); count++; execlp(p,NULL); } } printf("count is %d ",count); }
RESULT:
i is 0
mypid is [2798] parentid is [2361] te is 2799
this is PARENT
i is 1
mypid is [2798] parentid is [2361] te is 2800
this is PARENT
count is 2
root@ubuntu:/usr/lsrcc/tempUserFun# i is 1
i is 0
mypid is [2798] parentid is [2361] te is 0
this is CHILD
This is mytool1 print hello
mypid is [2798] parentid is [2361] te is 0
this is CHILD
This is mytool1 print hello
This is mytool2 print hello
This is mytool2 print hello
//主要提供一个子进程调用的例子