How to implement preferred read and write for * nix processes

There is a Unix function with a name flock()that processes can use to gain access to a resource (access) or access (“write”). The problem is that he is starving those processes that request exclusive access. Such a request remains in the queue until there are no processes containing a common lock; Meanwhile, new requests for a general lock are provided to them “in front” of the process awaiting an exclusive lock.

It is clear that the more processes request shared locks, the longer the writer will have to wait for this random time window, in which outstanding shared locks are not stored.

The behavior I'm looking for is this: after the writer has requested an exclusive lock, subsequent readers who request a shared lock will be queued for the writer. As I was told, the name of this type of lock is "write preferable / write lock".

In particular, there are several posts ( this one ) that address this question, but at the thread level. I need this Unix / Linux oriented solution to coordinate processes this way.

UPDATE . I need the solution to handle the possibility that the participation process may fail during a lock by automatically removing the lock.

+4
2

, , . , pthread_rwlock_t , , , pthread_rwlockattr_setpshared() pthread_rwlockattr_t, pthread_rwlock_t PTHREAD_PROCESS_SHARED.

reset, , - . , System V ( XIS IPC) , reset , . System V Linux.

, - ++, . , , , System V IPC. , - Sys V. , , . , 0, . , 0, .

+2

, , (.. , ): double flock() . , fd_sh fd_ex. , :

  • flock (fd_ex, LOCK_SH) - ,
  • flock (fd_sh, LOCK_SH) - .
  • flock (fd_ex, LOCK_UN) - ,
  • flock (fd_sh, LOCK_UN)

:

  • flock (fd_ex, LOCK_EX) -
  • flock (fd_sh, LOCK_EX) - .
  • flock (fd_sh, LOCK_UN) - , ( , )
  • flock (fd_ex, LOCK_UN)

, fd_ex , fd_sh , , , . , 1 , 2 , . , , ​​, , , , ( ​​ ) , , 1-3, ( , ).

- , , . . , , fd_ex . . .

: , pthread_rwlock ( PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP attr). 8 ( pthread_rwlock), /, .

1 - Intel Core2 Debian Linux 3.16.36:

                                 90% reads/10% writes
                 single flock        double flock         pthread_rwlock
total overhead*     90.4%                21.7%                 11.6%
readers waittime*   20.2%                50.1%                 47.1%
writers waittime    95.2%                64.5%                 54.8%

                                 50% reads/50% writes
                 single flock        double flock         pthread_rwlock
total overhead      22.0%                33.7%                  3.2%
readers waittime    63.6%                82.2%                 82.7%
writers waittime    87.8%                84.0%                 70.3%

                                 10% reads/90% writes
                 single flock        double flock         pthread_rwlock
total overhead       5.3%                 8.4%                   0.2%
readers waittime    82.5%                87.2%                  96.8%
writers waittime    87.3%                87.4%                  78.5%

" " - ( " " - " " )/ " ", - , ;

'waittime' - " " /( " " + " " ), ,

2 - 16 (32 HT, Intel Xeon) Debian Linux 3.19.6:

                                 90% reads/10% writes
                 single flock        double flock         pthread_rwlock
total overhead      134.3%               17.1%                 11.6%
readers waittime    13.2%                46.4%                 45.8%
writers waittime    96.7%                65.3%                 54.3%

                                 50% reads/50% writes
                 single flock        double flock         pthread_rwlock
total overhead      37.9%                30.5%                  2.9%
readers waittime    46.1%                78.4%                 83.1%
writers waittime    90.5%                85.9%                 70.0%

                                 10% reads/90% writes
                 single flock        double flock         pthread_rwlock
total overhead       7.2%                 9.0%                   0.4%
readers waittime    66.9%                80.6%                  96.8%
writers waittime    88.0%                87.9%                  78.4%

, . . . , . Pthread rwlock . , , .

. "usleep (10000 + rand()% 1000)". clock_gettime (CLOCK_MONOTONIC). ( ) (1), , . .

0

Source: https://habr.com/ru/post/1568797/


All Articles