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

OS Unit - 2

The document discusses processes and process scheduling. It defines a process as a program in execution that represents a unit of work. A process has four main sections - stack, heap, text, and data. It describes the different states a process can be in, such as ready, running, waiting. It also discusses process control information maintained by the operating system like process ID and program counters. The document then covers threads, scheduling queues, preemptive and non-preemptive scheduling, and examples of scheduling algorithms like first come first serve and shortest job first.

Uploaded by

ZINKAL PATEL
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)
18 views

OS Unit - 2

The document discusses processes and process scheduling. It defines a process as a program in execution that represents a unit of work. A process has four main sections - stack, heap, text, and data. It describes the different states a process can be in, such as ready, running, waiting. It also discusses process control information maintained by the operating system like process ID and program counters. The document then covers threads, scheduling queues, preemptive and non-preemptive scheduling, and examples of scheduling algorithms like first come first serve and shortest job first.

Uploaded by

ZINKAL PATEL
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/ 23

Unit – 2

Process Concept:

 process is basically a program in execution. The execution of a process


must progress in a sequential fashion.
 A process is defined as an entity which represents the basic unit of work
to be implemented in the system.
 To put it in simple terms, we write our computer programs in a text file
and when we execute this program, it becomes a process which performs
all the tasks mentioned in the program.
 When a program is loaded into the memory and it becomes a process, it
can be divided into four sections ─ stack, heap, text and data. The
following image shows a simplified layout of a process inside main
memory –

pg. 1 ASST: PROF BANSARI SHARMA


S.N. Component & Description

1 Stack
The process Stack contains the temporary data such as
method/function parameters, return address and local variables.

2 Heap
This is dynamically allocated memory to a process during its run time.

3 Text
This includes the current activity represented by the value of Program
Counter and the contents of the processor's registers.

4 Data
This section contains the global and static variables.

Process states:

1. New: Newly Created Process (or) being-created process.

2. Ready: After creation process moves to Ready state, i.e. the


process is ready for execution.

3. Run: Currently running process in CPU (only one process at


a time can be under execution in a single processor).

4. Wait (or Block): When a process requests I/O access.

5. Complete (or Terminated): The process completed its execution.

pg. 2 ASST: PROF BANSARI SHARMA


6. Suspended Ready: When the ready queue becomes full, some
processes are moved to suspended ready state

7. Suspended Block: When waiting queue becomes full.

Process control:
 Process State
 Process Privileges
 Process ID
 Pointers
 Program Counters
 CPU Registers
 CPU Scheduling Information
 Memory Management Information

pg. 3 ASST: PROF BANSARI SHARMA


 Process State: Current state of the process i.e, ready, running, or waiting
etc.

 Process Priority: allow/ disallow access to the system resource.

 Process ID: Unique Identification for each of the process in the operating
system.

 Pointer: A pointer to parent process.

 Program Counter: Program counter is a pointer to the address of the


next instruction to be executed for this process.

pg. 4 ASST: PROF BANSARI SHARMA


 CPU Registers: Various CPU registers where process need to be stored
for execution for running state.

 Memory Management Information: Information of page table, memory


limit, segment table information.

 Accounting Information: It includes the amount of CPU used for process


execution, time limits, execution ID etc.

 Input Output Status Information: This include list of input output


devices allocated to the process.

Threads:
 A thread is a single sequential flow of execution of tasks of a process so
it is also known as thread of execution or thread of control.

 There is a way of thread execution inside the process of any operating


system.

 Apart from this, there can be more than one thread inside a process.

 Each thread of the same process makes use of a separate program


counter and a stack of activation records and control blocks.

 Thread is often referred to as a lightweight process.

 It takes far less time to create a new thread in an existing process than to
create a new process.
 Threads can share the common data, they do not need to use Inter-
Process communication.
 Context switching is faster when working with threads.
 It takes less time to terminate a thread than a process.

pg. 5 ASST: PROF BANSARI SHARMA


Uni - Process Scheduling Queues:
The OS maintains all PCBs in Process Scheduling Queues. The OS
maintains a separate queue for each of the process states and PCBs
of all processes in the same execution state are placed in the same
queue. When the state of a process is changed, its PCB is unlinked
from its current queue and moved to its new state queue.
The Operating System maintains the following important process
scheduling queues −
 Job queue − This queue keeps all the processes in the system.
 Ready queue − This queue keeps a set of all processes residing in main
memory, ready and waiting to execute. A new process is always put in
this queue.

pg. 6 ASST: PROF BANSARI SHARMA


 Device queues − The processes which are blocked due to unavailability of
an I/O device constitute this queue.

Preemptive:

 A processor can be preempted to execute the different processes in the


middle of any current process execution.
 CPU utilization is more efficient compared to Non-Preemptive
Scheduling.
 Waiting and response time of preemptive Scheduling is less.
 Preemptive Scheduling is prioritized. The highest priority process is a
process that is currently utilized.
 Preemptive Scheduling is flexible.
 Examples: – Shortest Remaining Time First, Round Robin, etc.
 Preemptive Scheduling algorithm can be pre-empted that is the process
