Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
ASSIGNMENT : 3
NAME:
MUHAMMAD BAQAR KAZMI
ROLL NO:
16094119-055
SUBJECT:
OperatingSystem
SUBMISSION DATE:
25MAY 2018
Q1:Critical Section Problem
A critical section is a code segment that can be accessed by only one process at a time. Critical
section contains shared variables which need to be synchronized to maintain consistency of
data variables. In the entry section, the process requests for entry in the Critical Section.
The critical section problem refers to the problem of how to ensure that at most one process is
executing its critical section at a given time.
Any solution to the critical section problem must satisfy three requirements:
• Mutual Exclusion : If a process is executing in its critical section, then no other
process is allowed to execute in the critical section.
• Progress : If no process is in the critical section, then no other process from outside
can block it from entering the critical section.
• Bounded Waiting :Abound must exist on the number of times that other processes
are allowed to enter their critical sections after a process has made a request to
enter its critical section and before that request is granted.
Q2:Peterson’sSolution
Peterson’s Solution is a classical software based solution to the critical section problem.
In Peterson’s solution, we have two shared variables:
 boolean flag[i] :Initialized to FALSE, initially no one is interested in entering the critical
section
 int turn : The process whose turn is to enter the critical section.
Peterson’s Solution preserves all three conditions :
 Mutual Exclusion is assured as only one process can access the critical section at any time.
 Progress is also assured, as a process outside the critical section does not blocks other
processes from entering the critical section.
 Bounded Waiting is preserved as every process gets a fair chance.
Disadvantages of Peterson’s Solution
 It involves Busy waiting
 It is limited to 2 processes.
Q3: Hardware synchronization.
Many systems provide hardware support for critical section code. A single processor or
uniprocessor systemcould disableinterrupts by executing currently running code without
preemption, which is very inefficient on multiprocessor systems. Problems of Critical
Section are alsosolvable by hardware Modern machines provide specialatomic hardware
instruction.
• Another advanced approach is the Atomic Instructions (Non Interruptible instructions)
• It Test and modify the content of a word atomically.
• Atomically swap two variables
do {
[acquire lock]
critical section
[release lock]
remainder section
}
while (TRUE);
• Test and modify the content of a word atomically
boolean TestAndSet (boolean &target) {
boolean rv = target;
target = true;
return rv;
}
Q4: Semaphores
A Semaphore is an integer variable, which can be accessed only through two operations wait()
and signal().
FORMAT:
wait ( mutex ); // Mutual exclusion: mutex init to 1.
CRITICAL SECTION
signal( mutex );
REMAINDER
There are two types of semaphores : Binary Semaphores and Counting Semaphores
 Binary Semaphores : They can only be either 0 or 1. They are also known as mutex locks,
as the locks can provide mutual exclusion. All the processes can share the same mutex
semaphore that is initialized to 1. Then, a process has to wait until the lock becomes 0.
Then, the process can make the mutex semaphore 1 and start its critical section. When it
completes its critical section, it can reset the value of mutex semaphore to 0 and some
other process can enter its critical section.
 Counting Semaphores : They can have any value and are not restricted over a certain
domain. They can be used to control access a resource that has a limitation on the number
of simultaneous accesses. The semaphore can be initialized to the number of instances of
the resource. Whenever a process wants to use that resource, it checks if the number of
remaining instances is more than zero, i.e., the process has an instance available. Then, the
process can enter its critical section thereby decreasing the value of the counting
semaphore by 1. After the process is over with the use of the instance of the resource, it
can leave the critical section thereby adding 1 to the number of available instances of the
resource.
PROPERTIES
1. Simple
2. Works with many processes
3. Can have many different critical sections with different semaphores
4. Each critical section has unique access semaphores
5. Can permit multiple processes into the critical section at once, if desirable.
Q5: Classical Problems of Synchronization
Semaphore can be used in other synchronization problems besides Mutual Exclusion. Below are
some of the classical problemdepicting flaws of process synchronization in systems where
cooperating processes are present.
Bounded Buffer Problem
 This problem is generalized in terms of the Producer Consumer problem, where
