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

Lecture 4 & 5

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)
5 views

Lecture 4 & 5

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

CSE 2024

Operating System
Lecture 4 & 5
Process Management
Saurabh Mishra, PhD
Assistant Professor
Dept of CSE
Content
• Process
• Process Concept
• Process Scheduling
• Operation on Processes
• Inter Process Communication
• Producer Consumer Problem
• Bounded Buffer
• Semaphore
Process
• A Process can be thought as a program in execution
• A thread is a unit of execution with in a process. A process can
have one or many threads.

Process
Thread 1
Thread 2
Thread 3
Process Concept
• An operating system executes a variety of programs:
• Batch system – jobs
• Time-shared systems – user programs or tasks
• Textbook uses the terms job and process almost interchangeably.
• Process – a program in execution; process execution must
progress in sequential fashion.
• A process includes:
• program counter
• stack
• data section
Process State
• As a process executes, it changes state
• new: The process is being created.
• running: Instructions are being executed.
• waiting: The process is waiting for some event to occur.
• ready: The process is waiting to be assigned to a process.
• terminated: The process has finished execution.
Diagram of Process State
Process Control Block (PCB)
Information associated with each process.
• Process state
• Program counter
• CPU registers
• CPU scheduling information
• Memory-management information
• Accounting information
• I/O status information
Process Control Block (PCB)
CPU Switch From Process to Process
Process Scheduling Queues
• Job queue – set of all processes in the system.
• Ready queue – set of all processes residing in main memory,
ready and waiting to execute.
• Device queues – set of processes waiting for an I/O device.
• Process migration between the various queues.
Ready Queue And Various I/O Device Queues
Representation of Process Scheduling
Schedulers
• Long-term scheduler (or job scheduler) – selects which processes
should be brought into the ready queue.
• Short-term scheduler (or CPU scheduler) – selects which process
should be executed next and allocates CPU.
Addition of Medium Term Scheduling
Schedulers (Cont.)
• Short-term scheduler is invoked very frequently (milliseconds)
 (must be fast).
• Long-term scheduler is invoked very infrequently (seconds,
minutes)  (may be slow).
• The long-term scheduler controls the degree of
multiprogramming.
• Processes can be described as either:
• I/O-bound process – spends more time doing I/O than computations,
many short CPU bursts.
• CPU-bound process – spends more time doing computations; few very
long CPU bursts.
Context Switch
• When CPU switches to another process, the system must save
the state of the old process and load the saved state for the new
process.
• Context-switch time is overhead; the system does no useful work
while switching.
• Time dependent on hardware support.
Process Creation
• Parent process creates children processes, which, in turn create
other processes, forming a tree of processes.
• Resource sharing
• Parent and children share all resources.
• Children share subset of parent’s resources.
• Parent and child share no resources.
• Execution
• Parent and children execute concurrently.
• Parent waits until children terminate.
Process Creation (Cont.)
• Address space
• Child duplicate of parent.
• Child has a program loaded into it.
• UNIX examples
• fork system call creates new process
• execve system call used after a fork to replace the process’ memory
space with a new program.
A Tree of Processes On A Typical
UNIX System
Process Termination
• Process executes last statement and asks the operating system to
decide it (exit).
• Output data from child to parent (via wait).
• Process’ resources are deallocated by operating system.
• Parent may terminate execution of children processes (abort).
• Child has exceeded allocated resources.
• Task assigned to child is no longer required.
• Parent is exiting.
• Operating system does not allow child to continue if its parent terminates.
• Cascading termination.
Inter Process Communication
• Process can be categorized into
• Independent process cannot affect or be affected by the execution of
another process.
• Cooperating process can affect or be affected by the execution of another
process
• Advantages of process cooperation
• Information sharing
• Computation speed-up
• Modularity
• Convenience
Inter Process Communication
• There are two primary models of interprocess communication:
• Shared memory and
• Message passing.
• In the shared-memory model, a memory region shared by cooperating
processes gets established. Processes can then exchange information
by reading and writing all the data to the shared region.
• Shared-memory region resides within the address space of any
process creating the shared memory segment. Other processes that
wish to communicate using this shared-memory segment must
connect it to their address space.
• In the message-passing form, communication occurs through
messages exchanged among the cooperating processes.
Inter Process Communication
Producer Consumer Problem
(Shared Memory)
• Producer-Consumer problem is a classical synchronization
problem in the operating system.
• With the presence of more than one process and limited
resources in the system the synchronization problem arises.
• If one resource is shared between more than one process at the
same time then it can lead to data inconsistency.
• In the producer-consumer problem, the producer produces an
item and the consumer consumes the item produced by the
producer.
Producer Consumer Problem
(Shared Memory)
• In operating System Producer is a process which is able to
produce data/item.
• Consumer is a Process that is able to consume the data/item
produced by the Producer.
• Both Producer and Consumer share a common memory buffer.
This buffer is a space of a certain size in the memory of the system
which is used for storage. The producer produces the data into the
buffer and the consumer consumes the data from the buffer.
Producer Consumer Problem
(Shared Memory)
Producer Consumer Problem
(Shared Memory)
• Producer Process should not produce any data when the shared
buffer is full.
• Consumer Process should not consume any data when the
shared buffer is empty.
• The access to the shared buffer should be mutually exclusive i.e at
a time only one process should be able to access the shared
buffer and make changes to it.
• For consistent data synchronization between Producer and
Consumer, the above problem should be resolved.
Bounded-Buffer – Shared-Memory Solution
• Unbounded buffer
• Places no practical limit on the size of the buffer. The consumer
may have to wait for new items, but the producer can always
produce new items

