test5.c

来自「这个是学习嵌入式开发的重要例子」· C语言 代码 · 共 49 行

C
49
字号
#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 + =
减小字号Ctrl + -
显示快捷键?