Unit-2 OS BCS-401
Unit-2 OS BCS-401
(BCS-401)
Process Synchronization
(UNIT-2)
Course Instructor:
Dr. Sanjeev Kumar
(Associate Professor, IT Department, KIET, Ghaziabad)
Operating Systems Syllabus (Unit-2)
– In this Unit we will discuss how concurrent or parallel execution can contribute
to issues involving the integrity of data shared by several processes
OPERATING SYSTEMS (KCS-401)
(Dr. Sanjeev Kumar, Associate Professor, IT,
KIET)
Process Synchronization
– A cooperating process is one that can affect or be
affected by other processes executing in the
system.
– Concurrent access to shared data may result in
data inconsistency.
– The question is how concurrent or parallel
execution can contribute to issues involving the
integrity of data shared by several processes.
OPERATING SYSTEMS (KCS-401)
(Dr. Sanjeev Kumar, Associate Professor, IT,
KIET)
Intercrosses Communication (IPC)
– Cooperating processes require an interprocess
communication (IPC) mechanism that will allow
them to exchange data and information.
– There are two fundamental models of interprocess
communication: shared memory and message
passing.
– counter--
Operating system(BCS401)
Satisfies Mutual Exclusion, But(Dr.
Progress?
Sanjeev Kumar, IT Department,
KIET)
Strict Alternation: ME is Satisfied
P0 might be in its remainder section at this time as it is not reentering CS to make turn=1.
Here a process in remainder section, and not interested to use Critical section is deciding when the
other process will enter in its critical section So Progress is not satisfied. .
Thus,
Final value of semaphore variable S
= 10 – (6 x 1) + (4 x 1)
ATOMIC
OPERATION
P1
P2-->
Reader Process
• Related to OS as it PHILOSOPHER
reflects limited
resources and problem IF THINKS IF EATS
CASE-1 CASE-2
• In these cases, either mutual exclusion is violated
OPERATING SYSTEMS (KCS-401)
orKumar,
(Dr. Sanjeev a deadlock will
Associate Professor, IT,
KIET)
Monitor
• To deal with such errors (previous slide), researchers have
developed high-level language construct; the monitor type.
• A monitor type is an ADT that includes a set of programmer-
defined operations that are provided with mutual exclusion
within the monitor.
• The monitor type also contains the declaration of variable
whose value define the state of instance of that type along
with bodies of functions/Procedures that operate on those
variables.
• Philosopher i can set the variable state[i] = EATING only if her two neighbors are
not eating: (state[(i+4) % 5] != EATING) and (state[(i+1) % 5] != EATING).
• We also need to declare condition self[5]; where philosopher can delay himself
when he is hungry but is unable to obtain chopsticks he need
90
TYPES OF PROCESSES
Independent Process:
A process is independent if it cannot affect or be
affected by other processes executing in the system.
Any process that does not share data with any other
process is independent.
Co-operative Process:
A process is Co-operative if it can affect or be
affected by other processes executing in the system.
Any process that share data with any other process is
co-operative.
91
Reasons for providing an Environment
that allows process cooperation
1. Information Sharing: Several users may be interested in shared file.
There is a need to provide an environment so that the information can be
accessed concurrently.
2. Computational speedup: Task is divided into sub tasks that run
concurrently in order to achieve computational speedup. These subtasks
are the processes that communicate with each other.
3. Modularity: We want to design a system by dividing it into separate
modules. These modules communicate with each other and then put
together to achieve the goal.
4. Convenience: If we provide an environment that allow the processes to
cooperate with each other, then that becomes very convenient to the user.
Suppose a user may be using his system for doing several different tasks
at the same time. For example he may be writing document, listening
song or printing a document. So these tasks must be allowed to run
concurrently for smooth functioning of the system. 92
Inter Process Communication
Model (IPC Model)
• IPC means how two processes are communicate
to each other.
• Cooperative processes require an IPC
mechanism that will allow them to exchange
data and information.
94
SHARED MEMORY SYSTEM
• In the shared memory model, a region of memory that is shared by co-
operating processes is established. Processes can then exchange information
by reading and writing data to the shared region.
EXAMPLE
• Suppose process A and process B are executing simultaneously and they share
some resources or use some information from other process,
• process A generate information about resources being used and keeps it as a
record in shared memory. When process B need to use the shared information,
it will check in the record stored in shared memory and take note of the
information generated by process A and act accordingly.
• Processes can use shared memory for extracting information as a record from
other process as well as for delivering any specific information to other
process. 95
96
MESSAGE PASSING SYSTEM
• A standard message can have two parts: header and body.
• The header part is used for storing Message type, destination id,
source id, message length and control information.
• The control information contains information like what to do if runs out
of buffer space, sequence number, priority. Generally, message is sent
using FIFO style.
• There are two types of message passing communication are used:
1. Direct Communication
2. Indirect Communication
97
DIRECT MESSAGE PASSING
98
99
100
DIRECT MESSAGE PASSING
101
INDIRECT MESSAGE PASSING
• In indirect message passing , processes uses mailboxes ( also known as ports)
for sending and receiving messages. Each mailbox has a unique id.
• Suppose two processes want to communicate through Indirect message passing,
the required operations are : create a mail box, use this mail box for sending and
receiving messages, destroy the mail box.
• Operations are: send(A, message) means send the message to mailbox A.
receive(A, message) means receive message A from mailbox.
• A process can communicate with another process via a number of different
mailboxes, but two processes can communicate only if they have a shared
mailbox.
• A mailbox may be owned either by a process or by the operating system. If the
mailbox is owned by a process (that is, the mailbox is part of the address space of
the process), then we distinguish between the owner (which can only receive
messages through this mailbox) and the user (which can only send messages to
102
the mailbox)
103
104
105
Buffering In IPC
106
Process Generation: fork()
• Fork system call is used for creating a new process,
which is called child process, which runs concurrently
with the process that makes the fork() call (parent
process).
• After a new child process is created, both processes will
execute the next instruction following the fork() system
call.
• A child process uses the same pc(program counter), same
CPU registers, same open files which the parent process
is currently using.
OPERATING SYSTEMS (KCS-401)
(Dr. Sanjeev Kumar, Associate Professor, IT,
KIET)
fork()
• Please note that the below programs don’t compile in Windows