• Bounded buffer
• Assumes a fixed buffer size. In this case, the consumer must
wait if the buffer is empty, and the producer must wait if the
buffer is full.
Bounded-Buffer – Shared-Memory Solution
• Shared data
var n;
type item = … ;
var buffer. array [0..n–1] of item;
in, out: 0..n–1;
• Producer process
repeat

produce an item in nextp

while in+1 mod n = out do no-op;
buffer [in] :=nextp;
in :=in+1 mod n;
until false;
Bounded-Buffer – Shared-Memory Solution
• Consumer process

repeat
while in = out do no-op;
nextc := buffer [out];
out := out+1 mod n;

consume the item in nextc

until false;

• Solution is correct, but can only fill up n–1 buffer.


Semaphore – Shared-Memory Solution
• To solve the Producer-Consumer problem three semaphores
variable are used :

“Semaphores are variables used to indicate the number of


resources available in the system at a particular time. semaphore
variables are used to achieve ‘Process Synchronization’.”
Semaphore – Shared-Memory Solution
• Full : The full variable is used to track the space filled in the buffer by
the Producer process. It is initialized to 0 initially as initially no space is
filled by the Producer process.

• Empty : The Empty variable is used to track the empty space in the
buffer. The Empty variable is initially initialized to the BUFFER-SIZE as
initially, the whole buffer is empty.

• Mutex : Mutex is used to achieve mutual exclusion. mutex ensures that


at any particular time only the producer or the consumer is accessing
the buffer.
• Mutex is a binary semaphore variable that has a value of 0 or 1.
Semaphore – Shared-Memory Solution
• We will use the Signal() and wait() operation in the above-
mentioned semaphores to arrive at a solution to the Producer-
Consumer problem.

• Signal() - The signal function increases the semaphore value by 1.


• Wait() - The wait operation decreases the semaphore value by 1.
Semaphore – Mutex
• Mutex is used to solve the producer-consumer problem as mutex
helps in mutual exclusion.
• Mutex prevents more than one process to enter the critical
section. As mutexes have binary values i.e 0 and 1.
• So whenever any process tries to enter the critical section code it
first checks for the mutex value by using the wait operation.
Semaphore – Mutex
• wait(mutex) decreases the value of mutex by 1. so, suppose a process
P1 tries to enter the critical section when mutex value is 1. P1 executes
wait(mutex) and decreases the value of mutex. Now, the value of
mutex becomes 0 when P1 enters the critical section of the code.
• Now, suppose Process P2 tries to enter the critical section then it will
again try to decrease the value of mutex. But the mutex value is already
0. So, wait(mutex) will not execute, and P2 will now keep waiting for P1
to come out of the critical section.
• Now, suppose if P2 comes out of the critical section by executing
signal(mutex).
• signal(mutex) increases the value of mutex by 1.mutex value again
becomes 1. Now, the process P2 which was in a busy-waiting state will
be able to enter the critical section by executing wait(mutex).
• So, mutex helps in the mutual exclusion of the processes.
Semaphore – Producer Process Code
• wait(Empty) - Before producing items, the producer process checks for the
void Producer()
empty space in the buffer. If the buffer is full producer process waits for the {
consumer process to consume items from the buffer. so, the producer
process executes wait(Empty) before producing any item. while(true)
• wait(mutex) - Only one process can access the buffer at a time. So, once the { // producer
producer process enters into the critical section of the code it decreases the
value of mutex by executing wait(mutex) so that no other process can produces an
access the buffer at the same time.
• add() - This method adds the item to the buffer produced by the Producer item/data
process. once the Producer process reaches add function in the code, it is wait(Empty);
guaranteed that no other process will be able to access the shared buffer
concurrently which helps in data consistency. wait(mutex);
• signal(mutex) - Now, once the Producer process added the item into the
buffer it increases the mutex value by 1 so that other processes which were add();
in a busy-waiting state can access the critical section. signal(mutex);
• signal(Full) - when the producer process adds an item into the buffer
spaces is filled by one item so it increases the Full semaphore so that it signal(Full);
indicates the filled spaces in the buffer correctly.
}
}
Semaphore – Consumer Process Code
• wait(Full) - Before the consumer process starts consuming any item from
the buffer it checks if the buffer is empty or has some item in it. So, the void Consumer()
consumer process creates one more empty space in the buffer and this is
indicated by the full variable. The value of the full variable decreases by one { while(true)
when the wait(Full) is executed. If the Full variable is already zero i.e the { // consumer
buffer is empty then the consumer process cannot consume any item from
the buffer and it goes in the busy-waiting state. consumes an item
• wait(mutex) - It does the same as explained in the producer process. It
decreases the mutex by 1 and restricts another process to enter the critical wait(Full);
section until the consumer process increases the value of mutex by 1.
wait(mutex);
• consume() - This function consumes an item from the buffer. when code
reaches the consuming () function it will not allow any other process to consume();
access the critical section which maintains the data consistency.
signal(mutex);
• signal(mutex) - After consuming the item it increases the mutex value by 1
so that other processes which are in a busy-waiting state can access the signal(Empty);
critical section now.
• signal(Empty) - when a consumer process consumes an item it increases
}
the value of the Empty variable indicating that the empty space in the buffer }
is increased by 1.
Next Lecture
Threads

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy