OS UNIT2 Sylaja
OS UNIT2 Sylaja
OPERATING
SYSTEMS(UNIT 2)
Dr.SYLAJA VALLEE NARAYAN S R
Assistant Professor/CSE
GITAM University
Bangalore
UNIT 2 Process Management & CPU Scheduling
1.1.PROCESS
Process is a program in execution .
It contains Stack: contains temporary data(function parameter, return
addresses and local variables)
Heap: is memory dynamically allocated during process run time.
Data :contains local variables
Text: is program code
Includes
2.1.Scheduling Queues
2.2.Schedulers
2.3.Context Switch
Processes waiting for a particular I/O device are put in Device Queue.
Process scheduling is represented in Queuing diagram
1.Rectangle box represents ready and device queue
2.Circle represent resources that serves the queue
3.Arrow indicate the flow of processes in the system
15
Parent process create children processes, which, in turn create other processes, forming a tree
of processes. E.g. is shown below
The Secure Shell Daemon
application (SSH daemon or sshd
provides encrypted communications
between two untrusted hosts over an
insecure network.
Bash is a command processor that
runs in a text window where the user
types commands that cause actions.
Bash can also read and execute
commands from a file, called a shell
script.
Vim is a text editor for Unix that
comes with Linux, BSD, and macOS.
Tcsh”tee-see-shell” is an enhanced
version of the csh. It includes some
additional utilities such as command
line editing and filename/command
completion.
Ps (process status) command allows
to view information about the
processes running on Linux system.
3.2.Process Termination
Parent may terminate the execution of child process using the abort() system call.
Some reasons for doing so:
1. Child has exceeded allocated resources
2. Task assigned to child is no longer required
3. The parent is exiting and the operating systems does not allow a child to continue if
its parent terminates
The parent process may wait for termination of a child process by using the wait()
system call. The call returns status information and the pid of the terminated process
pid = wait(&status);
Zombie :a child process has terminated (via the exit system call) but exit status is
ignored by parent.
Orphan :If parent terminated without invoking wait and the child process is orphan,
which is handled by operating system.
cascading termination: All children, grandchildren, etc. are terminated.
4.Interprocess Communication(IPC)
is used for exchanging information between numerous threads in one or more processes (or
programs)
2 models of IPC
4.1.Shared Memory Systems
4.2.Message passing Systems
4.1.Shared Memory Systems
has shared memory that can be simultaneously accessed(read and write ) by multiple processes.
All POSIX systems, Windows operating systems use shared memory.
2 types of Process
1.Independent process
2.Cooperating process
Independent process: does
not share data with any other
process
Cooperating process: share
data with other processes. So
it requires IPC mechanism
Producer- consumer problem uses the concept of shared memory.
Examples of producer consumer problem are
Compiler may produce assembly code, which is consumed by an assembler. Assembler in turn,
may produce object modules which are consumed by the loader.
In Client- Server paradigm, Server can be producer and client can be consumer. E.g. web server
produces HTML files and images, which are consumed by the client web browser requesting the
resource.
A buffer is in the shared memory where producer can produce one item while the consumer is
consuming another item. Communication in client server is through Sockets, Remote Procedure
2 types of buffers are used Calls, Pipes, Remote Method Invocation (Java)
1.Unbouded buffer
2.Bounded buffer
Unbounded-buffer places no limit on the size of the buffer. Consumer may wait, producer never
waits.
Bounded-buffer assumes that there is a fixed buffer size. Consumer waits for new item,
producer waits if buffer is full.
Shared buffer is implemented as a circular array with two logical pointer: in and out
in points to the next free position in the buffer.
out points to the first full position in the buffer.
Buffer is empty when in ==out.
Buffer is full when ((in+1)%BUFFER_SIZE)==out
The Bounded Buffer
# define BUFFER_SIZE 10
typedef struct
{
……
} item;
item buffer[BUFFER_SIZE] ;
int in = 0;
int out = 0;
In the above diagram, both the processes P1 and P2 can access the message queue and
store and retrieve data.
Message Passing provides a mechanism to allow processes to communicate and to synchronize
their actions without sharing the same address space.
For example − chat programs on World Wide Web.
Message passing provides two operations
1.Send message
2.Receive message
Messages sent by a process can be either fixed or variable size.
Example: If process P and Q want to communicate they need to send a message to and receive
a message from each other through a communication link.
Communication link can be
1.Physical Implementation : shared memory, hardware bus, network
2. Logical Implementation :
includes
2.1.Naming(Direct or indirect communication)
2.2.Synchronization(Synchronous or asynchronous communication)
2.3.Buffering(Automatic or explicit buffering)
2.1.Direct or indirect communication(Naming)
In Direct communication , 2 addressing are there
i. Symmetry addressing: each process that wants to communicate must explicitly
name the sender and receiver(recipient)
send() and receive() primitives are defined as
send(P, message) :Send a message to process P
receive(Q, message) :Receive a message from process Q
ii. Asymmetry addressing: only sender names receiver, receiver is not required to
name the sender
send() and receive() primitives are defined as
send( P, message) : Send a message to process P
receive(id, message) : receive a message from any process. Variable id is set to the name of
the process with which communication has taken place.
Communication link has following properties
Links are established automatically
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
In Indirect Communication
the messages are sent to or received from mailboxes or ports. Mailbox is viewed as an
object in to which messages can be placed by processes and from which messages can be
removed. Each mail box has an unique identification. Example POSIX message queues use
an integer value to identify a mailbox.
Send() and receive() primitives are defined as follows
send( A, message) : send a message to mailbox A
receive(A, message) : receive a message from mailbox A
Communication Link has following properties
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
2.2.Synchronous or asynchronous communication(Synchronization)
Message passing can be
1.Blocking (synchronous)
2.Non-blocking(asynchronous)
Blocking send: Sending process is blocked until the message is received by
the receiving process or mail box.
Blocking receive: the receiver is blocked until a message is available
Non- blocking send: the sender sends the message and continue
Non- blocking receive: Receiver receives either a valid message or a null.
2.3.Buffering
Messages exchanged by communicating processes reside in a temporary
queue.
Queues are implemented in 3 ways.
1. Zero capacity (system with no buffering)
2. Bounded capacity (system with automatic buffering)
3. Unbounded capacity (system with automatic buffering)
In Zero capacity: no messages are queued on a link. Sender must wait for
receiver (rendezvous)
In Bounded capacity: finite length of n messages can be in the queue.
Sender must wait if link full.
In Unbounded capacity: infinite length of messages can be in the queue.
Sender never waits
Examples of IPC Systems - POSIX
POSIX Shared Memory
Process first creates shared memory segment
shm_fd = shm_open(name, O _ CREAT | O _RDWR,
0666);
Also used to open an existing segment to share it
Set the size of the object
ftruncate(shm fd, 4096);
Now the process could write to the shared memory
sprintf(shared memory, "Writing to shared
memory");
POSIX (Portable Operating System Interface) is a set of standard
operating system interfaces based on the Unix operating system.
Message passing in Windows via Local Procedure Calls
5.Multithreaded programming
Multithreading allows the application to divide its task into individual
threads.
In multi-threads, the same process or task can be done by the number of
threads.
Kernels are generally multithreaded
Fig: Multicore
Concurrency vs. Parallelism
Concurrent execution(serial) on single-core
system:
• That is, if application is 75% parallel / 25% serial, moving from 1 to 2 cores
results in speedup of 1.6 times
• As N approaches infinity, speedup approaches 1 / S
Features Multiprocessors Multicore
Definition It is a system with multiple CPUs that A multicore processor is a single
allows processing programs processor that contains multiple
simultaneously. independent processing units known as
cores that may read and execute program
instructions.
Execution Multiprocessors run multiple programs The multicore executes a single program
faster than a multicore system. faster.
Reliability It is more reliable than the multicore It is not much reliable than the
system. If one of any processors fails in multiprocessors.
the system, the other processors will not
be affected.
Traffic It has high traffic than the multicore It has less traffic than the multiprocessors.
system.
Cost It is more expensive as compared to a These are cheaper than the
multicore system. multiprocessors system.
7.2.Many-to-one Model
Many user-level threads mapped to single kernel thread.
Disadv:
One thread blocking causes all to block.
Adv:
allow the application to create any number of threads that can execute concurrently.
In a many-to-one (user-level threads) implementation, all threads activity is restricted to
user space.
Examples:
Solaris Green Threads
GNU Portable Threads
are
1.Pre-emptive
2.Non-preemptive
4.ROUNDROBIN SCHEDULING
PRE-EMPTIVE
5.PRIORITY SCHEDULING
NON-PREEMPTIVE
PRE-EMPTIVE
Arrival time (AT) : Arrival time is the time at which the process arrives in ready queue.
Burst time (BT) or CPU time of the process : Burst time is the unit of time in which a particular process
completes its execution.
Completion time (CT) :Completion time is the time at which the process has been terminated.
Turn-around time (TAT) : The total time from arrival time to completion time is known as turn-around time.
TAT can be written as,
Turn-around time (TAT) = Completion time (CT) – Arrival time (AT) or, TAT = Burst time (BT) + Waiting time
(WT)
Waiting time (WT) : Waiting time is the time at which the process waits for its allocation while the previous
process is in the CPU for execution. WT is written as,
Waiting time (WT) = Turn-around time (TAT) – Burst time (BT)
Response time (RT) : Response time is the time at which CPU has been allocated to a particular process first
time.
In case of non-preemptive scheduling, generally Waiting time and Response time is same.
Gantt chart :
A Gantt chart is a horizontal bar chart used to represent operating systems CPU scheduling in graphical view
that help to plan, coordinate and track specific CPU utilization factor like throughput, waiting time,
turnaround time etc.
First-Come First-Serve Scheduling:
Process that arrives first is given the CPU first , that is less Arrival Time first
is a non-preemptive Scheduling .
FIFO (First In First Out) principle is used.