#include <stdio.h>#include <pthread.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#define P_INFO printf("chld %u, self %u, father %u \n", pid, getpid(), getppid())void err_sys (const char* val){ fprintf(stdout, val); return;
}void func (void){ pid_t pid = -1;
pid = vfork(); if (pid < 0) { err_sys("vfork failed\n");
} if ( pid == 0) { printf("child 2\n");
P_INFO; return;
} if (pid > 0) { printf("father 1 func\n");
P_INFO;
return;
}
}void funf (void){ pid_t pid = getpgrp();
P_INFO; return;
}int main (int argc, char** argv) { pid_t pid = getpgrp(); printf("father 1\n"); func(); printf("father 1, out func to funf\n"); funf(); printf("father 1, out funf to exit\n");
P_INFO; return 0;
}
如果不是在main函數(shù)中而是在另一個函數(shù)中調(diào)用vfork,以后子進程從該函數(shù)返回時,會發(fā)生什么情況?
呼啦一陣風(fēng)
2023-04-18 19:15:30