Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

9-ameliy_paradigma_qq

.pdf
Скачиваний:
0
Добавлен:
18.01.2026
Размер:
248.22 Кб
Скачать

9-ámeliy jumıs. Parallel programmalastırıw

Jumıstıń maqseti: Parallel programmalastırıwdıń tiykarǵı túsiniklerin (Aǵımlar - Threads, Sinxronizaciya - Synchronization, Jarısıwshı jaǵday - Race Condition) úyreniw. C++ tilindegi std::thread kitapxanası hám OpenMP (yamasa basqa qural) járdeminde kóp aǵımlı (multithreaded) programmalar dúziw kónlikpesin qáliplestiriw. Máselelerdi parallel orınlaw arqalı programmanıń islew tezligin arttırıwdı hám resurslardı bólistiriwdi úyreniw.

Teoriyalıq bólim:

Parallel programmalastırıw – bul esaplaw máselesin bir neshe bóleklerge bólip, olardı bir waqıtta orınlaw usılı.

Tiykarǵı túsinikler:

1.Process: Yadda óz ornına iye bolǵan ǵárezsiz programma.

2.Aǵım (Thread): Process ishindegi kishi orınlanıw birligi. Aǵımlar process yadı menen ulıwma paydalanadı.

3.Jarısıwshı jaǵday (Race Condition): Eki yamasa onnan kóp aǵım bir waqıtta bir ózgeriwshini ózgertpekshi bolǵanda payda bolatuǵın qáte.

4.Mutex (Mutual Exclusion): "Óz-ara shıǵarıp taslaw" - bir waqıtta tek bir aǵımǵa resursqa kiriwge ruqsat beriwshi qural (Qulp).

5.Deadlock (Tuyıqqa tireliw): Aǵımlar biri-birinen resurs bosatıwın kútip, toqtap qalıw jaǵdayı.

ÁMELIY BÓLIM:

C++11 standartınan baslap kirgizilgen <thread> hám <mutex> kitapxanalarınan paydalanamız.

1-mısal: Ápiwayı aǵımlar jaratıw

Másele: Baslı aǵım (Main) menen birge qosımsha aǵımda jumıs islew.

Kod:

#include <iostream>

#include <thread> // Aǵım kitapxanası #include <chrono> // Waqıt (uyqılaw ushın) using namespace std;

// Aǵım orınlaytuǵın funkciya void jumis() {

for (int i = 0; i < 5; i++) {

cout << "Aǵım islep atır: " << i << endl; this_thread::sleep_for(chrono::seconds(1)); // 1 sekund kútiw

}

}

int main() {

cout << "Main baslandı." << endl;

//Jańa aǵım jaratıp, 'jumis' funkciyasın iske túsiremiz thread t1(jumis);

//Main aǵım óz jumısın dawam ettiredi

for (int i = 0; i < 3; i++) {

cout << "Main islep atır: " << i << endl; this_thread::sleep_for(chrono::seconds(1));

}

// t1 aǵımınıń juwmaqlanıwın kútemiz (birlestiriw) t1.join();

cout << "Main juwmaqlandı." << endl; return 0;

}

Túsindirme: t1.join() metodı main aǵımdı t1 tamamlanǵansha toqtatıp turadı. Eger join() qoyılmasa, main pitip qalǵanda t1 aǵımı da májbúriy toqtatıladı hám qáte shıǵıwı múmkin.

2-mısal: Jarısıwshı jaǵday (Race Condition) hám Mutex

Másele: Kóp aǵım bir esaplaǵıshtı (counter) arttırǵandaǵı qáte hám onı ońlaw.

Kod (Qáte variant):

#include <iostream> #include <thread> #include <vector> using namespace std;

int counter = 0; // Ulıwma resurs

void increment() {

for (int i = 0; i < 100000; i++) {

counter++; // Qáwipli operaciya (atomarlıq emes)

}

}

int main() {

thread t1(increment); thread t2(increment); t1.join(); t2.join();

cout << "Nátiyje: " << counter << endl; // Kútilgen: 200000, Haqıyqıy: <

200000 return 0;

}

Kod (Durıs variant - Mutex penen): #include <iostream>

#include <thread>

#include <mutex> // Mutex kitapxanası using namespace std;

int counter = 0;

