Fork!
This commit is contained in:
22
fork.cpp
Normal file
22
fork.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main() {
|
||||
std::cout << "wo shi ni die" << std::endl;
|
||||
pid_t pid;
|
||||
int result;
|
||||
for (int i = 0; i < 1024; i++) {
|
||||
if ((pid = fork()) == 0) {
|
||||
std::cout << "child #" << i << std::endl;
|
||||
exit(i);
|
||||
// return 0;
|
||||
}
|
||||
else {
|
||||
waitpid(pid, &result, 0);
|
||||
}
|
||||
std::cout << "child #" << i << " exited with code" << WEXITSTATUS(result) << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user