a finite buffer pool is used to exchange messages between producer and consumer
processes.
Because the buffer pool has a maximum size, this problem is often called the Bounded
buffer problem.
 Solution to this problem is, creating two counting semaphores "full" and "empty" to keep
track of the current number of full and empty buffers respectively.
The Readers Writers Problem
 In this problem there are some processes(called readers) that only read the shared data,
and never change it, and there are other processes(called writers) who may change the
data in addition to reading, or instead of reading it.
 There are various type of readers-writers problem, most centred on relative priorities of
readers and writers.
Dining Philosophers Problem
 The dining philosopher's problem involves the allocation of limited resources to a group of
processes in a deadlock-free and starvation-free manner.
 There are five philosophers sitting around a table, in which there are five chopsticks/forks
kept beside them and a bowl of rice in the centre, When a philosopher wants to eat, he
uses two chopsticks - one from their left and one from their right. When a philosopher
wants to think, he keeps down both chopsticks at their original place.
Q6:Monitors
A high-level abstraction that provides a convenient and effective mechanism for process
synchronization.Only one process may be active within the monitor at a time monitor monitor-
name.In concurrent programming, a monitor is a synchronization construct that allows threads
to have both mutual exclusion and the ability to wait (block) for a certain condition to become
true. Monitors also have a mechanism for signaling other threads that their condition has
been met.
 A monitor is essentially a class, in which all data is private, and with the special
restriction that only one method within any given monitor object may be active at the
same time.
 An additional restriction is that monitor methods may only access the shared data
within the monitor and any data passed to them as parameters, i.e. they cannot access
any data external to the monitor
Q7: SYNCHRONIZATIONEXAMPLES
Process manager
This problem deals with a Process class to serve as the core of a process manager for a
multiprogrammed kernel. The Process class maintains parent/child/sibling relationships among
processes, and coordinates the Exec (or fork), Exit, and Join (or wait) system calls. p-
>Birth(Process* parent) registers this newly created process p as a child of its parent.
 p->Death(int status) indicates that this process p has exited with the specified exit
status.
 int status = child->Join() waits for this child process to exit (i.e., to call Death), and
