Սինխրոնիզացիան Linux ՕՀ - ում. mutex – արգելափակում է մուտքը դեպի ռեսուրսը mutex – արգելափակում է մուտքը դեպի ռեսուրսը join – սպասում է ուրիշ հոսքերի.

Презентация:



Advertisements
Похожие презентации
Հոսքերը Windows ՕՀ - ում. Ներածություն Ներածություն Հոսքի ստեղծումը Հոսքի ստեղծումը Հոսքի ավարտը Հոսքի ավարտը Հոսքի ստեկը Հոսքի ստեկը Հոսքերի առաջնայնությունը.
Advertisements

Conditional Statements. Program control statements modify the order of statement execution. Statements in a C program normally executes from top to bottom,
Virtual memory Linux. Ներածություն Kernel and user space work with virtual addresses (also called linear addresses) that are mapped to physical addresses.
Животные из Красной Книги
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.
S5-1 PAT328, Section 5, September 2004 Copyright 2004 MSC.Software Corporation SECTION 5 RESULTS TITLE EDITOR.
It is an argument, collision of people. The reasons of conflicts : People want different things; They have different ideas and their values are different;
a) free treatment only in hospitals b) private treatment c) free treatment in hospitals and outside.
© 2006 Avaya Inc. All rights reserved. Programmable Buttons and features.
A Bill is a proposal for a new law, or a proposal to change an existing law that is presented for debate before Parliament. Bills are introduced in either.
Yogesh Mehla Now concept of logic building is not so complex and not so simple. We will not work on how to make logic program in.
Ways to Check for Divisibility Vüsal Abbasov Dividing By 1 All numbers are divisible by 1.
Gradient In simple terms, the variation in space of any quantity can be represented (eg graphically) by a slope. The gradient represents the steepness.
1/13 Chapter 06- Implementing Operators in a class.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
1 © Luxoft Training 2012 Inner and anonymous classes.
Unit II Constructor Cont… Destructor Default constructor.
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Sec. 2.3: Apply Deductive Reasoning Deductive Reasoning uses facts, definitions, accepted properties, and the laws of logic to form a logical argument.
Транксрипт:

Սինխրոնիզացիան Linux ՕՀ - ում

mutex – արգելափակում է մուտքը դեպի ռեսուրսը mutex – արգելափակում է մուտքը դեպի ռեսուրսը join – սպասում է ուրիշ հոսքերի աշխատանքի ավարտին join – սպասում է ուրիշ հոսքերի աշխատանքի ավարտին condition variables – իրականացնում է սինխրոնիզացիան pthread_cond_t- ի միջոցով condition variables – իրականացնում է սինխրոնիզացիան pthread_cond_t- ի միջոցով

Mutex Ինչ է մյուտեքսը ? Ինչ է մյուտեքսը ? Մյուտեքսները Լինուքս ՕՀ - ում օգտագործվում են միևնույն պրոցեսի հոսքերի սինխրոնիզացիայի ժամանակ : Նրանք չեն օգտագործվում պրոցեսների սինխրոնիզացիայի համար : Մյուտեքսները Լինուքս ՕՀ - ում օգտագործվում են միևնույն պրոցեսի հոսքերի սինխրոնիզացիայի ժամանակ : Նրանք չեն օգտագործվում պրոցեսների սինխրոնիզացիայի համար :

Օրինակ 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 ); }

Հոսքերի ստեղծումը 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; }

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;......

Condition Variables Պայմանի փոփոխականը pthread_cond_t տիպի փոփոխական է Պայմանի փոփոխականը pthread_cond_t տիպի փոփոխական է Պայմանի փոփոխականը պետք է օգտագործվի մյուտեքսի հետ միասին, որպեսզի չառաջանա race condition Պայմանի փոփոխականը պետք է օգտագործվի մյուտեքսի հետ միասին, որպեսզի չառաջանա race condition

Օրինակ … 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); …

Օրինակ 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 ); … } … }

Օրինակ 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 ); …