Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторная работа 4 Пантелеева БСТ1904.docx
Скачиваний:
67
Добавлен:
04.03.2022
Размер:
348.78 Кб
Скачать

Void parent(lpctstr parent_mail, lpctstr child_mail)

{

InitializeCriticalSection(&cs);

printf("\nUnsorted massiv :\n");

int arr[siz];

ifstream fin;

string filename = "C:\\Users\\kseny\\Desktop\\os\\os_maislot\\os_maislot\\matrix.txt";

fin.open(filename.c_str(), std::ifstream::in);

if (!fin.is_open())

printf("\nFile couldn't be open!\n");

else

{

for (int i = 0; i < siz; i++)

{

if (fin.eof())

{

printf("\nNot enoght numbers in file!\n");

delete arr;

return;

}

fin >> arr[i];

printf("%f", arr[i]);

}

fin.close();

}

int arr1[siz] = {};

int count = 0;

HANDLE parent_handle = create_mail(parent_mail);

printf("/n");

EnterCriticalSection(&cs);

int x = 1;

int b[siz];

for (int i = 0; i < siz; i++) b[i] = 400;

divide(arr, x, b);

for (int i = 0; i < siz; i++) printf("%f", b[i]);

printf("\n");

system("start C:\\Users\\kseny\\Desktop\\os\\secondary1\\Debug\\secondary1.exe");

HANDLE child_handle;

while (!join_mail(child_mail, child_handle)) {}

char message[512];

int_to_char(b, message);

printf("\n1 sending...\n");

send_message(child_handle, message);

while (!read_message(parent_handle, message)) {}

printf("\nHave 1 result\n");

char_to_mas_int(message, b);

for (int i = 0; i < siz; i++)

{

if (b[i] != 400)

{

arr1[count] = b[i];

count++;

}

}

CloseHandle(child_handle);

LeaveCriticalSection(&cs);

EnterCriticalSection(&cs);

int x1 = 10;

for (int i = 0; i < siz; i++) b[i] = 400;

divide(arr, x1, b);

for (int i = 0; i < siz; i++) printf("%f", b[i]);

printf("\n");

system("start C:\\Users\\kseny\\Desktop\\os\\secondary1\\Debug\\secondary1.exe");

while (!join_mail(child_mail, child_handle)) {}

int_to_char(b, message);

printf("\n2 sending...\n");

send_message(child_handle, message);

while (!read_message(parent_handle, message)) {}

printf("\nHave 2 result\n");

char_to_mas_int(message, b);

for (int i = 0; i < siz; i++)

{

if (b[i] != 400)

{

arr1[count] = b[i];

count++;

}

}

CloseHandle(child_handle);

LeaveCriticalSection(&cs);

EnterCriticalSection(&cs);

int x2 = 100;

for (int i = 0; i < siz; i++) b[i] = 400;

divide(arr, x2, b);

for (int i = 0; i < siz; i++) printf("%f", b[i]);

printf("\n");

system("start C:\\Users\\kseny\\Desktop\\os\\secondary1\\Debug\\secondary1.exe");

while (!join_mail(child_mail, child_handle)) {}

int_to_char(b, message);

printf("\n3 sending...\n");

send_message(child_handle, message);

while (!read_message(parent_handle, message)) {}

printf("\nHave 3 result\n");

char_to_mas_int(message, b);

for (int i = 0; i < siz; i++)

{

if (b[i] != 400)

{

arr1[count] = b[i];

count++;

}

}

CloseHandle(child_handle);

LeaveCriticalSection(&cs);

CloseHandle(parent_handle);

printf("\nSorted massiv: \n");

printArray(arr1);

printf("\n");

DeleteCriticalSection(&cs);

}

Void child(handle parent_handle, lpctstr parent_mail, lpctstr child_mail)

{

HANDLE child_handle = create_mail(child_mail);

char message[1024];

cout << "Waiting input..." << endl;

while (!read_message(child_handle, message)) {}

cout << "Long message: " << strlen(message) << endl;

CloseHandle(child_handle);

cout << "Handle close" << endl;

int b[siz];

char_to_mas_int(message, b);

cout << "Having massiv:" << endl;

for (int i = 0; i < siz; i++) cout << b[i] << " ";

cout << endl << endl;

int temp;

for (int i = 0; i < siz - 1; i++) {

for (int j = 0; j < siz - i - 1; j++) {

if (b[j] > b[j + 1]) {

temp = b[j];

b[j] = b[j + 1];

b[j + 1] = temp;

}

}

}

printf("\nResult = \n");

printArray(b);

cout << endl;

int_to_char(b, message);

cout << "Sending result..." << endl;

send_message(parent_handle, message);

cout << "Done" << endl;

}