Os QB
Os QB
The critical section refers to the segment of code where processes access shared
resources, such as common variables and files, and perform write operations on them.
Example:
2. Explain methods of handling deadlock
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and
circular wait holds simultaneously
Circular wait One way to ensure that this condition never hold is to impose a total
ordering of all resource types and to require that each process requests resource in an
increasing order of enumeration.
2. Deadlock Avoidance :
This approach allows the three necessary conditions of deadlock but makes judicious
choices to assure that deadlock point is never reached. It allows more concurrency
than avoidance detection
A decision is made dynamically whether the current resource allocation request will, if
granted, potentially lead to deadlock. It requires the knowledge of future process
requests. Two techniques to avoid deadlock :
1. Process initiation denial
2. Resource allocation denial
Advantages of deadlock avoidance techniques :
Not necessary to pre-empt and rollback processes
Less restrictive than deadlock prevention
Disadvantages :
Future resource requirements must be known in advance
Processes can be blocked for long periods
Exists fixed number of resources for allocation
3. Deadlock Detection :
Deadlock detection is used by employing and algorithm that tracks the circular waiting
and killing one or more processes so that deadlock is removed. The system state is
examined periodically to determine if a set of processes is deadlocked. A deadlock is
resolved by aborting and restarting a process, relinquishing all the resources that the
process held.
This technique doe not limit resources access or restrict process action.
Requested resources are granted to processes whenever possible.
It never delays the process initiation and facilitates online handling.
The disadvantage is the inherent pre-emption losses
To prevent a deadlock the OS must eliminate one of the four necessary conditions 1.
Mutual exclusion 2. Hold and wait 3. No preemption 4. Circular wait
2. Hold and wait : If a process holding certain resources is denied a further request, it
must release its original resources and if required request them again.
4. Circular wait : Circular wait can be bypassed if the operating system prevents the
formation of a circle.
• A deadlock is possible only if all four of these conditions simultaneously hold in the
system.
• Prevention strategies ensure that at least one of the conditions is always false.
4. Explain bankers algorithm for deadlock prevention
5. Explain bounded buffer problem
Bounded buffer problem, which is also called producer consumer problem, is
one of the classic problems of synchronization. Let's start by understanding the
problem here, before moving on to the solution and program code.
There is a buffer of n slots and each slot is capable of storing one unit of data. There are
two processes running, namely, producer and consumer, which are operating on the
buffer.
A producer tries to insert data into an empty slot of the buffer. A consumer tries to
remove data from a filled slot in the buffer. As you might have guessed by now, those
two processes won't produce the expected output if they are being executed
concurrently.
There needs to be a way to make the producer and consumer work in an independent
manner.
Here's a Solution
One solution of this problem is to use semaphores. The semaphores which will be used
here are:
At any instant, the current value of empty represents the number of empty slots in the
buffer and full represents the number of occupied slots in the buffer.
do
{
wait(empty);
wait(mutex);
signal(mutex);
signal(full);
}
while(TRUE)
Looking at the above code for a producer, we can see that a producer first waits
until there is atleast one empty slot.
Then it decrements the empty semaphore because, there will now be one less
empty slot, since the producer is going to insert data in one of those slots.
Then, it acquires lock on the buffer, so that the consumer cannot access the
buffer until producer completes its operation.
After performing the insert operation, the lock is released and the value of full is
incremented because the producer has just filled a slot in the buffer.
do
{
wait(full);
wait(mutex);
signal(mutex);
signal(empty);
}
while(TRUE);
The consumer waits until there is atleast one full slot in the buffer.
Then it decrements the full semaphore because the number of occupied slots will
be decreased by one, after the consumer completes its operation.
Following that, the consumer completes the removal operation so that the data
from one of the full slots is removed.
There is a shared resource which should be accessed by multiple processes. There are
two types of processes in this context. They are reader and writer. Any number
of readers can read from the shared resource simultaneously, but only one writer can
write to the shared resource. When a writer is writing data to the resource, no other
process can access the resource
The readers-writers problem relates to an object such as a file that is shared between
multiple processes. Some of these processes are readers i.e. they only want to read the
data from the object and some of the processes are writers i.e. they want to write into
the object.
The readers-writers problem is used to manage synchronization so that there are no
problems with the object data. For example - If two readers access the object at the same
time there is no problem. However if two writers or a reader and writer access the object
at the same time, there may be problems.
Reader Process
The code that defines the reader process is given below −
wait (mutex);
rc ++;
if (rc == 1)
wait (wrt);
signal(mutex);
.
. READ THE OBJECT
.
wait(mutex);
rc --;
if (rc == 0)
signal (wrt);
signal(mutex);
In the above code, mutex and wrt are semaphores that are initialized to 1. Also, rc is a
variable that is initialized to 0. The mutex semaphore ensures mutual exclusion and wrt
handles the writing mechanism and is common to the reader and writer process code.
Writer Process
The code that defines the writer process is given below:
wait(wrt);
.
. WRITE INTO THE OBJECT
.
signal(wrt);
If a writer wants to access the object, wait operation is performed on wrt. After that no
other writer can access the object. When a writer is done writing into the object, signal
operation is performed on wrt.
Consider there are five philosophers sitting around a circular dining table. The dining
table has five chopsticks and a bowl of rice in the middle as shown in the below figure.
At any instant, a philosopher is either eating or thinking. 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.
From the problem statement, it is clear that a philosopher can think for an indefinite
amount of time. But when a philosopher starts eating, he has to stop at some point of
time. The philosopher is in an endless cycle of thinking and eating.
while(TRUE)
wait(stick[i]);
wait(stick[(i+1) % 5]);
signal(stick[i]);
signal(stick[(i+1) % 5]);
When a philosopher wants to eat the rice, he will wait for the chopstick at his left and
picks up that chopstick. Then he waits for the right chopstick to be available, and then
picks it too. After eating, he puts both the chopsticks down.
But if all five philosophers are hungry simultaneously, and each of them pickup one
chopstick, then a deadlock situation occurs because they will be waiting for another
chopstick forever. The possible solutions for this are:
A philosopher must be allowed to pick up the chopsticks only if both the left and
right chopsticks are available.
Allow only four philosophers to sit at the table. That way, if all the four
philosophers pick up four chopsticks, there will be one chopstick left on the table.
So, one philosopher can start eating and eventually, two chopsticks will be
available. In this way, deadlocks can be avoided.
Monitors are used for process synchronization. With the help of programming
languages, we can use a monitor to achieve mutual exclusion among the
processes.
The Monitor is a module or package which encapsulates shared data structure,
procedures, and the synchronization between the concurrent procedure
invocations
Characteristics of Monitors.
1. Inside the monitors, we can only execute one process at a time.
2. Monitors offer high-level of synchronization
3. Monitors were derived to simplify the complexity of synchronization problems.
4. There is only one process that can be active at a time inside the monitor.
Components of Monitor
There are four main components of the monitor:
1. Initialization
2. Private data
3. Monitor procedure
4. Monitor entry queue
Initialization: – Initialization comprises the code, and when the monitors are created,
we use this code exactly once.
Private Data: – Private data is another component of the monitor. It comprises all the
private data, and the private data contains private procedures that can only be used
within the monitor. So, outside the monitor, private data is not visible.
Monitor Procedure: – Monitors Procedures are those procedures that can be called
from outside the monitor.
Monitor Entry Queue: – Monitor entry queue is another essential component of the
monitor that includes all the threads, which are called procedures.
Advantages:
1.We can use condition variables only in the monitors.
2 The monitors are comprised of the shared variables and the procedures which operate
the shared variable.
3. Condition variables are present in the monitor.
4. In monitors, wait always block the caller.
A semaphore either allows or disallows access to the resource, which depends on how it
is set up.
Characteristic of Semaphore
Here, are characteristic of a semaphore:
Types of Semaphores
The two common kinds of semaphores are
Counting semaphores
Binary semaphores.
Counting Semaphores
This type of Semaphore uses a count that helps task to be acquired or released
numerous times. If the initial count = 0, the counting semaphore should be created in
the unavailable state.
However, If the count is > 0, the semaphore is created in the available state, and the
number of tokens it has equals to its count.
Binary Semaphores
The binary semaphores are quite similar to counting semaphores, but their value is
restricted to 0 and 1. In this type of semaphore, the wait operation works only if
semaphore = 1, and the signal operation succeeds when semaphore= 0. It is easy to
implement than counting semaphores.
Example of Semaphore
The below-given program is a step by step implementation, which involves usage and
declaration of semaphore.
Program execution
I/O operations
File System manipulation
Communication
Error Detection
Resource Allocation
Protection
Program Execution
The OS loads a program into memory and then executes that program. It also makes
sure that once started that program can end its execution, either normally or forcefully.
The major steps during program management are:
I/O Operation
An I/O subsystem comprises of I/O devices and their corresponding driver software.
Drivers hide the peculiarities of specific hardware devices from the users.
An Operating System manages the communication between user and device drivers.
I/O operation means read or write operation with any file or any specific I/O
device.
Operating system provides the access to the required I/O device when required.
Programs need has to be read and then write them as files and directories. File handling
portion of operating system also allows users to create and delete files by specific name
along with extension, search for a given file and / or list file information. Some programs
comprise of permissions management for allowing or denying access to files or
directories based on file ownership.
Communication
Process needs to swap over information with other process. Processes executing on
same computer system or on different computer systems can communicate using
operating system support. Communication between two processes can be done using
shared memory or via message passing.
Error handling
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices or in
the memory hardware. Following are the major activities of an operating system with
respect to error handling −
Resource allocation
When multiple jobs running concurrently, resources must need to be allocated to each
of them. Resources can be CPU cycles, main memory storage, file storage and I/O
devices. CPU scheduling routines are used here to establish how best the CPU can be
used.
Protection and Security
This is to ensure the safety of the system. Thus, user authentication is required to access
a system. It is also necessary to protect a process from another when multiple processes
are running on a system at the same time.
The OS controls the access to the resources, protects the I/O devices from invalid
access, and provides authentication through passwords.
12. Functions of OS
1. Instruction
2. Input/output Management
3. Memory Management
4. File Management
5. Processor Management
6. Job Priority
7. Special Control Program
8. Scheduling of resources and jobs
9. Security
10. Monitoring activities
11. Job accounting
Input/output Management: What output will come from the input given by the user, the
operating system runs this program. This management involves coordinating various
input and output devices. It assigns the functions of those devices where one or more
applications are executed.
Memory Management: The operating system handles the responsibility of storing any
data, system programs, and user programs in memory. This function of the operating
system is called memory management.
File Management: The operating system is helpful in making changes in the stored files
and in replacing them. It also plays an important role in transferring various files to a
device.
Job Priority: The work of job priority is creation and promotion. It determines what action
should be done first in a computer system.
Special Control Program: The operating systems make automatic changes to the task
through specific control programs. These programs are called Special Control Program.
Security
The OS keeps the system and programs safe and secure through authentication. A user id
and password decide the authenticity of the user.
It allows the user to perform more than one task at a time, each task getting the same
amount of time to execute. Hence, the name time sharing OS. Moreover, it is an extension
of multiprogramming systems. In multiprogramming systems, the aim is to make the
maximum use of the CPU. On the other hand, here the aim is to achieve the minimum
response time of CPU.
It is the division of CPU time for each process when more than one task are given by
the user.
A short duration of time is chosen for each process. Moreover, this time duration is
very small in the order of 10-100 milliseconds. This time duration is known as time
slot, time slice, or quantum.
The Process State diagram illustrates the States in which a process can be in, and it also
defines the flow in which a particular state can be achieved by the Process. Let us first
take a look at the Process State diagram, which is as follows:
1. New
A program which is going to be picked up by the OS into the main memory is called a
new process.
2. Ready
Whenever a process is created, it directly enters in the ready state, in which, it waits for
the CPU to be assigned. The OS picks the new processes from the secondary memory
and put all of them in the main memory.
The processes which are ready for the execution and reside in the main memory are
called ready state processes.
3. Running
One of the processes from the ready state will be chosen by the OS depending upon the
scheduling algorithm.
Block or wait
From the Running state, a process can make the transition to the block or wait state
depending upon the scheduling algorithm or the intrinsic behavior of the process.
5. Completion or termination
When a process finishes its execution, it comes in the termination state. All the context
of the process (Process Control Block) will also be deleted the process will be terminated
by the Operating system.
1. Creation
Once the process is created, it will be ready and come into the ready queue (main
memory) and will be ready for the execution.
2. Scheduling
Out of the many processes present in the ready queue, the Operating system chooses
one process and start executing it. Selecting the process which is to be executed next, is
known as scheduling.
3. Execution
Once the process is scheduled for the execution, the processor starts executing it.
Process may come to the blocked or wait state during the execution then in that case
the processor starts executing the other processes.
4. Deletion/killing
Once the purpose of the process gets over then the OS will kill the process. The Context
of the process (PCB) will be deleted and the process gets terminated by the Operating
system.
Due to the swapping technique performance usually gets affected, but it also helps in
running multiple and big processes in parallel. The swapping process is also known as a
technique for memory compaction. Basically, low priority processes may be swapped
out so that processes with a higher priority may be loaded and executed
The swapping of processes by the memory manager is fast enough that some processes
will be in memory, ready to execute, when the CPU scheduler wants to reschedule the
CPU.
Advantages of Swapping
1. The swapping technique mainly helps the CPU to manage multiple processes
within a single main memory.
2. This technique helps to create and use virtual memory.
3. With the help of this technique, the CPU can perform several tasks
simultaneously. Thus, processes need not wait too long before their execution.
4. This technique is economical.
5. This technique can be easily applied to priority-based scheduling in order to
improve its performance.
Disadvantages of Swapping
The details about each segment are stored in a table called a segment table. Segment
table is stored in one (or many) of the segments.
Types of Segmentation
Characteristics of Segmentation
User authentication process is used just to identify who the owner is or who the
identified person is.In personal computer, generally, user authentication can be perform
using password. When a computer user wants to log into a computer system, then the
installed operating system (OS) on that computer system generally wants to determine
or check who the user is. This process is called as user authentication.
Sometime it is too important to authenticate the user because the computer system
may have some important documents of the owner
User authentication using password is the most widely used form of authenticating the
user.
In this method of authenticating the user with password, it is to require that the user
who is going to authenticate has to type their login name or id and login password
Authenticating the user using their password is an easy method and also easy to
implement.
both login and password match, then the login is allowed or the user is successfully
authenticated and approved to log into that system.
User authentication using a physical object is a second way to authenticate the user
here.
Here, physical object may refer to Bank's Automated Teller Machine (ATM) card or any
other plastic card that is used to authenticate. To authenticate the user, plastic card is
inserted by the user into a reader associated with the terminal or computer system.
Generally, the user must not only insert the card that is used as physical object to
authenticate him/her, but also type in a password just to prevent someone from using a
lost or stolen card.
This method measures the physical characteristics of the user that are very hard to
forge. These are called as biometrics.
((((For example, a company could have their policy that the employee working in the
Computer Science (CS) department are only allowed to log in from 10 A.M. to 4 P.M.,
Monday to Saturday, and then only from a machine in the CS department connected to
company's Local Area Network (LAN).
Now, any attempt to log in by a CS department employee at any wrong time or from any
wrong place would be treated or handled as an attempted break in and log in failure.)))
Processes in the system must be protected from one another’s activities. Else there
may be disruption in the normal working. Protection refers to a mechanism for
controlling the access of programs, processes, or users to resources defined by the
computer system.
Goals of protection-
To ensure that each program component which is active in a system uses system
resources only in ways consistent with stated policies.
To detect latent errors at the interfaces between the component subsystems. Early
detection helps in preventing malfunctioning of subsystems.
Principles of protection-
The time-tested guiding principle used for protection is called the principle of least
privilege.
An OS following this principle implements its features, programs, system calls, and data
structures so that failure or compromise of a component does the minimum damage
and allows minimum damage to be done.
It provides mechanisms to enable privileges when they are needed and to disable them
when not needed.
Privileged function access have audit trails that enable programmer or systems
administrator or law-enforcement officer to trace all protection and security activities of
the system.
We can create separate accounts for each user with just the privileges that the user
needs.
As earlier discussed access matrix is likely to be very sparse and takes up a large chunk
of memory. Therefore direct implementation of access matrix for access control is
storage inefficient.
The inefficiency can be removed by decomposing the access matrix into rows or
columns.Rows can be collapsed by deleting null values and so for the columns to
increase efficiency. From these approaches of decomposition three implementation of
access matrix can be formed which are widely used. They are as follows:
. Global Table
The simplest approach is one big global table with < domain, object, rights > entries.
Unfortunately this table is very large ( even if sparse ) and so cannot be kept in memory
( without invoking virtual memory techniques. )
There is also no good way to specify groupings - If everyone has access to some
resource, then it still needs a separate entry for every domain.
Each column of the table can be kept as a list of the access rights for that particular
object, discarding blank entries.
For efficiency a separate list of default access rights can also be kept, and checked first.
3. Capability Lists for Domains
In a similar fashion, each row of the table can be kept as a list of the capabilities of that
domain.
Capability lists are associated with each domain, but not directly accessible by the
domain or any user process.
Capability lists are themselves protected resources, distinguished from other data in
one of two ways:
A tag, possibly hardware implemented, distinguishing this special type of data. ( other
types may be floats, pointers, booleans, etc. )
The address space for a program may be split into multiple segments, at least one of
which is inaccessible by the program itself, and used by the operating system for
maintaining the process's access right capability list.
4 .A Lock-Key Mechanism
5. Comparison
Each of the methods here has certain advantages or disadvantages, depending on the
particular situation and task at hand.
Many systems employ some combination of the listed methods.
In this scheme, each partition may contain exactly one process. This process limits
the extent of multiprogramming, as the number of partitions decides the number
of processes.