seems nice

This commit is contained in:
2026-04-21 11:04:11 +07:00
parent 1c1c52786c
commit a729a9f68c
2 changed files with 58 additions and 57 deletions
BIN
View File
Binary file not shown.
+58 -57
View File
@@ -5,6 +5,9 @@
#include <unistd.h>
#include <sys/time.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <sstream>
using namespace std;
@@ -15,36 +18,35 @@ struct SharedData {
int arr[MAX];
};
// ================= TIME =================
double now() {
timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + t.tv_usec / 1e6;
string now() {
timeval tv;
gettimeofday(&tv, NULL);
ostringstream oss;
oss << tv.tv_sec << "."
<< setfill('0') << setw(6) << tv.tv_usec;
return oss.str();
}
// ================= MERGE =================
void merge(int *arr, int l, int m, int r) {
int *temp = new int[r - l + 1];
int i = l, j = m + 1, k = 0;
while (i <= m && j <= r) {
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
if (arr[i] < arr[j]) temp[k++] = arr[i++];
else temp[k++] = arr[j++];
}
while (i <= m) temp[k++] = arr[i++];
while (j <= r) temp[k++] = arr[j++];
for (int x = 0; x < k; x++)
arr[l + x] = temp[x];
for (int x = 0; x < k; x++) arr[l + x] = temp[x];
delete[] temp;
}
// ================= LOCAL SORT =================
void local_sort(int *arr, int l, int r) {
if (l >= r) return;
int m = (l + r) / 2;
@@ -53,45 +55,53 @@ void local_sort(int *arr, int l, int r) {
merge(arr, l, m, r);
}
// ================= PARALLEL SORT =================
void parallel_sort(int *arr, int l, int r, int depth) {
if (l >= r) return;
void parallel_sort(int l, int r, int depth, int shmid) {
SharedData *data = (SharedData*) shmat(shmid, NULL, 0);
int *arr = data->arr;
double start = now();
string start_time = now();
pid_t pid = getpid();
pid_t ppid = getppid();
cout << "[START] t=" << start
<< " PID=" << pid
<< " PPID=" << ppid
cout << "START PID=" << getpid()
<< " PPID=" << getppid()
<< " depth=" << depth
<< " range=[" << l << "," << r << "]\n";
<< " range=[" << l << "," << r << "]"
<< " time=" << start_time << "\n";
if (l >= r) {
string end_time = now();
cout << "END PID=" << getpid()
<< " range=[" << l << "," << r << "]"
<< " time=" << end_time << "\n";
shmdt(data);
return;
}
int m = (l + r) / 2;
if (depth >= MAX_DEPTH) {
local_sort(arr, l, r);
double end = now();
cout << "[END] t=" << end
<< " PID=" << pid
<< " duration=" << (end - start)
<< " sec\n";
string end_time = now();
cout << "END PID=" << getpid()
<< " depth=" << depth
<< " range=[" << l << "," << r << "]"
<< " time=" << end_time << "\n";
shmdt(data);
return;
}
pid_t left = fork();
if (left == 0) {
parallel_sort(arr, l, m, depth + 1);
parallel_sort(l, m, depth + 1, shmid);
_exit(0);
}
pid_t right = fork();
if (right == 0) {
parallel_sort(arr, m + 1, r, depth + 1);
parallel_sort(m + 1, r, depth + 1, shmid);
_exit(0);
}
@@ -100,20 +110,21 @@ void parallel_sort(int *arr, int l, int r, int depth) {
merge(arr, l, m, r);
double end = now();
string end_time = now();
cout << "[END] t=" << end
<< " PID=" << pid
<< " duration=" << (end - start)
<< " sec\n";
cout << "END PID=" << getpid()
<< " depth=" << depth
<< " range=[" << l << "," << r << "]"
<< " time=" << end_time << "\n";
shmdt(data);
}
// ================= MAIN =================
int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, sizeof(SharedData), 0666 | IPC_CREAT);
SharedData *data = (SharedData*) shmat(shmid, (void*)0, 0);
SharedData *data = (SharedData*) shmat(shmid, NULL, 0);
int n = 20000;
@@ -122,27 +133,17 @@ int main() {
for (int i = 0; i < n; i++)
data->arr[i] = rand() % 100000;
double global_start = now();
cout << "Before:\n";
for (int i = 0; i < 20; i++)
cout << data->arr[i] << " ";
cout << "\n\n";
cout << "Main PID=" << getpid() << "\n\n";
parallel_sort(0, n - 1, 0, shmid);
parallel_sort(data->arr, 0, n - 1, 0);
double global_end = now();
cout << "\nExecution time: "
<< (global_end - global_start)
<< " sec\n";
// Проверка
for (int i = 1; i < n; i++) {
if (data->arr[i - 1] > data->arr[i]) {
cout << "ERROR: not sorted!\n";
break;
}
}
cout << "Sorted correctly\n";
cout << "\nAfter:\n";
for (int i = 0; i < 20; i++)
cout << data->arr[i] << " ";
cout << "\n";
shmdt(data);
shmctl(shmid, IPC_RMID, NULL);