一个C++多进程的例子

南乡水 / 2024-02-26 / 原文

#include <iostream>
#include <unistd.h>

int main(int argc, char *argv[]) {
  pid_t pid = fork();
  if (pid > 0) {
    std::cout << "In parent process." << std::endl;
  }
  if (pid == 0) {
    std::cout << "In child process." << std::endl;
    execlp("echo", "", "Hello world!", NULL);
  }
}