returns the child’s exit status.
Each Process object P has an associated thread. The thread bound
to P calls Birth and Death on P as part of the implementation of the Exec and Exit systemcalls
respectively. Join on P may be called only by the thread bound to the parent of P. You do not
need to create these threads or enforce these restrictions; they are intended to simplify the
problem. Death on P waits until two conditions are satisfied: (1) the children of P have exited,
and (2) the parent of P has exited or has executed a Join on P. The intent is that
the Process object for P may be safely deleted after Death returns. Your solution should
represent the process tree with the following state for each Process object P: (1) a list of the
children of P, (2) a pointer to the parent of P, and (3) P’s exit status if P has exited. Be sure that
your solution is free of deadlock and dangling references.
Spinlocks withLoad-Lockedand Store-Conditional
The Alpha and MIPS 4000 processor architectures have no atomic read-modify-write
instructions, i.e., no test-and-set-lock instruction (TS). Atomic update is supported by pairs
of load_locked (LDL) and store-conditional (STC) instructions. The semantics of the Alpha
architecture’s LDL and STC instructions are as follows. Executing an LDL Rx, y instruction loads
the memory at the specified address (y) into the specified general register (Rx), and holds y in a
special per-processor lock register. STC Rx, y stores the contents of the specified general
register (Rx) to memory at the specified address (y), but only if y matches the address in the
CPU’s lock register.
BoundedBuffer
A producer/consumer Bounded Buffer is the basis for pipes. BoundedBuffer has two
primitives: Read(n) and Write(n). (Ordinarily these calls would transfer n bytes of data out of or
into the buffer, but ignore that for purposes of this problem. The BoundedBuffer has a
maximum size of N bytes. Read blocks if the buffer is empty, and Write blocks if the buffer is
full. A Read or Write with n > N is legal, but it will always block the caller at least
once.Implement BoundedBuffer using mutexes and condition variables.

More Related Content

Critical section operating system

  • 1. ASSIGNMENT : 3 NAME: MUHAMMAD BAQAR KAZMI ROLL NO: 16094119-055 SUBJECT: OperatingSystem SUBMISSION DATE: 25MAY 2018
  • 2. Q1:Critical Section Problem A critical section is a code segment that can be accessed by only one process at a time. Critical section contains shared variables which need to be synchronized to maintain consistency of data variables. In the entry section, the process requests for entry in the Critical Section. The critical section problem refers to the problem of how to ensure that at most one process is executing its critical section at a given time. Any solution to the critical section problem must satisfy three requirements: • Mutual Exclusion : If a process is executing in its critical section, then no other process is allowed to execute in the critical section. • Progress : If no process is in the critical section, then no other process from outside can block it from entering the critical section. • Bounded Waiting :Abound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Q2:Peterson’sSolution Peterson’s Solution is a classical software based solution to the critical section problem. In Peterson’s solution, we have two shared variables:  boolean flag[i] :Initialized to FALSE, initially no one is interested in entering the critical section
  • 3.  int turn : The process whose turn is to enter the critical section. Peterson’s Solution preserves all three conditions :  Mutual Exclusion is assured as only one process can access the critical section at any time.  Progress is also assured, as a process outside the critical section does not blocks other processes from entering the critical section.  Bounded Waiting is preserved as every process gets a fair chance. Disadvantages of Peterson’s Solution  It involves Busy waiting  It is limited to 2 processes. Q3: Hardware synchronization. Many systems provide hardware support for critical section code. A single processor or uniprocessor systemcould disableinterrupts by executing currently running code without preemption, which is very inefficient on multiprocessor systems. Problems of Critical Section are alsosolvable by hardware Modern machines provide specialatomic hardware instruction. • Another advanced approach is the Atomic Instructions (Non Interruptible instructions) • It Test and modify the content of a word atomically. • Atomically swap two variables
  • 4. do { [acquire lock] critical section [release lock] remainder section } while (TRUE); • Test and modify the content of a word atomically boolean TestAndSet (boolean &target) { boolean rv = target; target = true; return rv; } Q4: Semaphores
  • 5. A Semaphore is an integer variable, which can be accessed only through two operations wait() and signal(). FORMAT: wait ( mutex ); // Mutual exclusion: mutex init to 1. CRITICAL SECTION signal( mutex ); REMAINDER There are two types of semaphores : Binary Semaphores and Counting Semaphores  Binary Semaphores : They can only be either 0 or 1. They are also known as mutex locks, as the locks can provide mutual exclusion. All the processes can share the same mutex semaphore that is initialized to 1. Then, a process has to wait until the lock becomes 0. Then, the process can make the mutex semaphore 1 and start its critical section. When it completes its critical section, it can reset the value of mutex semaphore to 0 and some other process can enter its critical section.  Counting Semaphores : They can have any value and are not restricted over a certain domain. They can be used to control access a resource that has a limitation on the number of simultaneous accesses. The semaphore can be initialized to the number of instances of the resource. Whenever a process wants to use that resource, it checks if the number of remaining instances is more than zero, i.e., the process has an instance available. Then, the process can enter its critical section thereby decreasing the value of the counting semaphore by 1. After the process is over with the use of the instance of the resource, it can leave the critical section thereby adding 1 to the number of available instances of the resource. PROPERTIES 1. Simple 2. Works with many processes 3. Can have many different critical sections with different semaphores 4. Each critical section has unique access semaphores 5. Can permit multiple processes into the critical section at once, if desirable.
  • 6. Q5: Classical Problems of Synchronization Semaphore can be used in other synchronization problems besides Mutual Exclusion. Below are some of the classical problemdepicting flaws of process synchronization in systems where cooperating processes are present. Bounded Buffer Problem  This problem is generalized in terms of the Producer Consumer problem, where a finite buffer pool is used to exchange messages between producer and consumer processes. Because the buffer pool has a maximum size, this problem is often called the Bounded buffer problem.  Solution to this problem is, creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.
  • 7. The Readers Writers Problem  In this problem there are some processes(called readers) that only read the shared data, and never change it, and there are other processes(called writers) who may change the data in addition to reading, or instead of reading it.  There are various type of readers-writers problem, most centred on relative priorities of readers and writers. Dining Philosophers Problem  The dining philosopher's problem involves the allocation of limited resources to a group of processes in a deadlock-free and starvation-free manner.  There are five philosophers sitting around a table, in which there are five chopsticks/forks kept beside them and a bowl of rice in the centre, When a philosopher wants to eat, he uses two chopsticks - one from their left and one from their right. When a philosopher wants to think, he keeps down both chopsticks at their original place.
  • 8. Q6:Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization.Only one process may be active within the monitor at a time monitor monitor- name.In concurrent programming, a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. Monitors also have a mechanism for signaling other threads that their condition has been met.  A monitor is essentially a class, in which all data is private, and with the special restriction that only one method within any given monitor object may be active at the same time.  An additional restriction is that monitor methods may only access the shared data within the monitor and any data passed to them as parameters, i.e. they cannot access any data external to the monitor Q7: SYNCHRONIZATIONEXAMPLES Process manager This problem deals with a Process class to serve as the core of a process manager for a multiprogrammed kernel. The Process class maintains parent/child/sibling relationships among processes, and coordinates the Exec (or fork), Exit, and Join (or wait) system calls. p- >Birth(Process* parent) registers this newly created process p as a child of its parent.
  • 9.  p->Death(int status) indicates that this process p has exited with the specified exit status.  int status = child->Join() waits for this child process to exit (i.e., to call Death), and returns the child’s exit status. Each Process object P has an associated thread. The thread bound to P calls Birth and Death on P as part of the implementation of the Exec and Exit systemcalls respectively. Join on P may be called only by the thread bound to the parent of P. You do not need to create these threads or enforce these restrictions; they are intended to simplify the problem. Death on P waits until two conditions are satisfied: (1) the children of P have exited, and (2) the parent of P has exited or has executed a Join on P. The intent is that the Process object for P may be safely deleted after Death returns. Your solution should represent the process tree with the following state for each Process object P: (1) a list of the children of P, (2) a pointer to the parent of P, and (3) P’s exit status if P has exited. Be sure that your solution is free of deadlock and dangling references. Spinlocks withLoad-Lockedand Store-Conditional The Alpha and MIPS 4000 processor architectures have no atomic read-modify-write instructions, i.e., no test-and-set-lock instruction (TS). Atomic update is supported by pairs of load_locked (LDL) and store-conditional (STC) instructions. The semantics of the Alpha architecture’s LDL and STC instructions are as follows. Executing an LDL Rx, y instruction loads the memory at the specified address (y) into the specified general register (Rx), and holds y in a special per-processor lock register. STC Rx, y stores the contents of the specified general register (Rx) to memory at the specified address (y), but only if y matches the address in the CPU’s lock register. BoundedBuffer A producer/consumer Bounded Buffer is the basis for pipes. BoundedBuffer has two primitives: Read(n) and Write(n). (Ordinarily these calls would transfer n bytes of data out of or into the buffer, but ignore that for purposes of this problem. The BoundedBuffer has a maximum size of N bytes. Read blocks if the buffer is empty, and Write blocks if the buffer is
  • 10. full. A Read or Write with n > N is legal, but it will always block the caller at least once.Implement BoundedBuffer using mutexes and condition variables.