add 4, edit 3

This commit is contained in:
2026-05-12 19:13:20 +07:00
parent 17ae59ea48
commit 2ad3f7eaa4
16 changed files with 1944 additions and 94 deletions
+34 -9
View File
@@ -173,9 +173,12 @@ static pid_t spawn_sort_child(const std::vector<i32>& part,
try {
close_checked(to_child[1]);
close_checked(from_child[0]);
std::vector<i32> input = recv_vector(to_child[0]);
close_checked(to_child[0]);
SortResult result = process_recursive_sort(std::move(input), child_depth, opt);
send_result(from_child[1], result);
close_checked(from_child[1]);
_exit(0);
@@ -186,8 +189,10 @@ static pid_t spawn_sort_child(const std::vector<i32>& part,
close_checked(to_child[0]);
close_checked(from_child[1]);
send_vector(to_child[1], part);
close_checked(to_child[1]);
result_read_fd = from_child[0];
return pid;
}
@@ -205,21 +210,28 @@ static SortResult process_recursive_sort(std::vector<i32> a, int depth, const Op
std::vector<i32> left(a.begin(), a.begin() + static_cast<long>(mid));
std::vector<i32> right(a.begin() + static_cast<long>(mid), a.end());
int left_fd = -1, right_fd = -1;
int left_fd = -1;
int right_fd = -1;
pid_t left_pid = spawn_sort_child(left, depth + 1, opt, left_fd);
pid_t right_pid = spawn_sort_child(right, depth + 1, opt, right_fd);
SortResult left_result = recv_result(left_fd);
SortResult right_result = recv_result(right_fd);
close_checked(left_fd);
close_checked(right_fd);
int status_left = 0, status_right = 0;
int status_left = 0;
int status_right = 0;
while (waitpid(left_pid, &status_left, 0) < 0 && errno == EINTR) {}
while (waitpid(right_pid, &status_right, 0) < 0 && errno == EINTR) {}
if (!WIFEXITED(status_left) || WEXITSTATUS(status_left) != 0) {
throw std::runtime_error("left child failed");
}
if (!WIFEXITED(status_right) || WEXITSTATUS(status_right) != 0) {
throw std::runtime_error("right child failed");
}
@@ -234,19 +246,28 @@ static SortResult process_recursive_sort(std::vector<i32> a, int depth, const Op
static Options parse_args(int argc, char** argv) {
Options opt;
for (int i = 1; i < argc; ++i) {
std::string s = argv[i];
auto need_value = [&](const std::string& name) -> std::string {
if (i + 1 >= argc) throw std::runtime_error("missing value for " + name);
return argv[++i];
};
if (s == "--size" || s == "-n") opt.size = std::stoull(need_value(s));
else if (s == "--depth" || s == "-d") opt.max_depth = std::stoi(need_value(s));
else if (s == "--min-size" || s == "-m") opt.min_size = std::stoull(need_value(s));
else if (s == "--seed") opt.seed = static_cast<unsigned>(std::stoul(need_value(s)));
else if (s == "--print") opt.print = true;
else if (s == "--log") opt.log = true;
else if (s == "--help" || s == "-h") {
if (s == "--size" || s == "-n") {
opt.size = std::stoull(need_value(s));
} else if (s == "--depth" || s == "-d") {
opt.max_depth = std::stoi(need_value(s));
} else if (s == "--min-size" || s == "-m") {
opt.min_size = std::stoull(need_value(s));
} else if (s == "--seed") {
opt.seed = static_cast<unsigned>(std::stoul(need_value(s)));
} else if (s == "--print") {
opt.print = true;
} else if (s == "--log") {
opt.log = true;
} else if (s == "--help" || s == "-h") {
std::cout << "Usage: ./lab3 [--size N] [--depth D] [--min-size M] [--seed S] "
<< "[--print] [--log]\n";
std::exit(0);
@@ -254,6 +275,7 @@ static Options parse_args(int argc, char** argv) {
throw std::runtime_error("unknown argument: " + s);
}
}
if (opt.max_depth < 0) throw std::runtime_error("depth must be non-negative");
return opt;
}
@@ -264,6 +286,7 @@ static std::vector<i32> generate_data(const Options& opt) {
std::vector<i32> a(opt.size);
std::mt19937 rng(opt.seed);
std::uniform_int_distribution<i32> dist(-100000000, 100000000);
for (auto& x : a) x = dist(rng);
return a;
}
@@ -276,6 +299,7 @@ int main(int argc, char** argv) {
const auto t1 = std::chrono::steady_clock::now();
SortResult result = process_recursive_sort(std::move(data), 0, opt);
const auto t2 = std::chrono::steady_clock::now();
const double elapsed = std::chrono::duration<double>(t2 - t1).count();
const bool ok = std::is_sorted(result.data.begin(), result.data.end());
@@ -293,6 +317,7 @@ int main(int argc, char** argv) {
<< " processes=" << result.processes
<< " valid=" << (ok ? 1 : 0)
<< " time=" << elapsed << " sec\n";
return ok ? 0 : 3;
} catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << "\n";