• Basic Mechanisms Of Flow Synchronization
  • Problems With Implementation Of Blocking




    Download 6,34 Mb.
    Pdf ko'rish
    bet56/203
    Sana10.01.2024
    Hajmi6,34 Mb.
    #134102
    1   ...   52   53   54   55   56   57   58   59   ...   203
    Bog'liq
    Linux This Book Includes 4 Manuscripts The Underground Bible

    Problems With Implementation Of Blocking
    Consider a naive implementation of a critical section using block variables. 
    With each critical section associated integer variable, which assumes the
    value as a unit during freezing and zero after forging. Here's what the code
    for this implementation looks like:
    int lock = 0;
    void aquire_lock (int lock)
    {
    // if lock does not have ( lock == 0), roll it (set lock = 1)
    // and exit - we are in the critical section
    // otherwise wait until the lock is removed
    while (lock! = 0); // (1)
    lock = 1; // (2)
    }
    void release_lock (int lock)
    {
    // remove the lock
    lock = 0;
    }
    The main problem with this implementation is that checking the value of
    the locking variable and changing it is not part of a single atomic operation.


    Basic Mechanisms Of Flow Synchronization
    Modern OSes provide a wide range of ready-made synchronization
    mechanisms. Synchronization mechanisms  are operating system tools that
    help to solve the main task of synchronization - to ensure the coordination
    of flows that work with shared data. If such tools are the minimum blocks
    for building multithreaded programs, they are called synchronization
    primitives.
    Synchronization primitives are divided into the following main categories:

    Versatile, low-level, which can be used in different ways (
    semaphores )
    ❖ 
    Simple, low-level, each of which is adapted to the solution
    ❖ 
    High-level universal expressed through simple; this group includes
    the concept of a monitor which can be expressed through mutexes
    and conditional variables
    ❖ 
    High level, adapted to the solution of a specific synchronization
    problem (blocking read-write and barriers)
    Let us look at the different mechanisms and evaluate the advantages that
    each of them has, as well as their possible disadvantages. To further
    illustrate the material, let's take a classic example that demonstrates the
    need for synchronization - the problem of producer-consumers or the
    problem of limited buffer ( bounded buffer ).
    The formulation of the problem is simple. Suppose we have production
    threads and consumer threads. The manufacturer creates objects, the
    consumer receives them. The task is to synchronize their work so that the
    consumer cannot attempt to retrieve objects that have not yet been created,
    and the manufacturer cannot create more objects than the consumer can.
    To synchronize flows, place a buffer of fixed length n between the
    manufacturer and the consumer. The manufacturer can place objects on the
    buffer, the consumer - take them from there. If the consumer takes the


    object, it is removed from the buffer. There are several requirements to be
    met:
    1. When the manufacturer or consumer is working with the
    buffer, the rest of the threads should wait for it to finish
    2. When the manufacturer is trying to put an about object in
    the buffer and the buffer is full, it must wait until it appears
    in place
    3. When a consumer tries to pick an about object of the buffer
    and the buffer is empty, it must wait until it is on ' appears
    about ' object

    Download 6,34 Mb.
    1   ...   52   53   54   55   56   57   58   59   ...   203




    Download 6,34 Mb.
    Pdf ko'rish

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Problems With Implementation Of Blocking

    Download 6,34 Mb.
    Pdf ko'rish