#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int globVar = 5;
int main(void)
{
pid_t pid;
int i, var = 1;
printf("fork is different with vfork \n");
pid = fork();
switch(pid) {
case 0:
i = 3;
while( i-- > 0 ) {
printf("Child process is running\n");
globVar++;
var++;
sleep(1);
}
printf("Child's globVar = %d, var = %d\n", globVar,var);
break;
case -1:
perror("Process creation failed\n");
exit(0);
default:
i = 5;
while(i-- > 0) {
printf("Perent process is running\n");
globVar++;
var++;
sleep(1);
}
printf("Perent's GlobVar = %d, var = %d\n", globVar, var);
exit(0);
}
exit(0);
}
beyes@linux-beyes:~/C/base> ./diffork.exe
fork is different with vfork
Perent process is running
Child process is running
Perent process is running
Child process is running
Child process is running
Perent process is running
Child's globVar = 8, var = 4
Perent process is running
Perent process is running
Perent's GlobVar = 10, var = 6
[beyes@localhost syscall]$ ./vfork
fork is different with vfork
Child process is running
Child process is running
Child process is running
Child's globVar = 8, var = 4
Perent process is running
Perent process is running
Perent process is running
Perent process is running
Perent process is running
Perent's GlobVar = 13, var = 9
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |