Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 8 лет назад пользователемПавел Селиванов
1 Սինխրոնիզացիան Linux ՕՀ - ում
2 mutex – արգելափակում է մուտքը դեպի ռեսուրսը mutex – արգելափակում է մուտքը դեպի ռեսուրսը join – սպասում է ուրիշ հոսքերի աշխատանքի ավարտին join – սպասում է ուրիշ հոսքերի աշխատանքի ավարտին condition variables – իրականացնում է սինխրոնիզացիան pthread_cond_t- ի միջոցով condition variables – իրականացնում է սինխրոնիզացիան pthread_cond_t- ի միջոցով
3 Mutex Ինչ է մյուտեքսը ? Ինչ է մյուտեքսը ? Մյուտեքսները Լինուքս ՕՀ - ում օգտագործվում են միևնույն պրոցեսի հոսքերի սինխրոնիզացիայի ժամանակ : Նրանք չեն օգտագործվում պրոցեսների սինխրոնիզացիայի համար : Մյուտեքսները Լինուքս ՕՀ - ում օգտագործվում են միևնույն պրոցեսի հոսքերի սինխրոնիզացիայի ժամանակ : Նրանք չեն օգտագործվում պրոցեսների սինխրոնիզացիայի համար :
4 Օրինակ pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter=0; pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter=0; void function () { void function () { pthread_mutex_lock( &mutex1 ); pthread_mutex_lock( &mutex1 ); counter++; counter++; pthread_mutex_unlock( &mutex1 ); pthread_mutex_unlock( &mutex1 ); }
5 Հոսքերի ստեղծումը pthread_create- ի միջոցով void functionC() { … } pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; int main() { int rc1, rc2; pthread_t thread1, thread2; rc1=pthread_create( &thread1, NULL, &functionC, NULL); rc2=pthread_create( &thread2, NULL, &functionC, NULL); pthread_join( thread1, NULL); pthread_join( thread2, NULL); return 0; }
6 Join Երբ մի հոսքը սպասում է մինչև մյուս հոսքը ավարտի աշխատանքը Երբ մի հոսքը սպասում է մինչև մյուս հոսքը ավարտի աշխատանքը pthread_t thread_id[NTHREADS]; pthread_t thread_id[NTHREADS]; int i, j; int i, j; for(i=0; i < NTHREADS; i++) for(i=0; i < NTHREADS; i++) pthread_create( &thread_id[i], NULL, thread_function, NULL ); pthread_create( &thread_id[i], NULL, thread_function, NULL ); for(j=0; j < NTHREADS; j++) for(j=0; j < NTHREADS; j++) pthread_join( thread_id[j], NULL); pthread_join( thread_id[j], NULL); /* Now that all threads are complete I can print the final result. */ /* Now that all threads are complete I can print the final result. */ /* Without the join I could be printing a value before all the threads */ /* Without the join I could be printing a value before all the threads */ /* have been completed. */ /* have been completed. */ std::cout << finally done\n; std::cout << finally done\n;......
7 Condition Variables Պայմանի փոփոխականը pthread_cond_t տիպի փոփոխական է Պայմանի փոփոխականը pthread_cond_t տիպի փոփոխական է Պայմանի փոփոխականը պետք է օգտագործվի մյուտեքսի հետ միասին, որպեսզի չառաջանա race condition Պայմանի փոփոխականը պետք է օգտագործվի մյուտեքսի հետ միասին, որպեսզի չառաջանա race condition
8 Օրինակ … int count = 0; int count = 0; pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER; pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER; void *functionCount1(); void *functionCount1(); void *functionCount2(); void *functionCount2(); … pthread_t thread1, thread2; pthread_t thread1, thread2; pthread_create( &thread1, NULL, &functionCount1, NULL); pthread_create( &thread1, NULL, &functionCount1, NULL); pthread_create( &thread2, NULL, &functionCount2, NULL); pthread_create( &thread2, NULL, &functionCount2, NULL); pthread_join( thread1, NULL); pthread_join( thread1, NULL); pthread_join( thread2, NULL); pthread_join( thread2, NULL); …
9 Օրինակ void functionCount1() void functionCount1() { … { … pthread_mutex_lock( &count_mutex ); pthread_mutex_lock( &count_mutex ); // Wait while functionCount2() operates on count mutex // Wait while functionCount2() operates on count mutex // unlocked if condition // unlocked if condition // varialbe in functionCount2() // varialbe in functionCount2() // signaled. // signaled. pthread_cond_wait( &condition_var, &count_mutex ); pthread_cond_wait( &condition_var, &count_mutex ); count++; count++; pthread_mutex_unlock( &count_mutex ); pthread_mutex_unlock( &count_mutex ); … } … }
10 Օրինակ void functionCount2() { … void functionCount2() { … pthread_mutex_lock( &count_mutex ); pthread_mutex_lock( &count_mutex ); if( count COUNT_HALT2 ) { if( count COUNT_HALT2 ) { // Condition of if statement has been met. // Condition of if statement has been met. // Signal to free waiting thread by freeing the // Signal to free waiting thread by freeing the // mutex. Note: functionCount1() is now // mutex. Note: functionCount1() is now // permitted to modify "count". // permitted to modify "count". pthread_cond_signal( &condition_var ); pthread_cond_signal( &condition_var ); } else { } else { count++; count++; } … pthread_mutex_unlock( &count_mutex ); pthread_mutex_unlock( &count_mutex ); …
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.