c++开子进程

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

using namespace std;

int main() {
  pid_t pid;

  int num = 888;
  // fork返回两次(父进程-返回-子进程id、子进程-返回-0)
  pid = fork();

  if (pid == 0) {
    cout << "这是一个子进程." << endl;
	cout << "子进程打印 num: " << num << endl;
	while (true) {
		num += 1;
		cout << "子进程打印 循环num: " << numm << endl;
		sleep(1);
	}
  } else if (pid > 0) {
    cout << "这身一个父进程." <<  endl;
    cout << "父进程打印 子进程id: " << pid << endl;
	cout << "父进程打印 num: " << num << endl;
	while (true) {
		num -= 1;
		cout << "父进程打印 循环num: " << numm << endl;
		sleep(1);
	}
  } else {
    cout << "创建进程失败." << endl;
  }
  return 0;
}
发布了185 篇原创文章 · 获赞 121 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/echo_Ae/article/details/103486178
今日推荐