0% found this document useful (0 votes)
13 views

Unit III Exception

Fdfg

Uploaded by

surajj0624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit III Exception

Fdfg

Uploaded by

surajj0624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ques 1. What are semaphores?

Ans : Semaphore is a variable is used to solve critical section problem and to achieve process
synchronization in the multi-processing environment. The two most common kinds of
semaphores are counting semaphores and binary semaphores. Counting semaphore can take
non-negative integer values and Binary semaphore can take the value 0 & 1.

Ques 2. Define critical section.


Ans : In concurrent programming, concurrent accesses to shared resources can lead to
unexpected or erroneous behaviour , so parts of the program where the shared resource is
accessed need to be protected in ways that avoid the concurrent access. This protected section
is the critical section or critical region. It cannot be executed by more than one process at a
time. Typically, the critical section accesses a shared resource, such as a data structure, a
peripheral device, or a network connection, that would not operate correctly in the context
of multiple concurrent accesses.

Q3. Define co-routine with an example.


Ans : Co-routines: Computer program components that generalize subroutines for non-pre-
emptive multitasking, by allowing execution to be suspended and resumed. Coroutines are
well-suited for implementing familiar program components such as cooperative tasks,
exceptions, event loops, iterators, infinite lists and pipes. Coroutines are very similar to threads.
However, coroutines are cooperatively multitasked, whereas threads are typically pre-
emptively multitasked. This means that coroutines provide concurrency but not parallelism.
The advantages of coroutines over threads are that they may be used in a hard-real-time context
there is no need for synchronisation primitives such as mutexes, semaphores, etc. in order to
guard critical sections, and there is no need for support from the operating system.
It is possible to implement coroutines using preemptively-scheduled threads, in a way that will
be transparent to the calling code, but some of the advantages (particularly the suitability for
hard-real-time operation and relative cheapness of switching between them) will be lost.
A coroutine call is named a resume The first resume of a coroutine is to its beginning, but
subsequent calls enter at the point just after the last executed statement in the coroutine
Q4. What is concurrency?
Concurrency is when multiple sequences of operations are run in overlapping periods of
time. Dealing with constructs such as threads and locks and avoiding issues like race
conditions and deadlocks can be quite cumbersome, making concurrent programs difficult to
write. Through concurrency, programs can be designed as independent processes working
together in a specific composition.
• Categories of Concurrency:
(i) Physical concurrency - Multiple independent processors (multiple threads of control).
(ii) Logical concurrency - The appearance of physical concurrency is presented by time-
sharing one processor (software can be designed as if there were multiple threads of control).

Q5. What are semaphores? Explain their types with a suitable examples.

Ans: A semaphore is a data structure consisting of a counter and a queue for storing
task descriptors. Semaphores can be used to implement guards on the code that accesses
shared data structures. The two most common kinds of semaphores are counting
semaphores and binary semaphores. Counting semaphore can take non-negative integer
values and Binary semaphore can take the value 0 & 1.
Semaphores have only two operations, wait and release (originally called P and V by
Dijkstra)
Semaphores can be used to provide both competition and cooperation synchronization.
(a) Semaphores: Wait Operation
wait(aSemaphore)
if aSemaphore‘s counter > 0 then
decrement aSemaphore‘s counter
else
put the caller in aSemaphore‘s queue
attempt to transfer control to a ready task
-- if the task ready queue is empty,
-- deadlock occurs
End

(b) Semaphores: Release Operation


release(aSemaphore)
if aSemaphore‘s queue is empty then
increment aSemaphore‘s counter
else
put the calling task in the task ready queue
transfer control to a task from aSemaphore‘s
queue
end

Cooperation Synchronization with Semaphores : Example: A shared buffer

• The buffer is implemented as an ADT(abstract data type) with the operations DEPOSIT
and FETCH as the only ways to access the buffer.

• Use two semaphores for cooperation: emptyspots and fullspots.

Q6. Explain what is monitors in programming? Mention some difference between


semaphores and monitors.
Ans : A monitor is an abstract data type for shared data. A high-level abstraction that provides
a convenient and effective mechanism for process synchronization. Abstract data type, internal
variables only accessible by code within the procedure. Only one process may be active within
the monitor at a time. But not powerful enough to model some synchronization schemes. The
monitor is one of the ways to achieve Process synchronization. The monitor is supported by
programming languages to achieve mutual exclusion between processes. For example, Java
Synchronized methods. Java provides wait( ) and notify( ) constructs.

Advantages: Monitors have the advantage of making parallel programming easier and less
error prone than using techniques such as semaphore.

Disadvantages: Monitors have to be implemented as part of the programming language. The


compiler must generate code for them. This gives the compiler the additional burden of having
to know what operating system facilities are available to control access to critical sections in
concurrent processes. Some languages that do support monitors are Java,C#,Visual Basic.

Q7. What is exception handling?


Exception handling: An exception is any unusual event, either erroneous or not, detectable by
either hardware or software, that may require special processing.
•The special processing that may be required after detection of an exception is called
exception handling
•The exception handling code unit is called an exception handler
In a language without exception handling. When an exception occurs, control goes to the
operating system, where a message is displayed and the program is terminated. In a language
with exception handling programs are allowed to trap some exceptions, thereby providing the
possibility of fixing the problem and continuing.
Advantages of Built-in Exception Handling
•Error detection code is tedious to write and it clutters the program
•Exception handling encourages programmers to consider many different possible
errors
•Exception propagation allows a high level of reuse of exception handling code

Q9. Difference between error and exception

Basis of Exception Error


Comparison

Recoverable/ Exception can be recovered by


An error cannot be recovered.
Irrecoverable using the try-catch block.

Type It can be classified into two All errors in Java are


categories i.e. checked and unchecked.
unchecked.

Occurrence It occurs at compile time or run It occurs at run time.


time.
Package It belongs to It belongs to java.lang.Error
java.lang.Exception package. package.

Known or Only checked exceptions are Errors will not be known to


unknown known to the compiler. the compiler.

Causes It is mainly caused by the It is mostly caused by the


application itself. environment in which the
application is running.

Q10. What are exception handlers?

Exception Handlers are subprograms that are not invoked by explicit calls
Special situations, called exceptions:
Error conditions
Unpredictable conditions
Tracing and monitoring

Exception handlers typically contain only:


A set of declarations of local variables
A sequence of executable statements

Exception Handlers can be


-pre defined in the language
-programmer defined

Implementation
Operating system exceptions-raised directly by hardware interrupts.
Programmer defined-the translator inserts code to handle the exceptions.

Q11. What are various message passing mechanisms?


Ans : In computer science, message passing is a technique for invoking behavior (i.e., running
a program) on a computer. The invoking program sends a message to a process (which may be
an actor or object) and relies on the process and the supporting infrastructure to select and
invoke the actual code to run. Message passing differs from conventional programming where
a process, subroutine, or function is directly invoked by name. Message passing is key to some
models of concurrency and object-oriented programming.
Synchronous message passing: Synchronous message passing occurs between objects that
are running at the same time. It is used by object-oriented programming languages such as Java
and Smalltalk. Synchronous messaging is analogous to a synchronous function call; just as the
function caller waits until the function completes, the sending process waits until the receiving
process completes.

Asynchronous messaging requires additional capabilities for storing and retransmitting data
for systems that may not run concurrently, and are generally handled by an intermediary level
of software (often called middleware); a common type being Message-oriented middleware
(MOM). The buffer required in asynchronous communication can cause problems when it is
full. A decision has to be made whether to block the sender or whether to discard future
messages. A blocked sender may lead to deadlock. If messages are dropped, communication is
no longer reliable.

You might also like