mutex mtx; // Qulp (Lock) void increment() {

for (int i = 0; i < 100000; i++) {

mtx.lock();

// Resursqa kiriwdi qulplaymız

counter++;

// Qáwipsiz operaciya

mtx.unlock(); // Qulıptı ashamız

}

}

//... main funkciyası birdey ...

//Nátiyje: 200000 (Bárhama durıs)

3-mısal: Parametrli aǵımlar

Másele: Aǵımǵa maǵlıwmat beriw.

Kod:

#include <iostream> #include <thread> #include <string> using namespace std;

void salemlesiw(string at, int jas) {

cout << "Sálem, " << at << "! Seniń jasıń " << jas << " da.\n";

}

int main() {

string ati = "Azamat"; int jasi = 25;

// Parametrlerdi virgul arqalı beremiz thread t1(salemlesiw, ati, jasi);

thread t2(salemlesiw, "Gúlparshın", 22);

t1.join();

t2.join(); return 0;

}

4-mısal: Parallel esaplaw (Massiv summası)

Másele: Úlken massivtiń qosındısın eki aǵımǵa bólip esaplaw.

Kod:

#include <iostream> #include <vector> #include <thread>

#include <numeric> // accumulate using namespace std;

// Bólek qosındını esaplawshı funkciya

void sum_part(const vector<int>& data, int start, int end, long long& result)

{

long long s = 0;

for (int i = start; i < end; i++) { s += data[i];

}

result = s; // Nátiyjeni sırtqı ózgeriwshige jazamız

}

int main() {

int N = 1000000;

vector<int> data(N, 1); // 1 000 000 dana 1 sannan turatuǵın massiv long long sum1 = 0, sum2 = 0;

// Jumıstı teń ekige bólemiz

thread t1(sum_part, ref(data), 0, N/2, ref(sum1)); thread t2(sum_part, ref(data), N/2, N, ref(sum2));

t1.join();

t2.join();

long long total = sum1 + sum2;

cout << "Ulıwma summa: " << total << endl;

return 0;

}

Túsindirme: ref() funkciyası ózgeriwshini silteme (reference) retinde beriw ushın kerek, sebebi aǵımlar ádette nusqa (copy) menen isleydi.

ÁMELIY TAPSÍRMALAR

1-tapsırma: 3 aǵım menen sálemnama.

Úsh aǵım (t1, t2, t3) jaratıń.

1-aǵım: "A aǵımı iske tústi" dep jazsın.

2-aǵım: "B aǵımı iske tústi" dep jazsın.

3-aǵım: "C aǵımı iske tústi" dep jazsın.

Aǵımlar qaysı tártipte shıqqanın baqlań (tártibi kepillik berilmegen).

2-tapsırma: Bankomat (Mutex).

Bank esabınan (balance = 1000) bir waqıtta 5 adam (5 aǵım) hár qaysısı 200 somnan sheshiwge háreket etsin. Mutex qollanıp, balans teris bolıp ketpewin támiyinleń.

3-tapsırma: Matrica kóbeytiw (Parallel).

Kishkene (mısalı 4x4) matricanıń hárbir qatarın bólek aǵım menen qayta isleń.

(Yamasa ápiwayı: Matricanıń barlıq elementlerin 2 ge kóbeytiwdi 4 aǵımǵa bóliń).

4-tapsırma: Jarıs (Race Simulation).

5 avtomobil (aǵım) jarısıp atır. Hárbir aǵım random waqıt (1-3 sek) uyqılasın (sleep). Qaysı aǵım birinshi oyansa, sol "Finishke jetti!" dep jazsın. (Mutex qollanıp, ekranǵa jazıwdı retlestiriń).

5-tapsırma: Deadlock simulyaciyası.

Eki aǵım hám eki resurs (mutex1, mutex2) jaratıń.

1-aǵım: mutex1 di qulplaydı, kutedi, mutex2 ni qulplawǵa háreket etedi.

2-aǵım: mutex2 ni qulplaydı, kutedi, mutex1 di qulplawǵa háreket etedi.

Programmanıń "qatıp qalıwın" (Deadlock) baqlań hám onı qalay sheshiwge bolatuǵının túsindiriń (mısalı, qulplaw tártibin birdey etiw).

Соседние файлы в предмете Programmalastiriw paradigmalari