parallel-programming/1/threads/linux/thread1.cpp

106 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <thread>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int shame(int i, double long h, double long l) //функция для поиска чисел с 3 делителями
{
ofstream file2;
string path2 = "/home/danila/thread1output/" + to_string(i); //открываем файл, в который будем записывать промежуточные результаты от одного потока
file2.open(path2);
ifstream file3;
string path3 = "/home/danila/thread1output/simples"; //открываем файл с простыми числами
file3.open(path3);
string dasha;
while (getline(file3, dasha)) //читаем каждую строку из файла с простыми числами
{
int vika = stoi(dasha); //преобразуем string в int
double long katya = pow(vika, 4); //возводим простые числа в 4 степень
if (katya > h && katya < l) //если эта 4-я степень входит в промежуток, то записываем ее в промежуточные результаты
{
file2 << fixed << katya << endl;
}
}
file2.close();
file3.close();
return 0;
}
int main()
{
setlocale(LC_ALL, "Russian");
std::cout << "Start proccess\n";
ofstream file1;
string path1 = "/home/danila/thread1output/simples";//открываем файл, в который будем записывать простые числа
file1.open(path1);
int check = 1;
for (int i = 2; i <= pow(100000000000, 0.5); i++) //записывем в файл простые числа
{
check = 1;
for (int j = 2; j < i; j++)
{
if (i%j == 0) {
check = 0;
break;
}
}
if (check == 1) {
file1 << i << endl;
}
}
file1.close();
double long first = 10000000000; //здесь вводим верхнюю границу промежутка, в котором будем искать числа с 3 делителями
double long last = 100000000000; //здесь нижнюю
double long dif = (last - first) / 4; //находим, сколько чисел будет обрабатывать один поток (20 - это кол-во потоков на моём ноуте!!! Вам его нужно будет менять!!!!!!)
double long h = first;
double long l = h + dif;
//начало thread
std::vector<std::thread> th;
for (int i = 1; i <= 4; i++) { //здесь нужно заменить 20
th.push_back(thread(shame, i, h, l)); //добавление потоков в вектор и вызов функции
h += dif;
l += dif;
//cout << h << " " << l << endl;
}
for (auto& t : th) { //запуск потоков
t.join();
//cout << thread::get_id << endl;
}
ofstream result;
result.open("/home/danila/thread1output/result"); //окрываем файл, в котором будет храниться конечный результат
for (int j = 1; j <= 4; j++) { //здесь нужно заменить 20
ifstream jedi;
string path = "/home/danila/thread1output/" + to_string(j); //открываем файлы сс промежуточными результатами, полученными от каждого потока
jedi.open(path);
string rita;
while (getline(jedi, rita))
{
result << rita << endl; //переписываем результаты из промежуточных файлов в конечный
}
jedi.close();
}
result.close();
//конец thread
return 0;
}