OS_Chapter-1 Process Synchronization

Embed Size (px)

Citation preview

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    1/31

    1

    Prescribed Text Book

    Operating System Principles, Seventh Edition

    By

    Mrs.Anamika Raj,Lecturer,King Khalid University

    Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

    Chapter- one Process Co-ordination

    OPERATING SYSTEMS-2

    Page 1 / 31Page 1 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    2/31

    2

    PROCESS - CONCEPTProcesses executing concurrently in the operating system may be either independent processes or

    co-operating processes. A process is independent if it cannot affect or be affected by the otherprocesses executing in the system. Any process that does not share data with any other process isindependent. A process is co-operating if it can affect or be affected by the other processesexecuting in the system. Reasons for providing an environment that allows process co-operation:

    a) Information sharing Since several users may be interested in the same piece of information, we must provide an environment to allow concurrent access to suchinformation.

    b) Computation speedup If we want a particular task to run faster, we must break it intosub tasks each of which will be executing in parallel with the others. Such a speed up can

    be achieved only if the computer has multiple processing elements.c) Modularity Construct the system in a modular fashion dividing the system functions

    into separate processes or threads.d) Convenience Even an individual user may work on many tasks at the same time.

    Co-operating processes require an inter process communication (IPC) mechanism that allowthem to exchange data and information. There are two fundamental models of inter processcommunication

    1. Shared memory 2. Message Passing

    In the shared memory model, a region of memory that is shared by co-operating processes isestablished. Processes can then exchange information by reading and writing data to sharedregion.

    In the message passing model, communication takes place by means of messages exchangedbetween the co-operating processes.

    Mrs.Anamika Raj,Lecturer,King Khalid University

    Chapter one Process Co-ordination

    Page 2 / 31Page 2 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    3/31

    3

    Message passing is useful for exchanging smaller amounts of data because no conflicts need beavoided. Message passing is also easier to implement than is shared memory for inter computercommunication. Shared memory allows maximum speed and convenience of communication, asit can be done at memory speeds when within a computer. Shared memory is faster than memorypassing as message passing systems are typically implemented using system calls and thusrequire the more time consuming task of kernel intervention. In shared memory systems,system calls are required only to establish shared memory regions. Once shared memory isestablished, all accesses are treated as routine memory accesses and no assistance from thekernel is required.

    Shared Memory Systems

    Inter process communication using shared memory requires communicating processes toestablish a region of shared memory. A shared memory region resides in the address space of theprocess creating the shared memory segment. Other processes that wish to communicate using

    this shared memory segment must attach it to their address space. OS tries to prevent one processfrom accessing another processs memory. Shared memory requires that two or more processesagree to remove the above restriction. They can then exchange information by reading andwriting data in the shared areas. The processes are also responsible for ensuring that they are notwriting to the same location simultaneously.

    Producer Consumer problem which is a paradigm for co-operating processes:

    A producer produces information that is consumed by a consumer process. One solution to theproducer consumer problem uses shared memory. To allow producer and consumer processes to

    run concurrently, we must have available a buffer of items that can be filled by the producer andemptied by the consumer. This buffer will reside in a region of memory that is shared by theproducer and consumer processes. A producer can produce one item while consumer isconsuming another item. The producer and consumer must be synchronized so that the consumerdoes not try to consume an item that has not yet been produced.

    Two types of buffers can be used

    Unbounded buffer : places no limit on the size of the buffer Bounded: assumes a fixed size buffer.

    How the bounded buffer can be used to enable processes to share memory

    Mrs.Anamika Raj,Lecturer

    Chapter-one Process Co-ordination

    Page 3 / 31Page 3 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    4/31

    4

    #define BUFFER_SIZE 10typedef struct{ }item;item buffer[BUFFER_SIZE];int in =0;int out =0;

    The shared buffer is implemented as a circular array with two logical pointer in and out. Thevariable in points to the next free position in the buffer; out points to the first full position in thebuffer. The buffer is empty when in == out; the buffer is full when ((in+1)%BUFFER_SIZE) = =out.

    int itemCount;procedure producer() {

    while (true) {item = produceItem();

    if (itemCount == BUFFER_SIZE) {sleep();

    }putItemIntoBuffer(item);itemCount = itemCount + 1;

    if (itemCount == 1) {wakeup(consumer);

    }}}

    procedure consumer() {while (true) {if (itemCount == 0) {

    sleep();

    Mrs.Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 4 / 31Page 4 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    5/31

    5

    } item = removeItemFromBuffer();itemCount = itemCount - 1;if (itemCount == BUFFER_SIZE - 1) {wakeup (producer);

    }consumeItem(item);

    }} Message passing systems

    The co-operating processes in a shared environment scheme require that these processes share aregion of memory and that the code for accessing and manipulating the shared memory bewritten explicitly by the application programmer. Another way to achieve the same effect is forthe operating system to provide the means for co-operating processes to communicate with eachother via a message passing facility.

    Message passing provides a mechanism to allow processes to communicate and to synchronizetheir actions without sharing the same address space3 and is particularly useful in a distributedenvironment where the communicating processes may reside on different computers connectedby a network.

    A message passing facility provides at least two operations: send (message) and receive(message). Messages sent by a process can be of either fixed or variable size. If only fixed - sizedmessages can be sent, the system level implementation is straight forward. Variable sizedmessages require a more complex system level implementation.

    If processes P and Q want to communicate, they must send messages to and receive messages

    from each other, a communication link must exist between them. This link can be implementedin several ways. Here are several methods for logically implementing a link and the send ()/ receive () operations

    Direct or indirect communication Synchronous or asynchronous communication Automatic or explicit buffering

    Issues related to the above features:

    Naming: Processes that want to communicate must have a way to refer to each other.They can use either direct or indirect communication. Under direct communication, eachprocess that wants to communicate must explicitly name the recipient or sender of thecommunication. The send() and receive() primitives are defined as-

    Send(P, message) send a message to process PReceive(Q, message) receive a message from process Q

    Khalid University

    Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 5 / 31Page 5 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    6/31

    6

    A communication link has the following properties

    A link is established automatically between every pair of processes that want tocommunicate. The processes need to know only each others identity to communicate. A link is associated with exactly two processes.

    Between each pair of processes, there exists exactly one link.

    This scheme exhibits symmetry in addressing; that is both the sender process and receiverprocess must name the other to communicate. A variant of this scheme employs asymmetry inaddressing. Here, only the sender names the recipient; the recipient is not required to name thesender. Here, the send () and receive () primitives are defined as:

    Send (P, message) send a message to process P

    Receive (id, message) receive a message from any process, the variable id is set to the name of the process with which communication has taken place.

    The disadvantage in the above schemes is the limited modularity of the resulting processdefinitions. Changing the identifier of the process may necessitate examining all other processdefinitions. All references to the old identifier must be found so that they can be modified to thenew identifier.

    With indirect communication, the messages are sent to and received from mail boxes or ports.A mail box can be viewed abstractly as an object into which messages can be placed byprocesses and from which messages can be removed. Each mail box has a unique identification.A process can communicate with some other process via a number of different mail boxes. Two

    processes can communicate only if the processes have a shared mail box. The send () andreceive() primitives are defined as:

    Send (A, message) send a message to mail box A

    Receive (A, message) receive a message from mail box A.

    In this scheme, a communication link has the following properties

    A link is established between a pair of processes only if both members of the pair have ashared mail box.

    A link may be associated with more than two processes.Between each pair of communicating processes, there may be a number of different links,with each link corresponding to one mail box.

    If we have three processes P1, P2 and P3 sharing a mail box A, process P1 sends a messageto A while P2 and P3 execute receive from A. Then the decision of which process (P2 or P3)will receive a message from P1 depends on the following methods-

    Khalid University

    Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 6 / 31Page 6 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    7/31

    7

    Allow a link to be associated with two processes at most.Allow at most one process at a time to execute receive () operation.Allow the system to select arbitrarily which process will receive the message. Thesystem may identify the receiver to the sender.

    A mail box may be owned either by a process or by the OS. If the mail box is owned by aprocess, then we distinguish between the owner and the user. When a process that owns the mailbox terminates, the mail box disappears. Any process that subsequently sends a message to thismail box must be notified that the mail box no longer exists.

    A mail box owned by the OS is independent and is not attached to any particular process. TheOS then must provide a mechanism that allows a process to do the following:

    Create a new mail boxSend and receive messages through the mail boxDelete a mail box

    The process that creates a new mail box is that mail boxs owner by default. The owner is theonly process that can receive messages through this mail box.

    Synchronization: Communication between processes takes place through calls to send ()and receive () primitives. Message passing may be either blocking or non blocking also known as synchronous and asynchronous .

    Blocking send: The sending process is blocked until the message is received bythe receiving process or the mail box.Non blocking send: The sending process sends the message and resumesoperation.Blocking receive: The receiver blocks until a message is available.Non blocking receive: The receiver retrieves either a valid message or a null.

    When both send () and receive () are blocking, we have a rendezvous between the sender andthe receiver.

    Buffering: Whether the communication is direct or indirect, messages exchanged bycommunicating processes reside in a temporary queue. Such queues can be implemented

    in three ways Zero capacity: The queue has the maximum length of zero; thus the link cannothave any messages waiting in it.Bounded capacity: The queue has finite length n; thus, at most n messages canreside in it.Unbounded capacity: The queues length is infinite; thus any number of messagescan wait in it. The sender never blocks.

    Khalid University

    Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 7 / 31Page 7 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    8/31

    8

    The zero capacity buffer is sometimes referred to as a message system with no buffering; theother cases are referred to as systems with automatic buffering.

    Khalid University

    Anamika Raj

    Chapter -one Process Co-ordination

    Page 8 / 31Page 8 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    9/31

    9

    SYNCHRONIZATIONA co operating process is one that can affect or be affected by other processes executing in the

    system. Co- operating processes can either directly share a logical address space or be allowed toshare data only through files or messages. The former is achieved through the use of light weightprocesses or threads. Concurrent access to shared data may result in data inconsistency.

    A bounded buffer could be used to enable processes to share memory. While considering thesolution for producer consumer problem, it allows at most BUFFER_SIZE 1 items in thebuffer. If the algorithm can be modified to remedy this deficiency, one possibility is to add aninteger variable counter initialized to 0. Counter is incremented every time a new item is addedto the buffer and is decremented every time, an item is removed from the buffer.

    Although both the producer and consumer routines are correct separately, they may not functioncorrectly when executed concurrently.

    When several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place is called racecondition . To guard against the race condition, we need to ensure that only one process at a t imecan be manipulating the variable counter. Hence processes must be synchronized.

    The Critical Section Problem

    Each process in a system has a segment of code called a critical section in which the process maybe changing common variables, updating a table, writing a file etc. The important feature of thesystem is that when one process is executing in its critical section, no other process is to beallowed to execute in its critical section that is no two processes are executing in their criticalsections at the same time. The critical section problem is to design a protocol that the processescan use to co- operate. Each process must request permission to enter its critical section. Thesection of code implementing this request is the entry section. The critical section may befollowed by an exit section. The remaining code is the remainder section.

    Khalid University

    Mrs.Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 9 / 31Page 9 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    10/31

    10

    A critical section is a piece of code that only one thread can execute at a time. If multiple threadstry to enter a critical section, only one can run and the others will sleep.

    Imagine you have three threads that all want to enter a critical section.

    Only one thread can enter the critical section; the other two have to sleep. When a thread sleeps,its execution is paused and the OS will run some other thread.

    Once the thread in the critical section exits, another thread is woken up and allowed to enter thecritical section.

    Khalid University

    Mrs.Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 10 / 31Page 10 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    11/31

    11

    A solution to the critical section problem must satisfy the following three requirements:

    Mutual exclusion: If a process is executing in critical section, then no other processcan be executing in their critical section.Progress: If no process is executing in its critical section and some processes wish toenter their critical sections, then only those processes that are not executing in their

    remainder sections can participate in the decision on which will enter its criticalsection next and this selection cannot be postponed indefinitely.Bounded waiting: There exists a bound or limit on the number of times that otherprocesses are allowed to enter their critical section after a process has made a requestto enter its critical section and before that request is granted.

    At any given point of time, many kernel mode processes may be active in the OS. Two generalapproaches used to handle critical sections in OS are:

    Pre emptive kernels: allows a process to be pre empted while it is running in kernelmode; a kernel mode process will run until it exits kernel mode, blocks or voluntarilyyields control of the CPU

    Non pre emptive kernels: does not allow a process running in kernel mode to be preempted. A non pre emptive kernel is free from race conditions on kernel data structuresas only one process is active in the kernel.

    Petersons solution

    A classic software based solution to the critical section problem is known as Petersons solution.It provides a good algorithmic description of solving the critical section problem and illustratessome of the complexities involved in designing software that addresses the requirements of mutual exclusion, progress and bounded waiting requirements.

    Petersons solution is restricted to two processes that alternate execution between their criticalsections and remainder sections. Petersons solution requires two data items to be sharedbetween the two processes:

    int turn; boolean flag[2];

    Khalid University

    Mrs.Anamika Raj

    Chapter -one Process Co-ordination

    Page 11 / 31Page 11 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    12/31

    12

    The variable turn indicates whose turn it is to enter its critical section. The flag array is used toindicate if a process is ready to enter its critical section.

    Shared variables- boolean flag[2];

    initially flag [0] = flag [1] = false.- flag [i] = true _ Pi ready to enter its critical section

    do {flag [i]:= true;

    turn = j;while (flag [j] and turn == j) ;

    critical sectionflag [i] = false;

    remainder section} while (1);

    The algorithm does satisfy the three essential criteria to solve the critical section problem. Thethree criteria are mutual exclusion, progress, and bounded waiting.

    For two processes P0 and P1:

    Mutual exclusion

    P0 and P1 can never be in the critical section at the same time: If P0 is in its critical section, thenflag [0] is true and either flag [1] is false (meaning P1 has left its critical section) or turn is 0(meaning P1 is just now trying to enter the critical section, but graciously waiting). In both cases,P1 cannot be in critical section when P0 is in critical section.

    Progress

    A process cannot immediately re-enter the critical section if the other process has set its flag tosay that it would like to enter its critical section.

    Bounded waiting

    In Peterson's Algorithm, a process will not wait longer than one turn for entrance to the criticalsection: After giving priority to the other process, this process will run to completion and set itsflag to 0, thereby allowing the other process to enter the critical section.

    Synchronization Hardware

    Any solution to the critical section problem requires a simple tool called a lock. Race conditionsare prevented by requiring that critical regions are protected by locks that is a process mustacquire a lock before entering a critical section; it releases the lock when it exits the criticalsection.

    Khalid University

    Mrs.Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 12 / 31Page 12 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    13/31

    13

    But design of such locks can be sophisticated.

    Hardware features can make any programming task easier and improve system efficiency. Thecritical section problem can be solved simply in a uni processor environment if we could preventinterrupts from occurring while a shared variable was being modified. Then we can ensure thatthe current sequence of instructions would be allowed to execute in order without pre emption.No other instructions would be run, so no unexpected modifications could be made to the sharedvariable. This solution is not feasible in a multi processor environment. Disabling interrupts on amulti processor can be time consuming, as the message is passed to all the processors.

    This message passing delays entry into each critical section and system efficiency decreases.Many modern computer systems provide special hardware instructions that allow us either to testand modify the content of a word or to swap the contents of two words atomically that is as oneuninterruptable unit.

    The TestAndSet () instruction can be defined as above. This instruction is executed atomically.Thus, if two TestAndSet () instructions are executed simultaneously each on a different CPU,they will be executed sequentially in some order.

    Khalid University

    Mrs.Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 13 / 31Page 13 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    14/31

    14

    The Swap () instruction operates on the contents of two words

    Definition of Swap Instruction

    Mutual Exclusion implementation with the Swap() function

    This is also executed atomically. If the machine supports Swap() instruction, then mutualexclusion can be provided by using a global Boolean variable lock initialized to false. Eachprocess has a local Boolean variable key.

    But these algorithms do not satisfy the bounded waiting requirement.

    The below algorithm satisfies all the critical section problems:

    Common data structures used in this algorithm are:

    Boolean waiting[n];

    Boolean lock;

    Both these data structures are initialized to false. For proving that the mutual exclusionrequirement is met, we must make sure that process P i can enter its critical section only if eitherwaiting[i] == false or key == false. The value of key can become false only if the TestAndSet()is executed.

    Khalid University

    Mrs.Anamika Raj,Lecturer

    Chapter -one Process Co-ordination

    Page 14 / 31Page 14 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    15/31

    Chapter 5 Process Co-ordination

    15

    Semaphores

    The various hardware based solutions to the critical section problem are complicated for

    application programmers to use. To overcome this difficulty, we can use a synchronization toolcalled a semaphore.

    A semaphore S is an integer variable that is accessed only through standard atomic operations:wait() and signal().

    Wait: Signal:

    Wait(S) signal(S)

    { {

    While S

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    16/31

    Chapter 5 Process Co-ordination

    16

    Usage

    OSs distinguish between counting and binary semaphores. The value of a counting semaphorecan range over an unrestricted domain. The value of a binary semaphore can range only between0 and 1. Binary semaphores are known as mutex locks as they are locks that provide mutualexclusion.

    Binary semaphores are used to deal with the critical section problem for multiple processes.Counting semaphores can be used to control access to a given resource consisting of a finitenumber of instances. The semaphore is initialized to the number of resources available. Eachprocess that wishes to use a resource performs a wait() operation on the semaphore. When aprocess releases a resource, it performs a signal() operation.

    Count = 0 -> all resources are being used

    Semaphores can be used to solve various synchronization problems.Simple binary semaphore example with two processes:

    Two processes are both vying for the single semaphore. Process A performs the acquire first andtherefore is provided with the semaphore. The period in which the semaphore is owned by theprocess is commonly called a critical section . The critical section can be performed by only one

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 16 / 31Page 16 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    17/31

    Chapter 5 Process Co-ordination

    17

    process, therefore the need for the coordination provided by the semaphore. While Process A hasthe semaphore, Process B is not permitted to perform its critical section.

    Note that while Process A is in its critical section, Process B attempts to acquire the semaphore.As the semaphore has already been acquired by Process A, Process B is placed into a blocked

    state. When Process A finally releases the semaphore, it is then granted to Process B, which isallowed to enter its critical section. Process B at a later time releases the semaphore, making itavailable for acquisition.

    In the counting semaphore example, each process requires two resources before being able toperform its desired activities. In this example, the value of the counting semaphore is 3, whichmeans that only one process will be permitted to fully operate at a time. Process A acquires itstwo resources first, which means that Process B blocks until Process A releases at least one of itsresources.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 17 / 31Page 17 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    18/31

    Chapter 5 Process Co-ordination

    18

    Implementation

    The main disadvantage of the semaphore is that it requires busy waiting . While a process is inits critical section, any other process that tries to enter its critical section must loop continuouslyin the entry code. Busy waiting wastes CPU cycles that some other process might be able to use

    productively. This type of semaphore is called a spinlock because the process spins whilewaiting for the lock. To overcome, the need for busy waiting the definition of wait () and signal()semaphore operations can be modified. When a process executes the wait() operation and findsthat the semaphore value is not positive, it must wait. Rather than engaging in busy waiting, theprocess can block itself. The block operation places a process into a waiting queue associatedwith the semaphore and the state of the process is switched to the waiting state. Then control istransferred to CPU scheduler which selects another process to execute.

    A process that is blocked waiting on a semaphore S, should be restarted when some otherprocess executes a signal() operation. The process is restarted by a wakeup() operation which

    changes the process from the waiting state to the ready state. Process is then placed in the readyqueue.

    Each semaphore has an integer value and a list of processes list. When a process must wait on asemaphore, it is added to the list of processes. A signal () operation removes one process fromthe list of waiting processes and awakens that process.

    A signal() operation removes one process from the list of waiting processes and awakens thatprocess.

    Wait(semaphore DIAGRAM

    Signal(semaphore DIAGRAM

    The block() operation suspends the process that invokes it. The wakeup(P) operation resumes theexecution of a blocked process P. These two operations are provided by the OS as basic systemcalls.

    But this implementation may have negative semaphore values. The list of waiting processes canbe easily implemented by a link field in each PCB. Each semaphore contains an integer valueand a pointer to a list of PCBs. One way to add and remove processes from the list in a way thatensures bounded waiting is to use a FIFO queue where the semaphore contains the head and tailpointer to the queue.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 18 / 31Page 18 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    19/31

    Chapter 5 Process Co-ordination

    19

    The critical aspect of semaphores is that they be executed atomically. No two processes canexecute wait() and signal() operations on the same semaphore at the same time. This is a criticalsection problem and in a single CPU environment, this can be solved by simply inhibitinginterrupts during the time the wait() and signal() operations are executing. But in a multiprocessor environment, interrupts must be disabled on every processor. Disabling interrupts onevery processor can be a difficult task and diminishes performance. Hence SMP systems mustprovide alternate locking techniques such as spin locks to ensure that wait() and signal() areperformed atomically.

    Deadlocks and Starvation

    The implementation of a semaphore with a waiting queue may result in a situation where two ormore processes are waiting indefinitely for an event (execution of a signal()) that can be causedonly by one of the waiting processes. When such a state is reached, these processes are said to bedeadlocked .

    A set of processes is in a dead lock state when every process in the set is waiting for an eventthat can be caused only by another process in the set. These events are resource acquisition andrelease.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 19 / 31Page 19 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    20/31

    Chapter 5 Process Co-ordination

    20

    Another problem related to deadlocks is indefinite blocking or starvation , a situation in whichprocesses wait indefinitely within the semaphore. Indefinite blocking may occur if we add andremove processes from the list associated with a semaphore in LIFO order.

    Classic problems of synchronization

    These synchronization problems are examples of large class of concurrency control problems. Insolutions to these problems, we use semaphores for synchronization.

    The Bounded Buffer problem

    Here the pool consists of n buffers, each capable of holding one item. The mutex semaphoreprovides mutual exclusion for accesses to the buffer pool and is initialized to the value 1. Theempty and full semaphores count the number of empty and full buffers. The semaphore empty isinitialized to the value n, the semaphore full is initialized to value 0.

    The code below can be interpreted as the producer producing full buffers for the consumer or asthe consumer producing empty buffers for the producer.

    The structure of the producer process

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 20 / 31Page 20 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    21/31

    Chapter 5 Process Co-ordination

    21

    The structure of the consumer process

    The Readers Writers Problem

    A data base is to be shared among several concurrent processes. Some of these processes maywant only to read the database (readers) whereas others may want to update the database(writers). If two readers access the shared data simultaneously, no adverse effects will result. If a

    writer and some other thread, access the database simultaneously, problems occur.

    To prevent these problems, writers have exclusive access to the shared database. Thissynchronization problem is referred to as readers writers problem.

    The simplest readers writers problem requires that no reader will be kept waiting unless a writerhas already obtained permission to use the shared object. No reader should wait for other readersto finish simply because a writer is waiting.

    The second readers writers problem requires that once a writer is ready, that writer performs itswrite as soon as possible, that is if a writer is waiting to access the object, no new readers maystart reading.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 21 / 31Page 21 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    22/31

    Chapter 5 Process Co-ordination

    22

    In the solution to the first readers writers problem, the reader processes share the followingdata structures:

    Semaphore mutex, wrt;

    Int readcount;

    Semaphores mutex and wrt are initialized to 1, readcount is initialized to 0. Semaphore wrt iscommon to both reader and writer processes. The mutex semaphore is used to ensure mutualexclusion when the variable readcount is updated. The readcount variable keeps track of howmany processes are currently reading the object. The semaphore wrt functions as a mutualexclusion semaphore for the writers. It is also used by the first or last reader that enters or exitsthe critical section. It is not used by readers who enter or exit while other readers are in their

    critical section.The readers writers problem and its solutions has been generalized to provide reader writerlocks on some systems. Acquiring the reader writer lock requires specifying the mode of thelock, either read or write access. When a process only wishes to read shared data, it requests thereader writer lock in read mode; a process wishing to modify the shared data must request thelock in write mode. Multiple processes are permitted to concurrently acquire a reader writerlock in read mode, only one process may acquire the lock for writing as exclusive access isrequired for writers.

    Reader writer locks are useful in the following situations:

    - In applications where it is easy to identify which processes only read shared data andwhich threads only write shared data.

    - In applications that have more readers than writers.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 22 / 31Page 22 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    23/31

    Chapter 5 Process Co-ordination

    23

    Dining Philosophers Problem

    Consider five philosophers who spend their lives thinking and eating. The philosophers share acircular table surrounded by five chairs, each belonging to one philosopher. In the center of thetable is a bowl of rice, and the table is laid with five single chop sticks. When a philosopherthinks, she does not interact with her colleagues. From time to time, a philosopher gets hungryand tries to pick up the two chopsticks that are closest to her. A philosopher may pick up onlyone chopstick at a time. She cannot pick up a chopstick that is already in the hand of the

    neighbor. When a hungry philosopher has both her chopsticks at the same time, she eats withoutreleasing her chop sticks. When she is finished eating, she puts down both of her chop sticks andstarts thinking again.

    The dining philosophers problem is considered a classic synchronization problem as it anexample of a large class of concurrency control problems.

    One simple solution is to represent each chop stick with a semaphore. A philosopher tries to graba chop stick by executing a wait () operation on that semaphore; she releases her chop sticks byexecuting the signal () operation on the appropriate semaphores. Thus, the shared data are

    Semaphore chopstick [5];

    where all elements of chopstick are initialized to 1.This solution is rejected as it could create adead lock. Suppose that all five philosophers become hungry simultaneously and each grabs herleft chop stick. All the elements of chop stick will now be equal to 0. When each philosophertries to grab her right chopstick, she will be delayed forever.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 23 / 31Page 23 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    24/31

    Chapter 5 Process Co-ordination

    24

    Solution to dining philosophers problem:

    - Allow at most four philosophers to be sitting simultaneously at the table- Allow a philosopher to pick up her chopsticks only if both chopsticks are available.- Use an asymmetric solution; that is, an odd philosopher picks up first her left chopstick

    and then her right chopstick whereas an even philosopher picks up her right chopstick and then her left chopstick.

    Monitors

    Although semaphores provide a convenient and effective mechanism for processsynchronization, using them incorrectly can result in timing errors that are difficult to detectsince these errors happen only if some particular execution sequences take place and thesesequences do not always occur.

    * Suppose that a process interchanges the order in which the wait () and signal () operationson the semaphore mutex are executed.

    Signal (mutex);. Critical section.. Wait (mutex);

    Here several processes may be executing in their critical sections simultaneously, violating themutual exclusion requirement.

    * Suppose that a process replaces signal (mutex) with wait (mutex) that is it executesWait(mutex); Critical section. Wait(mutex);

    Here a deadlock will occur.

    * Suppose that a process omits the wait(mutex), or the signal(mutex) or both. Here, either

    mutual exclusion is violated or a dead lock will occur.To deal with such errors, a fundamental high level synchronization construct called monitor typeis used.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 24 / 31Page 24 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    25/31

    Chapter 5 Process Co-ordination

    25

    Usage

    A monitor type presents a set of programmer defined operations that are provided mutualexclusion within the monitor. The monitor type also contains the declaration of variables whosevalues define the state of an instance of that type, along with the bodies of the procedures orfunctions that operate on those variables. The representation of a monitor type cannot be useddirectly by the various processes. Thus, a procedure defined within a monitor can access onlythose variables declared locally within the monitor and its formal parameters. The local variablesof a monitor can be accessed by only the local procedures.

    The monitor construct ensures that only one process at a time can be active within the monitor.But this monitor construct is not powerful for modeling some synchronization schemes. For thiswe need additional synchronization mechanisms. These mechanisms are provided by conditionconstruct. The only operations that can be invoked on a condition variable are wait() andsignal(). The operation x.wait() means that the process invoking this operation is suspended untilanother process invokes x.signal(). The x.signal() operation resumes exactly one suspendedprocess.

    When x.signal() operation is invoked by a process P, there is a suspended process Q associated

    with condition x. If suspended process Q is allowed to resume its execution, the signalingprocess P must wait. Otherwise, both P and Q would be active simultaneously within the monitor

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 25 / 31Page 25 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    26/31

    Chapter 5 Process Co-ordination

    26

    However, both processes can conceptually continue with their execution. Two possibilities exist:

    1. Signal and wait P either waits until Q leaves the monitor or waits for another condition.2. Signal and condition Q either waits until P leaves the monitor or waits for another

    condition.

    Dining Philosophers Solution using Monitors

    This solution imposes the restriction that a philosopher may pick up her chopsticks only if bothof them are available. To code this solution, we need to distinguish among three states in whichwe may find a philosopher. For this purpose, we use this data structure

    enum {thinking, hungry, eating } state[5];

    Philosopher i can set the variable state[i] = eating only if her two neighbors are not eating: (state[(i+4) % 5]! = eating) and (state [(i+1)%5]!=eating)

    Also declare condition self [5];

    where philosopher i can delay herself when she is hungry but is unable to obtain the chop sticksshe needs. The distribution of the chopsticks is controlled by the monitor dp. Each philosopherbefore starting to eat, must invoke the operation pickup(). This may result in the suspension of the philosopher process. After the successful completion of the operation the philosopher mayeat. Following this, the philosopher invokes the putdown() operation. Thus philosopher i mustinvoke the operations pickup() and putdown() in the following sequence:

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 26 / 31Page 26 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    27/31

    Chapter 5 Process Co-ordination

    27

    dp.pickup(i);

    .

    Eat

    .

    Dp.putdown(i);

    This solution ensures that no two neighbors are eating simultaneously and that no deadlocks willoccur.

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 27 / 31Page 27 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    28/31

    Chapter 5 Process Co-ordination

    28

    Monitor solution to Dining Philosophers problem

    Implementing a Monitor using Semaphores

    For each monitor, a semaphore mutex initialized to 1 is provided. A process must executewait(mutex) before entering the monitor and must execute(signal) after leaving the monitor.

    Since a signaling process must wait until the resumed process either leaves or waits, anadditional semaphore next initialized to 0, on which the signaling processes may suspendthemselves. An integer variable next_count is used to count the number of processes suspendedon next. Thus, each external procedure P is replaced by

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 28 / 31Page 28 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    29/31

    Chapter 5 Process Co-ordination

    29

    Mutual exclusion within a monitor is thus ensured.

    Implementing condition variables:

    For each condition x, we introduce a semaphore x_sem and an integer variable x_count, bothinitialized to 0.

    The operations x.wait() and x.signal() can be implemented as

    Resuming Processes Within a Monitor

    If several processes are suspended on condition x, and an x.signal() operation is executed bysome process, then for determining which suspended process should be resumed next, we useFCFS ordering so that the process waiting the longest is resumed first. Or conditional waitconstruct() can be used as

    x.wait(c);

    www.jntuworld.com

    Khalid University

    Page 29 / 31Page 29 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    30/31

    Chapter 5 Process Co-ordination

    30

    where c is an integer expression that is evaluated when the wait() operation is executed. Thevalue of c which is called a priority number is then stored with the name of the process that issuspended. When x.signal() is executed, the process with the smallest associated priority numberis resumed next.

    The resource allocator monitor controls the allocation of a single resource among competingprocesses. Each process, when requesting an allocation of this resource specifies the maximumtime it plans to use the resource. The monitor allocates the resource to the process that has theshortest time- allocation request. A process that needs access to the resource must follow thissequence

    R.acquire(t);

    ..

    Access the resource;

    R.release();

    Where R is an instance of type Resource Allocator

    But the following problems can occur

    A process might access a resource without first gaining access permission to the resourceA process might never release a resource once it has been granted access to the resourceA process might attempt to release a resource that it never requested.A process might request the same resource twice

    Khalid University

    Mrs.Anamika Raj,Lecturer Page 30 / 31Page 30 / 31

  • 7/29/2019 OS_Chapter-1 Process Synchronization

    31/31

    Chapter 5 Process Co-ordination

    One possible solution to the current problem is to include the resource access operations withinthe Resource Allocator monitor.

    Atomic Transactions

    Please refer to this website for information about Atomic Transactionshttp://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/6_Synchronization.html

    Khalid University

    http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/6_Synchronization.htmlhttp://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/6_Synchronization.htmlhttp://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/6_Synchronization.html