init
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
# Компилятор
|
||||||
|
CXX = g++
|
||||||
|
|
||||||
|
# Флаги
|
||||||
|
CXXFLAGS = -Wall -Wextra -O2 -std=c++17
|
||||||
|
|
||||||
|
# Линковка (для shm, fork, etc.)
|
||||||
|
LDFLAGS = -lrt
|
||||||
|
|
||||||
|
# Имя программы
|
||||||
|
TARGET = lab1
|
||||||
|
|
||||||
|
# Исходник
|
||||||
|
SRC = main.cpp
|
||||||
|
|
||||||
|
# ==========================
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(SRC)
|
||||||
|
$(CXX) $(CXXFLAGS) $(SRC) -o $(TARGET) $(LDFLAGS)
|
||||||
|
|
||||||
|
run: $(TARGET)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET)
|
||||||
|
|
||||||
|
rebuild: clean all
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
#define MAX 100000
|
||||||
|
#define MAX_DEPTH 3
|
||||||
|
|
||||||
|
struct SharedData {
|
||||||
|
int arr[MAX];
|
||||||
|
};
|
||||||
|
|
||||||
|
// ================= 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++];
|
||||||
|
}
|
||||||
|
|
||||||
|
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];
|
||||||
|
|
||||||
|
delete[] temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= LOCAL SORT =================
|
||||||
|
void local_sort(int *arr, int l, int r) {
|
||||||
|
if (l >= r) return;
|
||||||
|
int m = (l + r) / 2;
|
||||||
|
local_sort(arr, l, m);
|
||||||
|
local_sort(arr, m + 1, r);
|
||||||
|
merge(arr, l, m, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= PARALLEL SORT =================
|
||||||
|
void parallel_sort(int *arr, int l, int r, int depth) {
|
||||||
|
if (l >= r) return;
|
||||||
|
|
||||||
|
int m = (l + r) / 2;
|
||||||
|
|
||||||
|
cout << "PID " << getpid()
|
||||||
|
<< " depth=" << depth
|
||||||
|
<< " range=[" << l << "," << r << "]\n";
|
||||||
|
|
||||||
|
if (depth >= MAX_DEPTH) {
|
||||||
|
local_sort(arr, l, r);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pid_t left = fork();
|
||||||
|
|
||||||
|
if (left == 0) {
|
||||||
|
parallel_sort(arr, l, m, depth + 1);
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pid_t right = fork();
|
||||||
|
|
||||||
|
if (right == 0) {
|
||||||
|
parallel_sort(arr, m + 1, r, depth + 1);
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
waitpid(left, NULL, 0);
|
||||||
|
waitpid(right, NULL, 0);
|
||||||
|
|
||||||
|
merge(arr, l, m, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= 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);
|
||||||
|
|
||||||
|
int n = 20000;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
data->arr[i] = rand() % 100000;
|
||||||
|
|
||||||
|
timeval start, end;
|
||||||
|
|
||||||
|
gettimeofday(&start, NULL);
|
||||||
|
|
||||||
|
parallel_sort(data->arr, 0, n - 1, 0);
|
||||||
|
|
||||||
|
gettimeofday(&end, NULL);
|
||||||
|
|
||||||
|
double time_spent =
|
||||||
|
(end.tv_sec - start.tv_sec) +
|
||||||
|
(end.tv_usec - start.tv_usec) / 1e6;
|
||||||
|
|
||||||
|
cout << "\nExecution time: " << time_spent << " 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";
|
||||||
|
|
||||||
|
shmdt(data);
|
||||||
|
shmctl(shmid, IPC_RMID, NULL);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user