can be Scheduled
 In this process, the CPU is allocated to the processes for a specific time
period.
 Preemptive algorithm has the overhead of switching the process from
the ready state to the running state and vice-versa.

pg. 7 ASST: PROF BANSARI SHARMA


Non-preemptive Scheduling:

 Once the processor starts its execution, it must finish it before executing
the other. It can’t be paused in the middle.
 CPU utilization is less efficient compared to preemptive Scheduling.
 Waiting and response time of the non-preemptive Scheduling method is
higher.
 When any process enters the state of running, the state of that process
is never deleted from the scheduler until it finishes its job.
 Non-preemptive Scheduling is rigid.
 Examples: First Come First Serve, Shortest Job First, Priority Scheduling,
etc.
 In non-preemptive scheduling process cannot be Scheduled
 In this process, CPU is allocated to the process until it terminates or
switches to the waiting state.
 Non-preemptive Scheduling has no such overhead of switching the
process from running into the ready state.

A Process Scheduler schedules different processes to be assigned to the


CPU based on particular scheduling algorithms. There are six popular
process scheduling algorithms which we are going to discuss in this
chapter −

 First-Come, First-Served (FCFS) Scheduling


 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Shortest Remaining Time
 Round Robin(RR) Scheduling
 Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive
algorithms are designed so that once a process enters the running state, it
cannot be preempted until it completes its allotted time, whereas the

pg. 8 ASST: PROF BANSARI SHARMA


preemptive scheduling is based on priority where a scheduler may preempt a
low priority running process anytime when a high priority process enters into a
ready state.

First Come First Serve (FCFS):

 Jobs are executed on first come, first serve basis.


 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.

1. Turn Around Time = Completion Time - Arrival Time


2. Waiting Time = Turnaround time - Burst Time

Process Arrival Burst Completion Turn Around Waiting


ID Time Time Time Time Time

0 0 2 2 2 0

1 1 6 8 7 1

2 2 4 12 10 6

3 3 9 21 18 9

4 6 12 33 29 17

Average Waiting Time = 33/5

pg. 9 ASST: PROF BANSARI SHARMA


= 6.6

Shortest Job First (SJF):

 This is also known as shortest job first, or SJF


 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known
in advance.
 Impossible to implement in interactive systems where required CPU
time is not known.
 The processer should know in advance how much time process will
take.

Given: Table of processes, and their Arrival time, Execution time

In SJF scheduling, the process with the lowest burst time, among the list of
available processes in the ready queue, is going to be scheduled next.

pg. 10 ASST: PROF BANSARI SHARMA


PID Arrival Burst Completion Turn Around Waiting
Time Time Time Time Time

1 1 7 8 7 0

2 3 3 13 10 7

3 6 2 10 4 2

4 7 10 31 24 14

5 9 8 21 12 4

Average Waiting Time = 27/5

= 5.4

pg. 11 ASST: PROF BANSARI SHARMA


Round Robin (RR):
 Round Robin is the preemptive process scheduling algorithm.
 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and
other process executes for a given time period.
 Context switching is used to save states of preempted processes.

The formula of Round Robin Turn Around Time = Completion Time – Arrival
Time burst time.
The formula of Round Robin Waiting Time (W.T): Time Difference between the
turnaround and the burst time.
The formula of Round Robin Waiting Time = Turn Around Time – Burst Time

pg. 12 ASST: PROF BANSARI SHARMA


Priority:

 Priority scheduling is a non-preemptive algorithm and one of the most


common scheduling algorithms in batch systems.
 Each process is assigned a priority. Process with highest priority is to be
executed first and so on.
 Processes with same priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time
requirements or any other resource requirement.

1. Turnaround Time = Completion Time - Arrival Time


2. Waiting Time = Turn Around Time - Burst Time

Process Priority Arrival Burst Completion Turn Waiting


Id Time Time Time Around Time
Time

1 2 0 1 1 1 0

2 6 1 7 22 21 14

3 3 2 3 5 3 0

4 5 3 6 16 13 7

5 4 4 5 10 6 1

6 10 5 15 45 40 25

7 9 6 8 30 24 16

Average Waiting Time = (0+14+0+7+1+25+16)/7 = 63/7 = 9

pg. 13 ASST: PROF BANSARI SHARMA


Shortest Remaining Time:

 Shortest remaining time (SRT) is the preemptive version of the SJN


algorithm.
 The processor is allocated to the job closest to completion but it can be
preempted by a newer ready job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time
is not known.
 It is often used in batch environments where short jobs need to give
preference.

pg. 14 ASST: PROF BANSARI SHARMA


Thread Scheduling:

Scheduling of threads involves two boundary scheduling,

 Scheduling of user level threads (ULT) to kernel level threads (KLT)


via lightweight process (LWP) by the application developer.

 Scheduling of kernel level threads by the system scheduler to


perform different unique OS functions.

pg. 15 ASST: PROF BANSARI SHARMA


System calls like PS:

A system call is a procedure that provides the interface between a process


and the operating system. It is the way by which a computer program
requests a service from the kernel of the operating system.

