📄 test5.c
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <string.h>#include <errno.h>void child_process_main(void);int main(int argc, char **argv){ fprintf(stdout, "[%d]current pid = %d, ppid = %d\n", getpid(), getpid(), getppid()); int i; for (i = 0; i < 3; i++) { pid_t pid; //if ((pid = fork()) < 0) if ((pid = vfork()) < 0) { fprintf(stderr, "[%d]fork() failed: %s\n", getpid(), strerror(errno)); } else if (pid == 0) { // XXX: Child process fprintf(stdout, "[%d]I'm new created process, pid = %d, ppid = %d\n", getpid(), getpid(), getppid()); //child_process_main(); //exec("ls"); exit(0); } else { // XXX: parent process fprintf(stdout, "[%d] New created child process pid = %d\n", getpid(), pid); fprintf(stdout, "[%d] current pid = %d, ppid = %d\n", getpid(), getpid(), getppid()); } } return 0;}void child_process_main(void){ fprintf(stdout, "child_process_main()\n"); //exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -