Files
rocm-systems/examples/fork/fork.cpp
T

137 خطوط
3.9 KiB
C++

2023-02-08 01:31:38 -06:00
2023-02-27 12:09:03 -06:00
#include <omnitrace/user.h>
2023-02-08 01:31:38 -06:00
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
2023-10-16 18:04:47 -05:00
#include <pthread.h>
#include <set>
2023-02-08 01:31:38 -06:00
#include <string>
#include <sys/wait.h>
#include <thread>
#include <unistd.h>
2023-06-14 11:55:22 -05:00
#include <vector>
2023-02-08 01:31:38 -06:00
void
print_info(const char* _name)
{
2023-06-14 11:55:22 -05:00
fflush(stdout);
fflush(stderr);
2023-02-08 01:31:38 -06:00
printf("[%s] pid = %i, ppid = %i\n", _name, getpid(), getppid());
2023-06-14 11:55:22 -05:00
fflush(stdout);
fflush(stderr);
2023-02-08 01:31:38 -06:00
}
int
run(const char* _name, int nchildren)
{
2023-10-16 18:04:47 -05:00
auto _barrier = pthread_barrier_t{};
auto _threads = std::vector<std::thread>{};
auto _children = std::vector<pid_t>{};
_children.resize(nchildren, 0);
pthread_barrier_init(&_barrier, nullptr, nchildren + 1);
2023-02-08 01:31:38 -06:00
for(int i = 0; i < nchildren; ++i)
{
2023-02-27 12:09:03 -06:00
omnitrace_user_push_region("launch_child");
2023-10-16 18:04:47 -05:00
auto _run = [&_barrier, &_children, i, _name](uint64_t _nsec) {
pthread_barrier_wait(&_barrier);
_children.at(i) = fork();
if(_children.at(i) == 0)
2023-02-08 01:31:38 -06:00
{
// child code
print_info(_name);
2023-10-16 18:04:47 -05:00
printf("[%s][%i] child job starting...\n", _name, getpid());
2023-02-08 01:31:38 -06:00
auto _sleep = [=]() {
2023-10-16 18:04:47 -05:00
omnitrace_user_push_region("child_process_child_thread");
std::this_thread::sleep_for(std::chrono::seconds{ _nsec });
omnitrace_user_pop_region("child_process_child_thread");
2023-02-08 01:31:38 -06:00
};
2023-10-16 18:04:47 -05:00
omnitrace_user_push_region("child_process");
2023-02-08 01:31:38 -06:00
std::thread{ _sleep }.join();
2023-10-16 18:04:47 -05:00
omnitrace_user_push_region("child_process");
printf("[%s][%i] child job complete\n", _name, getpid());
2023-02-08 01:31:38 -06:00
exit(EXIT_SUCCESS);
}
2023-10-16 18:04:47 -05:00
else
{
pthread_barrier_wait(&_barrier);
}
2023-02-08 01:31:38 -06:00
};
2023-10-16 18:04:47 -05:00
_threads.emplace_back(_run, i + 1);
2023-02-27 12:09:03 -06:00
omnitrace_user_pop_region("launch_child");
2023-02-08 01:31:38 -06:00
}
2023-10-16 18:04:47 -05:00
// all child threads should start executing their fork once this returns
pthread_barrier_wait(&_barrier);
// wait for the threads to successfully fork
pthread_barrier_wait(&_barrier);
2023-02-27 12:09:03 -06:00
omnitrace_user_push_region("wait_for_children");
2023-02-08 01:31:38 -06:00
int _status = 0;
pid_t _wait_pid = 0;
// parent waits for all the child processes
2023-10-16 18:04:47 -05:00
for(auto& itr : _children)
2023-02-08 01:31:38 -06:00
{
2023-10-16 18:04:47 -05:00
while(itr == 0)
{}
printf("[%s][%i] performing waitpid(%i, ...)\n", _name, getpid(), itr);
while((_wait_pid = waitpid(itr, &_status, WUNTRACED | WNOHANG)) <= 0)
2023-02-08 01:31:38 -06:00
{
2023-10-16 18:04:47 -05:00
if(_wait_pid == 0) continue;
printf("[%s][%i] returned from waitpid(%i) with pid = %i (status = %i) :: ",
_name, getpid(), itr, _wait_pid, _status);
if(WIFEXITED(_status))
{
printf("exited, status=%d\n", WEXITSTATUS(_status));
}
else if(WIFSIGNALED(_status))
{
printf("killed by signal %d\n", WTERMSIG(_status));
}
else if(WIFSTOPPED(_status))
{
printf("stopped by signal %d\n", WSTOPSIG(_status));
}
else if(WIFCONTINUED(_status))
{
printf("continued\n");
}
else
{
printf("unknown\n");
}
2023-02-08 01:31:38 -06:00
}
}
2023-02-27 12:09:03 -06:00
2023-10-16 18:04:47 -05:00
printf("[%s][%i] joining threads ...\n", _name, getpid());
2023-06-14 11:55:22 -05:00
for(auto& itr : _threads)
itr.join();
2023-02-27 12:09:03 -06:00
omnitrace_user_pop_region("wait_for_children");
2023-10-16 18:04:47 -05:00
printf("[%s][%i] returning (error code: %i) ...\n", _name, getpid(), _status);
2023-02-08 01:31:38 -06:00
return _status;
}
int
main(int argc, char** argv)
{
2023-10-16 18:04:47 -05:00
int _nfork = 4;
int _nrep = 1;
if(argc > 1) _nfork = std::stoi(argv[1]);
if(argc > 2) _nrep = std::stoi(argv[2]);
2023-02-08 01:31:38 -06:00
print_info(argv[0]);
2023-10-16 18:04:47 -05:00
for(int i = 0; i < _nrep; ++i)
{
auto _ec = run(argv[0], _nfork);
if(_ec != 0) return _ec;
}
printf("[%s][%i] job complete\n", argv[0], getpid());
return EXIT_SUCCESS;
2023-02-08 01:31:38 -06:00
}