System calls are divided into 5 categories mainly:

 Process Control
 File Management
 Device Management
 Information Maintenance
 Communication

pg. 16 ASST: PROF BANSARI SHARMA


Process Control:

This system calls perform the task of process creation, process termination,
etc.
The Linux System calls under this are fork() , exit() , exec().

 fork()
 A new process is created by the fork() system call.
 A new process may be created with fork() without a new
program being run-the new sub-process simply continues
to execute exactly the same program that the first (parent)
process was running.
 It is one of the most widely used system calls under
process management.
 exit()
 The exit() system call is used by a program to terminate its
execution.
 The operating system reclaims resources that were used
by the process after the exit() system call.
 exec()
 A new program will start executing after a call to exec()
 Running a new program does not require that a new
process be created first: any process may call exec() at any
time. The currently running program is immediately
terminated, and the new program starts executing in the
context of the existing process.

File Management:

File management system calls handle file manipulation jobs like creating a
file, reading, and writing, etc. The Linux System calls under this are open(),
read(), write(), close().

 open():
 It is the system call to open a file.

pg. 17 ASST: PROF BANSARI SHARMA


 This system call just opens the file, to perform operations
such as read and write, we need to execute different
system call to perform the operations.
 read():
 This system call opens the file in reading mode
 We cannot edit the files with this system call.
 Multiple processes can execute the read() system call on
the same file simultaneously.
 write():
 This system call opens the file in writing mode
 We can edit the files with this system call.
 Multiple processes can not execute the write() system call
on the same file simultaneously.
 close():
 This system call closes the opened file.

Device Management:

Device management does the job of device manipulation like reading from
device buffers, writing into device buffers, etc. The Linux System calls under
this is ioctl().

 ioctl():
 ioctl() is referred to as Input and Output Control.
 ioctl is a system call for device-specific input/output
operations and other operations which cannot be
expressed by regular system calls.
Information Maintenance:

It handles information and its transfer between the OS and the user program.
In addition, OS keeps the information about all its processes and system calls
are used to access this information. The System calls under this are getpid(),
alarm(), sleep().

 getpid():
 getpid stands for Get the Process ID.
 The getpid() function shall return the process ID of the
calling process.

pg. 18 ASST: PROF BANSARI SHARMA


 The getpid() function shall always be successful and no
return value is reserved to indicate an error.
 alarm():
 This system call sets an alarm clock for the delivery of a
signal that when it has to be reached.
 It arranges for a signal to be delivered to the calling
process.
 sleep():
 This System call suspends the execution of the currently
running process for some interval of time
 Meanwhile, during this interval, another process is given
chance to execute.

Communication:

These types of system calls are specially used for inter-process


communications.
Two models are used for inter-process communication
1. Message Passing(processes exchange messages with one another)
2. Shared memory(processes share memory region to communicate)
The system calls under this are pipe() , shmget() ,mmap().

 pipe():
 The pipe() system call is used to communicate between
different Linux processes.
 It is mainly used for inter-process communication.
 The pipe() system function is used to open file descriptors.
 shmget():
 shmget stands for shared memory segment.
 It is mainly used for Shared memory communication.
 This system call is used to access the shared memory and
access the messages in order to communicate with the
process.

 mmap():
 This function call is used to map or unmap files or devices
into memory.
 The mmap() system call is responsible for mapping the
content of the file to the virtual memory space of the

pg. 19 ASST: PROF BANSARI SHARMA


process.

Fork:
 In an operating system, a fork is
a Unix or Linux system call to create a
new process from an existing running process. The
new process is a child process of the calling parent
process.
 In computer programming, a fork is
when developers take the source code for an existing
project and use it to create new software based on the
original code.

Negative Value: creation of a child process was unsuccessful.

Zero: Returned to the newly created child process.

Positive value: Returned to parent or caller. The value contains process ID of


newly created child process.

pg. 20 ASST: PROF BANSARI SHARMA


Join:

The join instruction is the that instruction in the process execution that
provides the medium to recombine two concurrent computations into
a single one. The join instruction has one parameter integer count that
specifies the number of computations which are to be joined. It
decrements the integer by one. If the value of the integer after
decrement is non-zero then the process terminates otherwise the
process continues execution with the next statement.

exec family:

pg. 21 ASST: PROF BANSARI SHARMA


 The exec family of system calls replaces the program
executed by a process. When a process calls exec, all
code (text) and data in the process is lost and
replaced with the executable of the new program.

 It comes under the header file unistd.h.


 There are many members in the exec family.

Wait:
pg. 22 ASST: PROF BANSARI SHARMA
 In computer operating systems, a process (or task) may wait on another
process to complete its execution.

 The parent process may then issue a wait system call, which suspends
the execution of the parent process while the child executes.

 A call to wait() blocks the calling process until one of its child processes
exits or a signal is received. After child process terminates,
parent continues its execution after wait system call instruction.

Child process may terminate due to any of these:

 It calls exit();
 It returns (an int) from main
 It receives a signal (from the OS or another process) whose default
action is to terminate.

pg. 23 ASST: PROF BANSARI SHARMA

You might also like