赞
踩
#include <cstdio>
#include <iostream>
#include <unistd.h>
using namespace std;
int main ( void ) {
fork();
fork();
fork();
cout << "S" << endl;
return 0;
}
执行第一个fork调用时,
进程A创建了进程B;
执行第二个fork调用时,
进程A创建了进程C,
进程B创建了进程D;
执行第三个fork调用时,
进程A创建了进程E,
进程B创建了进程F,
进程C创建了进程G,
进程D创建了进程H。
如图,
#include <iostream> #include <unistd.h> using namespace std; int main ( void ) { cout << "Program begin." << endl; int pid = fork(); if ( pid != 0 ) { cout << "Process 'pid != 0' -> true (parentId, selfId):" << "(" << getppid() << ", " << getpid() << ")" << " / pid=" << pid << endl; for ( int i = 0; i < 10; ++i ) { cout << "Parent " << "\t" << i << endl; sleep(1); } } else { cout << "Process 'pid != 0' -> false (parentId, selfId):" << "("
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。