Artificial Intelligence Research Notes
Artificial Intelligence Research Notes
Section A
Question 1
f. Mutual Exclusion
● Concept: Mutual exclusion is a fundamental synchronization property ensuring
that no two concurrent processes (or threads) are simultaneously active inside
their critical section. A critical section is a piece of code that accesses shared
resources (like shared variables, data structures, files, or devices like the bank
database ).
● Importance for Synchronization: It prevents race conditions, where the
outcome of computation depends on the unpredictable timing of concurrent
access to shared resources. In the bank example, without mutual exclusion, two
tellers might try to update the same account balance concurrently, leading to
incorrect results (e.g., one withdrawal overwriting another). It ensures data
integrity and consistency in concurrent systems.
● Implementation: Mutual exclusion can be implemented using various
mechanisms:
○ Mutex Locks (as mentioned ): A common technique. A process must acquire
the mutex lock before entering the critical section and release it upon exiting.
Only one process can hold the lock at a time. Others trying to acquire a held
lock will block (wait) until it's released.
○ Semaphores: Binary semaphores (initialized to 1) can function like mutexes.
wait() (or P()) decrements the semaphore (acquires the lock), signal() (or V())
increments it (releases the lock).
○ Monitors: High-level language constructs that encapsulate shared data and
procedures operating on it, automatically enforcing mutual exclusion for
monitor procedures.
○ Hardware Support: Special atomic instructions (e.g., Test-and-Set,
Compare-and-Swap) provided by the CPU can be used as building blocks for
implementing locks efficiently.
○ Disabling Interrupts (Careful Use): On a uniprocessor, temporarily disabling
interrupts can prevent context switches within a critical section, ensuring
atomicity. However, this is dangerous, affects system responsiveness, and
doesn't work on multiprocessor systems.
The standard three-state model (Running, Ready, Blocked) typically shows these
transitions:
1. Running → Ready (Preemption, e.g., time slice expired)
2. Running → Blocked (Waiting for event, e.g., I/O request)
3. Ready → Running (Scheduler dispatches process)
4. Blocked → Ready (Event occurred, e.g., I/O completed)
Section B
Question 2
(Assuming Figure 2a depicts the classic "four cars at an intersection" scenario, each
wanting to proceed forward but blocked by the car to its right)
1. Mutual Exclusion: Each section of the intersection (the resource) can only be
occupied by one car (process) at a time. A car cannot drive through a space
already occupied by another car.
2. Hold and Wait: Each car is occupying one section of the intersection (holding a
resource, e.g., car 'a' holds the space it's in) and waiting to enter the section
occupied by the car to its right (waiting for another resource, e.g., car 'a' waits for
the space held by car 'b').
3. No Preemption: A section of the intersection occupied by a car cannot be
forcibly taken away from it (e.g., car 'b' cannot be forcibly removed from its spot
to let car 'a' pass). Cars must voluntarily vacate their spot (resource).
4. Circular Wait: There exists a circular chain of cars waiting for resources held by
the next car in the chain. For example, car 'a' waits for the resource held by car
'b', car 'b' waits for the resource held by car 'c', car 'c' waits for the resource held
by car 'd', and car 'd' waits for the resource held by car 'a'.
(Note: The prompt mentions a protocol where processes acquire all resources first.
This is actually a deadlock prevention technique (breaking Hold and Wait). If this
protocol is strictly followed, deadlocks shouldn't occur. However, the question asks
how to resolve deadlocks if they happen in such a system, implying the protocol might
fail or the question context is slightly shifted. Assuming deadlocks can still occur for
some reason, here are resolution techniques.)
1. Process Termination: Abort one or more processes involved in the deadlock
cycle.
○ Methods:
■ Abort all deadlocked processes: Simple, but costly as all work done is lost.
■ Abort one process at a time: Abort one process, check if the deadlock is
resolved. If not, abort another, and so on. Choosing which process to
abort is key (e.g., lowest priority, least progress made, fewest resources
held).
○ Advantages: Relatively easy to implement. Guaranteed to break the deadlock
eventually.
○ Disadvantages: Loss of computation, potentially requires processes to be
restarted from scratch. Difficult to choose the optimal victim.
○ Example Scenario: In a batch processing system where jobs can be easily
restarted, terminating a deadlocked job might be acceptable.
2. Resource Preemption: Forcibly take away resources from one or more
deadlocked processes and give them to other processes until the deadlock cycle
is broken.
○ Issues:
■ Victim Selection: Which process and which resources to preempt?
Minimize cost.
■ Rollback: The process that loses a resource must be rolled back to a prior
safe state before it acquired that resource. This can be complex, requiring
checkpointing or state saving.
■ Starvation: Ensure the same process isn't always chosen as the victim.
○ Advantages: Potentially less disruptive than process termination if rollback is
feasible.
○ Disadvantages: Complex to implement rollback mechanisms. Overhead of
state saving. Potential for starvation.
○ Example Scenario: In a database system, a transaction holding locks might be
chosen for rollback to resolve a deadlock, using the database's transaction
logging/recovery mechanisms.
3. Operator Intervention: Alert a human operator about the deadlock, allowing
them to manually intervene and resolve it (e.g., by killing a process, releasing a
resource if possible).
○ Advantages: Flexible; the operator can use judgment based on the specific
situation.
○ Disadvantages: Slow, requires human monitoring, not suitable for automated
or time-critical systems.
○ Example Scenario: A long-running scientific simulation gets stuck; an
administrator investigates and manually terminates one of the processes
involved.
(Note: Deadlock detection algorithms must run first to identify that a deadlock exists
and which processes/resources are involved before recovery techniques can be
applied.)
Question 3
Processes:
● N1: Arrival=0, Burst=25
● N2: Arrival=5, Burst=15
● N3: Arrival=10, Burst=5
● N4: Arrival=15, Burst=5
● Calculations:
○ Completion Times (CT): N1=25, N2=40, N3=45, N4=50
○ Turnaround Times (TAT = CT - Arrival):
■ N1: 25 - 0 = 25
■ N2: 40 - 5 = 35
■ N3: 45 - 10 = 35
■ N4: 50 - 15 = 35
■ Average TAT: (25 + 35 + 35 + 35) / 4 = 130 / 4 = 32.5 ms
○ Waiting Times (WT = TAT - Burst):
■ N1: 25 - 25 = 0
■ N2: 35 - 15 = 20
■ N3: 35 - 5 = 30
■ N4: 35 - 5 = 30
■ Average WT: (0 + 20 + 30 + 30) / 4 = 80 / 4 = 20 ms
ii. Preemptive Shortest Job First (SJF) / Shortest Remaining Time First (SRTF)
● Gantt Chart & Execution Trace:
○ t=0: N1 arrives (Burst=25). N1 starts running.
○ t=5: N2 arrives (Burst=15). N1 remaining=20. N2 burst (15) < N1 remaining (20).
Preempt N1. N2 starts running.
○ t=10: N3 arrives (Burst=5). N2 remaining=10. N3 burst (5) < N2 remaining (10).
Preempt N2. N3 starts running.
○ t=15: N4 arrives (Burst=5). N3 remaining=0 (finishes exactly at t=15). Compare
N1(20), N2(10), N4(5). N4 is shortest. N4 starts running.
○ t=20: N4 finishes (Burst=5). Compare N1(20), N2(10). N2 is shorter. N2
resumes.
○ t=30: N2 finishes (Ran for 10 more). Only N1(20) left. N1 resumes.
○ t=50: N1 finishes (Ran for 20 more).
| N1 | N2 | N3 | N4 | N2 | N1 |
0 5 10 15 20 30 50
● Calculations:
○ Completion Times (CT): N1=50, N2=30, N3=15, N4=20
○ Turnaround Times (TAT = CT - Arrival):
■ N1: 50 - 0 = 50
■N2: 30 - 5 = 25
■N3: 15 - 10 = 5
■N4: 20 - 15 = 5
■Average TAT: (50 + 25 + 5 + 5) / 4 = 85 / 4 = 21.25 ms
○ Waiting Times (WT = TAT - Burst):
■ N1: 50 - 25 = 25
■ N2: 25 - 15 = 10
■ N3: 5 - 5 = 0
■ N4: 5 - 5 = 0
■ Average WT: (25 + 10 + 0 + 0) / 4 = 35 / 4 = 8.75 ms
| N1 | N3 | N4 | N2 |
0 25 30 35 50
● Calculations:
○ Completion Times (CT): N1=25, N2=50, N3=30, N4=35
○ Turnaround Times (TAT = CT - Arrival):
■ N1: 25 - 0 = 25
■ N2: 50 - 5 = 45
■ N3: 30 - 10 = 20
■ N4: 35 - 15 = 20
■ Average TAT: (25 + 45 + 20 + 20) / 4 = 110 / 4 = 27.5 ms
○ Waiting Times (WT = TAT - Burst):
■ N1: 25 - 25 = 0
■ N2: 45 - 15 = 30
■ N3: 20 - 5 = 15
■ N4: 20 - 5 = 15
■ Average WT: (0 + 30 + 15 + 15) / 4 = 60 / 4 = 15 ms
Question 4
A valid solution to the critical-section problem must satisfy these three requirements:
1. Mutual Exclusion: If one process is executing in its critical section, no other
processes can be executing in their critical sections simultaneously.
2. Progress: If no process is executing in its critical section and some processes
wish to enter their critical sections, then only those processes that are not
executing in their remainder sections can participate in2 deciding which will enter
its critical section next. This selection cannot be postponed indefinitely.3
(Essentially, if someone wants to enter and the critical section is free, a decision
must eventually be made allowing someone to enter).
3. Bounded Waiting (No Starvation): There must be a limit on the number of times
or the amount of time 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.4 This ensures that a process waiting to enter its critical
section will eventually get access and not wait forever (starvation).