#include<stdio.h> intmain( ) { int A; /* fork another process */ A = fork( ); if (A == 0) { /* child process */ printf("this is from child process\n"); execlp("/bin/ls", "ls", NULL); } else { /* parent process */ printf("this is from parent process\n"); int status; int pid = wait(&status); printf("Child %d completes", pid); } printf("process ends %d\n", A);
return0; }
执行结果:
1 2 3 4 5 6 7 8
#include<stdio.h> #include<unistd.h> intmain(){ for (int i = 0; i < 3; i++){ fork(); } return0; }