1.vfork也可以创建进程,与forkde的区别
1.1 vfork直接使用父进程存储空间,不拷贝
1.2 vfork保证子进程先运行,当子进程调用exit退出后,父进程才执行
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int cnt = 0;
pid = vfork(); //当子进程调用exit退出后,父进程才执行
if(pid > 0){
while(1){
printf("this is father print\n",getpid());
sleep(1);
printf("cnt = %d\n",cnt);
}
}
else if(pid == 0){
while(1){
printf("this is child print\n",getpid());
sleep(1);
cnt++;
if(cnt == 3){
exit(0);