-  . - , , (, ), .

, :

( );

      ;

      -;

      ;

      ..

, .

, (threads) ̠ ( , ),  .

, - ( ), .

2.2.

( ), () :

      ( )

     

.

- .



. , race condition ( ).

p1 I = I+J,

p2 - I = I-K

:

Load R1,I Load R1,I

Load R2,J Load R2,K

Add R1,R2 Sub R1,R2

Store R1,I Store R1,I

.

堠 堠 .


 :

      ;

      , , , ;

      ( );

      .

.

1. ( , ).

2. (MONO, MULTI).

.

.

(1968).

int turn;

boolean flag[2 ];

proc( int i )

{

while (TRUE)

{

<>;

enter_region( i );

< >;

leave_region( i );

}

}

void enter_region( int i )

{

try: flag[ i ]=TRUE;

while (flag [( i+1 ) % 2])

{

if ( turn = = i ) continue;

flag[ i ] = FALSE;

while ( turn != i );

goto try;

}

}

void leave_region( int i )

{

turn = ( i +1 ) % 2;

flag[ i ] = FALSE;

}

turn = 0;

flag[ 0 ] = FALSE;

flag[ 1 ] = FALSE;

proc( 0 ) AND proc( 1 ) /* 2 */

(1981)

int turn;

int flag[ 2 ];

void enter_region(

int i )

{

int other; /* */



other = 1 - i;



flag[ i ] = TRUE;

turn = i;

while (turn = = i && flag[ other ] = = TRUE) /* */;

}

void leave_region(

int i )

{

flag[ i ] = FALSE;

}

 

TEST_and_SET_LOCK.

TSL(r,s): [r = s; s = 1]

- .

enter_region:

tsl reg, flag

cmp reg, #0 /* */

jnz enter_region /* - */

ret

leave_region:

mov flag, #0 /* */

ret

(1965).

- , :

P(s):

[if (s == 0) < >;

else s = s-1;]

V(s):



[if (s == 0) < >;

s = s+1;]

().

-.

- (-, ).

semaphore s = 1;

semaphore full = 0;

semaphore empty = N;



producer() consumer()

{ {



int item; int item;

while (TRUE) while (TRUE)

{ {

produce_item(&item);

P(empty); P(full);

P(s); P(s);



enter_item(item); remove_item(&item);

V(s); V(s);

V(full); V(empty);

consume_item(item);

} }

} }



producer() AND consumer() /* 2 */

.

.

      ;

      ;

      .

. TSL .

- , . Ѡ . " " - , , ,

********** 4

, .