vfork并不完全复制父进程的数据段,而是和父进程共享数据段。调用vfork对于父子进程的执行次序有所限制,调用vfork时,父进程将被挂起,子进程运行至调用exec函数族或调用exit时解除这种状态。编写代码如下:
- #include <sys/types.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
-
int main(int argc, char *argv[])
-
{
-
pid_t pid;
-
if((pid=vfork()) < 0){
-
printf("fork error!\n");
-
exit(1);
-
}
-
else if(pid == 0){
-
printf("child process is printing.\n");
-
}
-
else{
-
printf("parent process is printing.\n");
-
}
-
exit(0);
-
}
执行结果如下:
peng@ubuntu:~/src/test/c/linuxc$ gcc 6.2.c
peng@ubuntu:~/src/test/c/linuxc$ ./a.out
child process is printing.
parent process is printing.
peng@ubuntu:~/src/test/c/linuxc$ ./a.out
child process is printing.
parent process is printing.
阅读(2954) | 评论(1) | 转发(0) |