0% found this document useful (0 votes)
14 views7 pages

OS KCA203 Unit-2.1

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)
14 views7 pages

OS KCA203 Unit-2.1

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/ 7

Dekker’s Algorithm

1. Problem Statement:
○ In concurrent systems, multiple processes or threads may need to access
shared resources.
○ Ensuring mutual exclusion (only one process at a time) in the critical
section is essential.
○ We want to avoid deadlock and ensure progress.
2. Dekker’s Solution:
○ Dekker’s algorithm was one of the first provably-correct solutions to the
critical section problem.
○ It allows two threads to share a single-use resource without conflict,
using only shared memory for communication.
○ Unlike naive turn-taking algorithms, Dekker’s algorithm avoids strict
alternation.
○ It was one of the earliest mutual exclusion algorithms invented.
3. Algorithm (Final Version):
○ Dekker’s algorithm requires an array of Boolean values (flag) and an
integer variable (turn).

The algorithm ensures mutual exclusion between two processes:

repeat
flag[i] := true;
while flag[j] do
if turn = j then
begin
flag[i] := false;
while turn = j do no-op;
flag[i] := true;
end;
critical section
turn := j;
flag[i] := false;
until false;
Peterson’s Solution

-Two process solution


-Assume that the load and store machine-language instructions
are atomic; that is, cannot be interrupted
-The two processes share two variables:
● int turn;
● Boolean flag[2]

-The variable turn indicates whose turn it is to enter the critical


section
-The flag array is used to indicate if a process is ready to enter the
critical section. flag[i] = true implies that process Pi is ready!

Algorithm for Process Pi

do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
Dining-Philosophers Problem

● N philosophers’ sit at a round table with a bowl of rice in the


middle.

● They spend their lives alternating thinking and eating.


● They do not interact with their neighbors.
● Occasionally try to pick up 2 chopsticks (one at a time) to eat
from bowl
○ Need both to eat, then release both when done
● In the case of 5 philosophers, the shared data
■ Bowl of rice (data set)
■ Semaphore chopstick [5] initialized to 1
Semaphore Solution:

▪ The structure of Philosopher i :


while (true){
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );

/* eat for awhile */

signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );

/* think for awhile */

}
Sleeping Barber problem

The analogy is based upon a hypothetical barber shop with one barber. There is
a barber shop which has one barber, one barber chair, and n chairs for waiting
for customers if there are any to sit on the chair.

If there is no customer, then the barber sleeps in his own chair.

When a customer arrives, he has to wake up the barber.


If there are many customers and the barber is cutting a customer’s hair, then the
remaining customers either wait if there are empty chairs in the waiting room or
they leave if no chairs are empty.

Algorithm for Sleeping Barber problem:

Semaphore Customers = 0;
Semaphore Barber = 0;
Mutex Seats = 1;
int FreeSeats = N;
Barber {
while(true) {
down(Customers);
down(Seats);
FreeSeats++;
up(Barber);
up(Seats);
/* barber is cutting hair.*/
}
}
Customer {
while(true) {
down(Seats);
if(FreeSeats > 0) {
FreeSeats--;
up(Customers);
up(Seats);
down(Barber);
// customer is having hair cut
} else {
up(Seats);
// customer leaves
}
}
}

Inter-process communication (IPC)

Inter-process communication (IPC) is a fundamental concept in computer


science and operating systems that allows processes (i.e., running instances of
programs) to communicate and share data with each other. There are several
methods of IPC, each suited to different scenarios and requirements:

1. Shared Memory: In this method, processes share a common portion of


memory that they can read from and write to. This is efficient but requires
synchronization mechanisms to avoid data corruption.
2. Message Passing: Processes communicate by sending messages to each
other. This can be implemented using various mechanisms such as pipes,
sockets, and message queues.
3. Synchronization Primitives: IPC often involves synchronization primitives
like semaphores, mutexes, and condition variables to coordinate access to
shared resources and avoid race conditions.
4. Signals and Events: Processes can also communicate through signals and
events. Signals are used to notify processes of events or to request specific
actions (e.g., SIGINT for interrupt). Events can be used in event-driven
architectures to trigger actions in response to specific conditions.
5. Remote Procedure Calls (RPC): RPC allows a process to execute
procedures or functions in another process as if they were local, abstracting
the communication details.
6. Distributed Objects: This is a higher-level abstraction where objects in
different processes or even different machines can interact as if they were
local objects, often used in distributed computing environments.

Each IPC method has its advantages and trade-offs in terms of performance,
complexity, and suitability for different use cases. The choice of IPC mechanism
depends on factors such as the nature of the communication, performance
requirements, security considerations, and the architecture of the system.

Process creation:

Process creation is managed by the Linux kernel using system calls and utilities
provided by the operating system. Here's a basic overview of how process creation
works in Linux

1. Fork System Call:


○ One of the primary mechanisms for creating a new process in Linux is the
fork system call. When a process calls fork, the kernel creates a new
process (the child process) that is a copy of the calling process (the parent
process) in almost all aspects.
○ The fork system call creates a new process with its own PID, separate
address space, and resources (such as file descriptors).
2. Exec System Calls:
○ After forking a new process, it's common to use the exec family of system
calls (e.g., execve, execl, execvp, etc.) to replace the current process
image with a new program.

You might also like